source: java2python/src/main/java/tudelft/utilities/j2p/t/J2P.java@ 361

Last change on this file since 361 was 361, checked in by wouter, 2 years ago

#110 added ImmutableList translator temporarily to the current project to see if we can get all working

File size: 4.6 KB
Line 
1package tudelft.utilities.j2p.t;
2
3import com.github.javaparser.ast.expr.FieldAccessExpr;
4import com.github.javaparser.ast.expr.MethodCallExpr;
5
6import tudelft.utilities.j2p.formatting.Block;
7import 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 */
25public 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 *
63 * @return any install needed to run this . This code will be pasted into
64 * setup.sh.
65 */
66 public String getInstall();
67
68 /**
69 * @param node a {@link FieldAccessExpr}
70 * @return code accessing the equivalent field in Python
71 */
72 public ExpressionLine field(FieldAccessExpr node);
73
74 /************ FACTORY METHODS ************/
75 /**
76 *
77 * @param node the {@link Object} to be translated
78 * @return translator for that node type
79 * @throws ClassNotFoundException
80 */
81 public static J2P fetch(Object node) throws ClassNotFoundException {
82 return fetch(node.getClass().getName());
83 }
84
85 /**
86 *
87 * @param fully_qualified_name, as returned by {@link Class#getName()}. name
88 * can also be a primitive type, eg "int",
89 * referring to the java int.
90 * @return a J2P that can translate the given fully_qualified_name
91 * @throws IllegalArgumentException if the argument can not be handled, eg
92 * because there is no translator for the
93 * specified class
94 */
95 public static J2P fetch(String fully_qualified_name)
96 throws IllegalArgumentException {
97
98 if (StubPrimitive.isPrimitive(fully_qualified_name)) {
99 return new StubPrimitive(fully_qualified_name);
100 }
101
102 String trname = BASE + fully_qualified_name;
103 Class<?> clazz;
104 try {
105 clazz = Class.forName(trname);
106 } catch (ClassNotFoundException e) {
107 // it's not explicitly stubbed.
108 // If it's not a system class, try to stub it
109 if (!(fully_qualified_name.startsWith("java")))
110 try {
111 return new Stub(Class.forName(fully_qualified_name));
112 } catch (ClassNotFoundException e1) {
113 // we are going to throw anyway
114 }
115
116 throw new IllegalArgumentException("No translator found for "
117 + fully_qualified_name + ", missing class " + trname
118 + ". Please add this translator to your project.");
119 }
120
121 // if we get here, we found explicit stub clazz
122 try {
123 if (!J2P.class.isAssignableFrom(clazz))
124 throw new ClassCastException(
125 "class " + trname + " is not implementing J2P");
126 return (J2P) clazz.getDeclaredConstructor().newInstance();
127 } catch (Exception e) {
128 throw new IllegalArgumentException("Found translator " + trname
129 + " but failed to create instance", e);
130 }
131 }
132
133}
Note: See TracBrowser for help on using the repository browser.