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 the class name (simple name, not full.class.path) of the
|
---|
45 | * equivalent python class, eg "sys" if the class is
|
---|
46 | * java.lang.System. Or MyClass if the class is my.package.MyClass.
|
---|
47 | * It is assumed that the import (see {@link #getImport()}) will
|
---|
48 | * ensure this name is properly defined.
|
---|
49 | */
|
---|
50 | public String getName();
|
---|
51 |
|
---|
52 | /**
|
---|
53 | *
|
---|
54 | * @return the import statement needed to use this class. Can return null if
|
---|
55 | * no import is needed. <br/>
|
---|
56 | * NOTE: you should not import a class inside the class definition
|
---|
57 | * itself (recursive import).
|
---|
58 | */
|
---|
59 | public String getImport();
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * @param node a {@link FieldAccessExpr}
|
---|
63 | * @return code accessing the equivalent field in Python
|
---|
64 | */
|
---|
65 | public ExpressionLine field(FieldAccessExpr node);
|
---|
66 |
|
---|
67 | /************ FACTORY METHODS ************/
|
---|
68 | /**
|
---|
69 | *
|
---|
70 | * @param node the {@link Object} to be translated
|
---|
71 | * @return translator for that node type
|
---|
72 | * @throws ClassNotFoundException
|
---|
73 | */
|
---|
74 | public static J2P fetch(Object node) throws ClassNotFoundException {
|
---|
75 | return fetch(node.getClass().getName());
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | *
|
---|
80 | * @param fully_qualified_name, as returned by {@link Class#getName()}. name
|
---|
81 | * can also be a primitive type, eg "int",
|
---|
82 | * referring to the java int.
|
---|
83 | * @return a J2P that can translate the given fully_qualified_name
|
---|
84 | * @throws IllegalArgumentException if the argument can not be handled, eg
|
---|
85 | * because there is no translator for the
|
---|
86 | * specified class
|
---|
87 | */
|
---|
88 | public static J2P fetch(String fully_qualified_name)
|
---|
89 | throws IllegalArgumentException {
|
---|
90 |
|
---|
91 | if (StubPrimitive.isPrimitive(fully_qualified_name)) {
|
---|
92 | return new StubPrimitive(fully_qualified_name);
|
---|
93 | }
|
---|
94 |
|
---|
95 | String trname = BASE + fully_qualified_name;
|
---|
96 | Class<?> clazz;
|
---|
97 | try {
|
---|
98 | clazz = Class.forName(trname);
|
---|
99 | } catch (ClassNotFoundException e) {
|
---|
100 | // it's not explicitly stubbed.
|
---|
101 | // If it's not a system class, try to stub it
|
---|
102 | if (!(fully_qualified_name.startsWith("java")))
|
---|
103 | try {
|
---|
104 | return new Stub(Class.forName(fully_qualified_name));
|
---|
105 | } catch (ClassNotFoundException e1) {
|
---|
106 | // we are going to throw anyway
|
---|
107 | }
|
---|
108 |
|
---|
109 | throw new IllegalArgumentException("No translator found for "
|
---|
110 | + fully_qualified_name + ", missing class " + trname
|
---|
111 | + ". Please add this translator to your project.");
|
---|
112 | }
|
---|
113 |
|
---|
114 | // if we get here, we found explicit stub clazz
|
---|
115 | try {
|
---|
116 | if (!J2P.class.isAssignableFrom(clazz))
|
---|
117 | throw new ClassCastException(
|
---|
118 | "class " + trname + " is not implementing J2P");
|
---|
119 | return (J2P) clazz.getDeclaredConstructor().newInstance();
|
---|
120 | } catch (Exception e) {
|
---|
121 | throw new IllegalArgumentException("Found translator " + trname
|
---|
122 | + " but failed to create instance", e);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|