1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
package me.snowleo.yaml;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
@SupportedAnnotationTypes("me.snowleo.yaml.YamlClass")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class YamlPropertyProcessor extends AbstractProcessor
{
private transient Filer filer;
private transient Messager messager;
private Elements eltUtils;
public YamlPropertyProcessor()
{
super();
}
@Override
public void init(final ProcessingEnvironment processingEnv)
{
super.init(processingEnv);
filer = processingEnv.getFiler();
messager = processingEnv.getMessager();
eltUtils = processingEnv.getElementUtils();
}
@Override
public boolean process(final Set<? extends TypeElement> set, final RoundEnvironment roundEnv)
{
for (Element elem : roundEnv.getElementsAnnotatedWith(YamlClass.class))
{
if (elem.getKind() != ElementKind.CLASS)
{
messager.printMessage(Diagnostic.Kind.ERROR, "YamlClass has to be a class", elem);
continue;
}
final TypeElement clazz = (TypeElement)elem;
String superClassName = clazz.getSuperclass().toString();
if (!superClassName.startsWith(clazz.getEnclosingElement().toString()))
{
superClassName = clazz.getEnclosingElement().toString() + "." + superClassName;
}
if (!superClassName.equalsIgnoreCase(clazz.getQualifiedName().toString() + "Yaml"))
{
messager.printMessage(Diagnostic.Kind.ERROR, "YamlClass must be extended by " + clazz.getQualifiedName().toString() + "Yaml", elem);
continue;
}
final List<VariableElement> fields = new ArrayList<VariableElement>();
for (VariableElement field : ElementFilter.fieldsIn(clazz.getEnclosedElements()))
{
if (!(field.getModifiers().contains(Modifier.STATIC))
&& field.getModifiers().contains(Modifier.PROTECTED))
{
fields.add(field);
}
else if (field.getAnnotation(YamlComment.class) != null)
{
messager.printMessage(Diagnostic.Kind.ERROR, "YamlComment fields have to be nonstatic and protected.", field);
continue;
}
}
if (fields.isEmpty())
{
messager.printMessage(Diagnostic.Kind.ERROR, "YamlClass needs at least one protected field.", clazz);
continue;
}
createImplclass(clazz, fields);
createSuperclass(clazz, fields);
}
return true;
}
private void createSuperclass(final TypeElement clazz, final List<VariableElement> fields)
{
try
{
final JavaFileObject file = filer.createSourceFile(clazz.getQualifiedName() + "Yaml");
messager.printMessage(Diagnostic.Kind.NOTE, "Creating " + file.toUri());
final Writer writer = file.openWriter();
//Add the content to the newly generated file.
final PrintWriter pwriter = new PrintWriter(writer);
try
{
pwriter.print("package ");
pwriter.print(clazz.getEnclosingElement());
pwriter.println(';');
pwriter.println();
pwriter.print("public class ");
pwriter.print(clazz.getSimpleName());
pwriter.print("Yaml extends BaseYaml<");
pwriter.print(clazz.getSimpleName());
pwriter.println("> {");
pwriter.println(" @Override");
pwriter.print(" protected Class<");
pwriter.print(clazz.getSimpleName());
pwriter.print("YamlImpl> getClazz() { return ");
pwriter.print(clazz.getSimpleName());
pwriter.println("YamlImpl.class; };");
for (VariableElement var : fields)
{
final TypeMirror type = var.asType();
final String name = capitalize(var.getSimpleName().toString());
pwriter.print(" public ");
pwriter.print(type.toString());
if (type.toString().equalsIgnoreCase("boolean"))
{
pwriter.print(" is");
}
else
{
pwriter.print(" get");
}
pwriter.print(name);
pwriter.println("() { throw new AssertionError(\"Cannot be called.\"); }");
pwriter.print(" public void set");
pwriter.print(name);
pwriter.print("(final ");
pwriter.print(type.toString());
pwriter.print(' ');
pwriter.print(name);
pwriter.println(") { throw new AssertionError(\"Cannot be called.\"); }");
}
pwriter.println("}");
pwriter.flush();
}
finally
{
pwriter.close();
}
}
catch (IOException ex)
{
messager.printMessage(Diagnostic.Kind.ERROR, ex.toString());
}
}
private void createImplclass(final TypeElement clazz, final List<VariableElement> fields)
{
try
{
final JavaFileObject file = filer.createSourceFile(clazz.getQualifiedName() + "YamlImpl");
messager.printMessage(Diagnostic.Kind.NOTE, "Creating " + file.toUri());
final Writer writer = file.openWriter();
//Add the content to the newly generated file.
final PrintWriter pwriter = new PrintWriter(writer);
try
{
pwriter.print("package ");
pwriter.print(clazz.getEnclosingElement());
pwriter.println(';');
pwriter.println();
pwriter.print("public class ");
pwriter.print(clazz.getSimpleName());
pwriter.print("YamlImpl extends ");
pwriter.print(clazz.getSimpleName());
pwriter.println(" {");
for (VariableElement var : fields)
{
final TypeMirror type = var.asType();
final String name = capitalize(var.getSimpleName().toString());
pwriter.print(" public ");
pwriter.print(type.toString());
if (type.toString().equalsIgnoreCase("boolean"))
{
pwriter.print(" is");
}
else
{
pwriter.print(" get");
}
pwriter.print(name);
pwriter.print("() { return this.");
pwriter.print(var.getSimpleName().toString());
pwriter.println("; }");
pwriter.print(" public void set");
pwriter.print(name);
pwriter.print("(final ");
pwriter.print(type.toString());
pwriter.print(' ');
pwriter.print(name);
pwriter.print(") { this.");
pwriter.print(var.getSimpleName().toString());
pwriter.print(" = ");
pwriter.print(name);
pwriter.println("; }");
}
pwriter.println("}");
pwriter.flush();
}
finally
{
pwriter.close();
}
}
catch (IOException ex)
{
messager.printMessage(Diagnostic.Kind.ERROR, ex.toString());
}
}
private static String capitalize(String name)
{
char[] c = name.toCharArray();
c[0] = Character.toUpperCase(c[0]);
return new String(c);
}
}
|