1 | package tudelft.utilities.j2p.t;
|
---|
2 |
|
---|
3 | import com.github.javaparser.ast.expr.FieldAccessExpr;
|
---|
4 | import com.github.javaparser.ast.expr.MethodCallExpr;
|
---|
5 |
|
---|
6 | import tudelft.utilities.j2p.formatting.Block;
|
---|
7 | import tudelft.utilities.j2p.formatting.ExpressionLine;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * The interface to java-python translator. A translator helps to translate java
|
---|
11 | * Class access - function calls and field access. The result is a {@link Block}
|
---|
12 | * containing python code and imports. Translators for a class full.class.path
|
---|
13 | * are stored in the package
|
---|
14 | * <code>tudelft.utilities.j2p.t.full.class.path</code>. See also
|
---|
15 | * {@link #fetch(Object)}
|
---|
16 | * <p>
|
---|
17 | * Third party libraries should also put their packages in this same
|
---|
18 | * <code>tudelft.utilities.j2p.t.XXX</code> package. For instance a translator
|
---|
19 | * for "com.mycompany" could provide a jar with classes in
|
---|
20 | * <code>tudelft.utilities.j2p.t.com.mycompany</code>.
|
---|
21 | *
|
---|
22 | * @param <NodeType> the type of the java object that is translated into Python
|
---|
23 | * code.
|
---|
24 | */
|
---|
25 | public interface J2P {
|
---|
26 | static final String BASE = "tudelft.utilities.j2p.t.";
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Translates a MethodCall in java to python The impplementation may have to
|
---|
30 | * do further calls to translate scope and arguments. This is deliberately
|
---|
31 | * left to the translator so that it can manipulate function names, argument
|
---|
32 | * orders etc. Specific example : SCOPE.getClass() -> type(SCOPE). Here the
|
---|
33 | * scope SCOPE needs to be put as argument inside the call. This potential
|
---|
34 | * reversal requires us to pass the SCOPE into the call translator....
|
---|
35 | *
|
---|
36 | *
|
---|
37 | * @param callexp the {@link MethodCallExpr},
|
---|
38 | * @return a Block with the translation
|
---|
39 | */
|
---|
40 | public ExpressionLine call(MethodCallExpr callexp);
|
---|
41 |
|
---|
42 | /**
|
---|
43 | *
|
---|
44 | * @return an {@link ExpressionLine} with the simple name, plus the required
|
---|
45 | * imports and installs to use this. eg when there is a Range object
|
---|
46 | * in the immutablelist package, this might return an ExpressionLine
|
---|
47 | * with text "Range", import "from tudelft.immutablelist.Range
|
---|
48 | * import Range" and install "utilities@http://.....".
|
---|
49 | */
|
---|
50 | public ExpressionLine getName();
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * @param node a {@link FieldAccessExpr}
|
---|
54 | * @return code accessing the equivalent field in Python
|
---|
55 | */
|
---|
56 | public ExpressionLine field(FieldAccessExpr node);
|
---|
57 |
|
---|
58 | /************ FACTORY METHODS ************/
|
---|
59 | /**
|
---|
60 | *
|
---|
61 | * @param node the {@link Object} to be translated
|
---|
62 | * @return translator for that node type
|
---|
63 | * @throws ClassNotFoundException
|
---|
64 | */
|
---|
65 | public static J2P fetch(Object node) throws ClassNotFoundException {
|
---|
66 | return fetch(node.getClass().getName());
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | *
|
---|
71 | * @param fully_qualified_name, as returned by {@link Class#getName()}. name
|
---|
72 | * can also be a primitive type, eg "int",
|
---|
73 | * referring to the java int.
|
---|
74 | * @return a J2P that can translate the given fully_qualified_name
|
---|
75 | * @throws IllegalArgumentException if the argument can not be handled, eg
|
---|
76 | * because there is no translator for the
|
---|
77 | * specified class
|
---|
78 | */
|
---|
79 | public static J2P fetch(String fully_qualified_name)
|
---|
80 | throws IllegalArgumentException {
|
---|
81 |
|
---|
82 | if (StubPrimitive.isPrimitive(fully_qualified_name)) {
|
---|
83 | return new StubPrimitive(fully_qualified_name);
|
---|
84 | }
|
---|
85 |
|
---|
86 | String trname = BASE + fully_qualified_name;
|
---|
87 | Class<?> clazz;
|
---|
88 | try {
|
---|
89 | clazz = Class.forName(trname);
|
---|
90 | } catch (ClassNotFoundException e) {
|
---|
91 | // it's not explicitly stubbed.
|
---|
92 | // If it's not a system class, try to stub it
|
---|
93 | if (!(fully_qualified_name.startsWith("java")))
|
---|
94 | try {
|
---|
95 | return new Stub(Class.forName(fully_qualified_name));
|
---|
96 | } catch (ClassNotFoundException e1) {
|
---|
97 | // we are going to throw anyway
|
---|
98 | }
|
---|
99 |
|
---|
100 | throw new IllegalArgumentException("No translator found for "
|
---|
101 | + fully_qualified_name + ", missing class " + trname
|
---|
102 | + ". Please add this translator to your project.");
|
---|
103 | }
|
---|
104 |
|
---|
105 | // if we get here, we found explicit stub clazz
|
---|
106 | try {
|
---|
107 | if (!J2P.class.isAssignableFrom(clazz))
|
---|
108 | throw new ClassCastException(
|
---|
109 | "class " + trname + " is not implementing J2P");
|
---|
110 | return (J2P) clazz.getDeclaredConstructor().newInstance();
|
---|
111 | } catch (Exception e) {
|
---|
112 | throw new IllegalArgumentException("Found translator " + trname
|
---|
113 | + " but failed to create instance", e);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|