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