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

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

#115 extracted annotation processing code. enhanced typechecking of Annotated Node

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