source: java2python/src/main/java/tudelft/utilities/j2p/t/StubPrimitive.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: 2.5 KB
Line 
1package tudelft.utilities.j2p.t;
2
3import java.util.Arrays;
4import java.util.List;
5
6import com.github.javaparser.ast.Node;
7import com.github.javaparser.ast.expr.AnnotationExpr;
8import com.github.javaparser.ast.expr.FieldAccessExpr;
9import com.github.javaparser.ast.expr.MethodCallExpr;
10import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
11
12import tudelft.utilities.j2p.formatting.Block;
13import tudelft.utilities.j2p.formatting.ExpressionLine;
14
15/***
16 * Stub but for primitives (int, boolean, byte, char..)
17 */
18public class StubPrimitive implements J2P {
19
20 private static final List<String> primitivetypes = Arrays.asList("boolean",
21 "byte", "char", "float", "int", "long", "short", "double", "void");
22
23 private String type;
24
25 private ExpressionLine INTTYPE = new ExpressionLine("int");
26
27 private ExpressionLine BOOLTYPE = new ExpressionLine("bool");
28
29 private ExpressionLine CHARTYPE = new ExpressionLine("str");
30
31 private ExpressionLine FLOATTYPE = new ExpressionLine("float");
32
33 // PEP-484: None is considered equivalent to type(None)
34 private ExpressionLine VOIDTYPE = new ExpressionLine("None");
35
36 /**
37 *
38 * @param type
39 * @return true if given type is primitive type. Note that "void" is also
40 * supported as if it were a primitive type. This is because there
41 * is not a "void" class and we need a Stub for the void type
42 */
43 static public boolean isPrimitive(String type) {
44 return primitivetypes.contains(type);
45 }
46
47 /**
48 *
49 * @param primitive "int","char", etc
50 */
51 public StubPrimitive(String primitive) {
52 this.type = primitive;
53 getName(); // check if type is legla
54 }
55
56 @Override
57 public ExpressionLine call(MethodCallExpr callexp) {
58 throw new IllegalArgumentException(
59 "Not implemented " + this.getClass() + callexp);
60 }
61
62 @Override
63 public ExpressionLine getName() {
64 switch (type) {
65 case "int":
66 return INTTYPE;
67 case "boolean":
68 return BOOLTYPE;
69 case "char":
70 return CHARTYPE; // should we?
71 case "long":
72 return INTTYPE;
73 case "float":
74 return FLOATTYPE;
75 case "double":
76 return FLOATTYPE;
77 case "short":
78 return INTTYPE;
79 case "void":
80 return VOIDTYPE;
81
82 }
83 throw new IllegalArgumentException("Unknown type " + type);
84 }
85
86 @Override
87 public ExpressionLine field(FieldAccessExpr node) {
88 throw new IllegalArgumentException(
89 "primitive " + type + "has no field");
90 }
91
92 @Override
93 public <T extends Node & NodeWithAnnotations<?>> Block annotate(T code,
94 AnnotationExpr annotation) {
95 throw new IllegalArgumentException(
96 "Primitives do not support annotation");
97 }
98
99}
Note: See TracBrowser for help on using the repository browser.