11
11
import java .io .IOException ;
12
12
import javax .lang .model .element .TypeElement ;
13
13
import javax .lang .model .element .Element ;
14
+ import javax .lang .model .element .ElementKind ;
14
15
import javax .lang .model .element .ExecutableElement ;
15
16
import javax .lang .model .element .VariableElement ;
16
17
import javax .lang .model .type .TypeMirror ;
@@ -28,6 +29,53 @@ public class Processor extends AbstractProcessor {
28
29
Messager messager ;
29
30
String prefix = "NQP" ;
30
31
32
+ static class AstTypes {
33
+ TypeMirror nodeClass ;
34
+ TypeMirror nodesClass ;
35
+ TypeMirror intClass ;
36
+ TypeMirror numClass ;
37
+ TypeMirror strClass ;
38
+ TypeMirror scopeClass ;
39
+
40
+ AstTypes (AstBuilder annotation ) {
41
+ try {
42
+ annotation .nodeClass ();
43
+ } catch (MirroredTypeException e ) {
44
+ nodeClass = e .getTypeMirror ();
45
+ }
46
+
47
+ try {
48
+ annotation .nodesClass ();
49
+ } catch (MirroredTypeException e ) {
50
+ nodesClass = e .getTypeMirror ();
51
+ }
52
+
53
+ try {
54
+ annotation .intClass ();
55
+ } catch (MirroredTypeException e ) {
56
+ intClass = e .getTypeMirror ();
57
+ }
58
+
59
+ try {
60
+ annotation .numClass ();
61
+ } catch (MirroredTypeException e ) {
62
+ numClass = e .getTypeMirror ();
63
+ }
64
+
65
+ try {
66
+ annotation .strClass ();
67
+ } catch (MirroredTypeException e ) {
68
+ strClass = e .getTypeMirror ();
69
+ }
70
+
71
+ try {
72
+ annotation .scopeClass ();
73
+ } catch (MirroredTypeException e ) {
74
+ scopeClass = e .getTypeMirror ();
75
+ }
76
+ }
77
+ }
78
+
31
79
private String opNameFromClassName (String className ) {
32
80
return className .replaceFirst ("^" + Pattern .quote (prefix ), "" )
33
81
.replaceFirst ("IntNode$" , "_i" )
@@ -40,43 +88,55 @@ private String opNameFromClassName(String className) {
40
88
.toLowerCase ();
41
89
}
42
90
43
- private void writeBuildMethod (TypeMirror nodeClass , TypeMirror nodesClass , TypeMirror intClass , TypeMirror numClass , TypeMirror strClass , PrintWriter writer , RoundEnvironment roundEnv ) {
91
+ private void writeBuildMethod (AstTypes astTypes , PrintWriter writer , RoundEnvironment roundEnv ) {
44
92
writer .append (" public NQPNode buildSimple(SixModelObject node, NQPScope scope, ThreadContext tc) {\n " );
45
93
46
94
writer .append (" switch (node.at_pos_boxed(tc, 0).get_str(tc)) {\n " );
47
95
48
96
for (Element element : roundEnv .getElementsAnnotatedWith (Deserializer .class )) {
49
97
50
- ExecutableElement constructor = (ExecutableElement ) element ;
98
+ ExecutableElement executableElement = (ExecutableElement ) element ;
51
99
TypeElement type = (TypeElement ) element .getEnclosingElement ();
52
100
53
101
String gotOpName = ((Deserializer )element .getAnnotation (Deserializer .class )).value ();
54
102
103
+ String typeName = type .getQualifiedName ().toString ();
104
+
105
+ String call = element .getKind () == ElementKind .CONSTRUCTOR
106
+ ? "new " + typeName
107
+ : typeName + "." + executableElement .getSimpleName ().toString ();
108
+
55
109
String opName = gotOpName .equals ("" )
56
110
? opNameFromClassName (type .getSimpleName ().toString ())
57
111
: gotOpName ;
58
112
59
- writer .append (" case \" " + opName + "\" : return new " + type . getQualifiedName () + "(" );
113
+ writer .append (" case \" " + opName + "\" : return " + call + "(" );
60
114
115
+ boolean first = true ;;
61
116
int i = 0 ;
62
117
63
- for (VariableElement param : constructor . getParameters ()) {
118
+ for (VariableElement param : executableElement . getParameters ()) {
64
119
TypeMirror paramType = param .asType ();
65
120
66
- if (i != 0 ) {
121
+ if (first ) {
122
+ first = false ;
123
+ } else {
67
124
writer .append ("," );
68
125
}
69
126
70
- if (paramType .equals (nodeClass )) {
127
+ if (paramType .equals (astTypes . nodeClass )) {
71
128
writer .append ("build(node.at_pos_boxed(tc, " + (i +1 ) + "), scope, tc)" );
72
- } else if (paramType .equals (nodesClass )) {
129
+ } else if (paramType .equals (astTypes . nodesClass )) {
73
130
writer .append ("expressions(node, " + (i +1 ) + ", scope, tc)" );
74
- } else if (paramType .equals (intClass )) {
131
+ } else if (paramType .equals (astTypes . intClass )) {
75
132
writer .append ("node.at_pos_boxed(tc, " + (i +1 ) + ").get_int(tc)" );
76
- } else if (paramType .equals (numClass )) {
133
+ } else if (paramType .equals (astTypes . numClass )) {
77
134
writer .append ("node.at_pos_boxed(tc, " + (i +1 ) + ").get_num(tc)" );
78
- } else if (paramType .equals (strClass )) {
135
+ } else if (paramType .equals (astTypes . strClass )) {
79
136
writer .append ("node.at_pos_boxed(tc, " + (i +1 ) + ").get_str(tc)" );
137
+ } else if (paramType .equals (astTypes .scopeClass )) {
138
+ writer .append ("scope" );
139
+ i --;
80
140
} else {
81
141
processingEnv .getMessager ().printMessage (Diagnostic .Kind .ERROR , "Wrong param type: " + paramType .toString ());
82
142
}
@@ -108,43 +168,8 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
108
168
for (Element element : roundEnv .getElementsAnnotatedWith (AstBuilder .class )) {
109
169
TypeElement type = (TypeElement ) element ;
110
170
111
- AstBuilder annotation = element .getAnnotation (AstBuilder .class );
112
-
113
- TypeMirror nodeClass = null ;
114
- TypeMirror nodesClass = null ;
115
- TypeMirror intClass = null ;
116
- TypeMirror numClass = null ;
117
- TypeMirror strClass = null ;
118
-
119
- try {
120
- annotation .nodeClass ();
121
- } catch (MirroredTypeException e ) {
122
- nodeClass = e .getTypeMirror ();
123
- }
124
-
125
- try {
126
- annotation .nodesClass ();
127
- } catch (MirroredTypeException e ) {
128
- nodesClass = e .getTypeMirror ();
129
- }
171
+ AstTypes astTypes = new AstTypes (element .getAnnotation (AstBuilder .class ));
130
172
131
- try {
132
- annotation .intClass ();
133
- } catch (MirroredTypeException e ) {
134
- intClass = e .getTypeMirror ();
135
- }
136
-
137
- try {
138
- annotation .numClass ();
139
- } catch (MirroredTypeException e ) {
140
- numClass = e .getTypeMirror ();
141
- }
142
-
143
- try {
144
- annotation .strClass ();
145
- } catch (MirroredTypeException e ) {
146
- strClass = e .getTypeMirror ();
147
- }
148
173
149
174
String builderClass = type .getQualifiedName ().toString ();
150
175
String generatedClassQualified = builderClass + "Gen" ;
@@ -163,7 +188,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
163
188
164
189
writer .append ("public class " + generatedClassSimple + " extends " + builderClass + " {\n " );
165
190
166
- writeBuildMethod (nodeClass , nodesClass , intClass , numClass , strClass , writer , roundEnv );
191
+ writeBuildMethod (astTypes , writer , roundEnv );
167
192
168
193
writer .append ("}\n " );
169
194
}
0 commit comments