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

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

#113 added first hook to do annotation processing

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