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