forked from pardom-zz/ActiveAndroid
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAnnotationProcessor.java
More file actions
337 lines (287 loc) · 13.9 KB
/
AnnotationProcessor.java
File metadata and controls
337 lines (287 loc) · 13.9 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package com.activeandroid.internal;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
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.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic.Kind;
import javax.tools.JavaFileObject;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
public final class AnnotationProcessor extends AbstractProcessor {
private static final String MODEL = "model";
private static final String CURSOR = "cursor";
private static final String CONTENT_VALUES = "contentValues";
private static final String COLUMNS_ORDERED = "columnsOrdered";
private RoundEnvironment env;
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
this.env = env;
if (annotations.size() > 0) {
parseColumns();
}
return true;
}
@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> supportedTypes = new HashSet<String>();
supportedTypes.add(Column.class.getCanonicalName());
return supportedTypes;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
private void parseColumns() {
Set<? extends Element> columns = env.getElementsAnnotatedWith(Column.class);
Map<TypeElement, Set<VariableElement>> tables = new HashMap<TypeElement, Set<VariableElement>>();
for (Element element : columns) {
if (element instanceof VariableElement == false || element.getKind() != ElementKind.FIELD) {
error("@Column annotation should be applied only to local variables", element);
continue;
}
VariableElement columnElement = (VariableElement) element;
if (checkColumnModifiers(columnElement) == false)
continue;
TypeElement tableElement = null;
if (element.getEnclosingElement() instanceof TypeElement)
tableElement = (TypeElement) element.getEnclosingElement();
else
error("@Column annotation located not inside of class", element);
if (checkTableModifiers(tableElement) == false)
continue;
Set<VariableElement> columnsElements = tables.get(tableElement);
if (columnsElements == null) {
columnsElements = new HashSet<VariableElement>();
tables.put(tableElement, columnsElements);
}
columnsElements.add(columnElement);
}
for (TypeElement table : tables.keySet()) {
generate(table, tables.get(table));
}
}
private void generate(TypeElement tableElement, Set<VariableElement> columns) {
String packageName = processingEnv.getElementUtils().getPackageOf(tableElement).getQualifiedName().toString();
String className = tableElement.getQualifiedName().toString();
String fillerClassName = getClassName(tableElement, packageName) + ModelFiller.SUFFIX;
try {
JavaFileObject jfo = processingEnv.getFiler().createSourceFile(packageName + "." + fillerClassName, tableElement);
Writer writer = jfo.openWriter();
writer.write("//Generated by ActiveAndroid. Do not modify\n");
writer.write("package " + packageName + ";\n\n");
writer.write("import java.util.ArrayList;\n");
writer.write("import java.util.Arrays;\n");
writer.write("import java.util.List;\n\n");
writer.write("import com.activeandroid.internal.ModelHelper;\n");
writer.write("import com.activeandroid.internal.ModelFiller;\n");
writer.write("\n");
writer.write("public class " + fillerClassName + " extends ModelFiller {\n\n");
writer.write(" public void loadFromCursor(com.activeandroid.Model genericModel, android.database.Cursor " + CURSOR + ") {\n");
writer.write(" if (superModelFiller != null)\n");
writer.write(" superModelFiller.loadFromCursor(genericModel, " + CURSOR + ");\n");
writer.write(" List<String> " + COLUMNS_ORDERED + " = new ArrayList<String>(Arrays.asList(" + CURSOR + ".getColumnNames()));\n");
writer.write(" " + className + " " + MODEL + " = (" + className + ") genericModel;\n");
writer.write(getLoadFromCursorCode(columns));
writer.write(" }\n\n");
writer.write(" ");
writer.write("public void fillContentValues(com.activeandroid.Model genericModel, android.content.ContentValues " + CONTENT_VALUES + ") {\n");
writer.write(" if (superModelFiller != null)\n");
writer.write(" superModelFiller.fillContentValues(genericModel, " + CONTENT_VALUES + ");\n");
writer.write(" " + className + " " + MODEL + " = (" + className + ") genericModel;\n");
writer.write(getFillContentValuesCode(columns));
writer.write(" }\n");
writer.write("}");
writer.flush();
writer.close();
} catch (IOException exception) {
processingEnv.getMessager().printMessage(Kind.ERROR, exception.getMessage());
}
}
private String getLoadFromCursorCode(Set<VariableElement> columns) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(" int i = -1; // column index \n");
final String nullCheck = CURSOR + ".isNull(i) ? null : ";
for (VariableElement column : columns) {
Column annotation = column.getAnnotation(Column.class);
String fieldName = annotation.name();
if (fieldName == null || fieldName.isEmpty())
fieldName = column.getSimpleName().toString();
TypeMirror typeMirror = column.asType();
boolean notPrimitiveType = typeMirror instanceof DeclaredType;
String type = typeMirror.toString() + ".class";
String getColumnIndex = COLUMNS_ORDERED + ".indexOf(\"" + fieldName + "\")";
String getColumnIndexAssignment = "i = " + getColumnIndex + "; \n";
stringBuilder.append(" " + getColumnIndexAssignment );
if (notPrimitiveType) {
stringBuilder.append(" if (ModelHelper.isSerializable(" + type + ")) {\n");
stringBuilder.append(" " + MODEL + "." + column.getSimpleName() + " = (" + typeMirror.toString() + ") ModelHelper.getSerializable(cursor, " + type + ", i);\n");
stringBuilder.append(" } else {\n");
stringBuilder.append(" " + MODEL + "." + column.getSimpleName() + " = ");
} else {
stringBuilder.append(" " + MODEL + "." + column.getSimpleName() + " = ");
}
if (isTypeOf(typeMirror, Integer.class) || isTypeOf(typeMirror, Byte.class) || isTypeOf(typeMirror, Short.class) )
stringBuilder.append(nullCheck).append(CURSOR + ".getInt(i);\n");
else if (isTypeOf(typeMirror, Long.class))
stringBuilder.append(nullCheck).append(CURSOR + ".getLong(i);\n");
else if (isTypeOf(typeMirror, Float.class))
stringBuilder.append(nullCheck).append(CURSOR + ".getFloat(i);\n");
else if (isTypeOf(typeMirror, Double.class))
stringBuilder.append(nullCheck).append(CURSOR + ".getDouble(i);\n");
else if (isTypeOf(typeMirror, int.class))
stringBuilder.append(CURSOR + ".getInt(i);\n");
else if (isTypeOf(typeMirror, byte.class))
stringBuilder.append(CURSOR + ".getInt(i);\n");
else if (isTypeOf(typeMirror, short.class))
stringBuilder.append(CURSOR + ".getInt(i);\n");
else if (isTypeOf(typeMirror, long.class))
stringBuilder.append(CURSOR + ".getLong(i);\n");
else if (isTypeOf(typeMirror, float.class))
stringBuilder.append(CURSOR + ".getFloat(i);\n");
else if (isTypeOf(typeMirror, double.class))
stringBuilder.append(CURSOR + ".getDouble(i);\n");
else if (isTypeOf(typeMirror, Boolean.class))
stringBuilder.append(nullCheck).append(CURSOR + ".getInt(i) != 0;\n");
else if (isTypeOf(typeMirror, boolean.class))
stringBuilder.append(CURSOR + ".getInt(i) != 0;\n");
else if (isTypeOf(typeMirror, char.class))
stringBuilder.append(CURSOR + ".getString(i);\n");
else if (isTypeOf(typeMirror, Character.class))
stringBuilder.append(nullCheck).append(CURSOR + ".getString(i);\n");
else if (isTypeOf(typeMirror, String.class))
stringBuilder.append(nullCheck).append(CURSOR + ".getString(i);\n");
else if (isTypeOf(typeMirror, byte[].class))
stringBuilder.append(CURSOR + ".getBlob(i);\n");
else if (isTypeOf(typeMirror, Byte[].class))
stringBuilder.append(nullCheck).append(CURSOR + ".getBlob(i);\n");
else if (isTypeOf(typeMirror, Model.class))
stringBuilder.append("(" + typeMirror.toString() + ") ModelHelper.getModel(cursor, " + type + ", i);\n");
else if (isTypeOf(typeMirror, Enum.class))
stringBuilder.append("(" + typeMirror.toString() + ") ModelHelper.getEnum(cursor, " + type + ", i);\n");
else
stringBuilder.append(" null;\n");
if (notPrimitiveType) {
stringBuilder.append(" }\n");
}
}
return stringBuilder.toString();
}
private String getFillContentValuesCode(Set<VariableElement> columns) {
StringBuilder stringBuilder = new StringBuilder();
for (VariableElement column : columns) {
Column annotation = column.getAnnotation(Column.class);
String fieldName = annotation.name();
if (fieldName == null || fieldName.isEmpty())
fieldName = column.getSimpleName().toString();
TypeMirror typeMirror = column.asType();
boolean notPrimitiveType = typeMirror instanceof DeclaredType;
String type = typeMirror.toString() + ".class";
String getValue = MODEL + "." + column.getSimpleName();
if (notPrimitiveType) {
stringBuilder.append(" if (ModelHelper.isSerializable(" + type + ")) {\n");
stringBuilder.append(" ModelHelper.setSerializable(" + CONTENT_VALUES + ", " + type + ", " + getValue + ", \"" + fieldName + "\");\n");
stringBuilder.append(" } else if (" + getValue + " != null) {\n");
stringBuilder.append(" " + CONTENT_VALUES + ".");
} else {
stringBuilder.append(" " + CONTENT_VALUES + ".");
}
if (isTypeOf(typeMirror, Integer.class) || isTypeOf(typeMirror, int.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ");\n");
else if (isTypeOf(typeMirror, Byte.class) || isTypeOf(typeMirror, byte.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ");\n");
else if (isTypeOf(typeMirror, Short.class) || isTypeOf(typeMirror, short.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ");\n");
else if (isTypeOf(typeMirror, Long.class) || isTypeOf(typeMirror, long.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ");\n");
else if (isTypeOf(typeMirror, Float.class) || isTypeOf(typeMirror, float.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ");\n");
else if (isTypeOf(typeMirror, Double.class) || isTypeOf(typeMirror, double.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ");\n");
else if (isTypeOf(typeMirror, Boolean.class) || isTypeOf(typeMirror, boolean.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ");\n");
else if (isTypeOf(typeMirror, Character.class) || isTypeOf(typeMirror, char.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ".toString());\n");
else if (isTypeOf(typeMirror, String.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ".toString());\n");
else if (isTypeOf(typeMirror, Byte[].class) || isTypeOf(typeMirror, byte[].class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ");\n");
else if (isTypeOf(typeMirror, Model.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ".getId());\n");
else if (isTypeOf(typeMirror, Enum.class))
stringBuilder.append("put(\"" + fieldName + "\", " + getValue + ".name());\n");
else
stringBuilder.append("putNull(\"" + fieldName + "\");\n");
if (notPrimitiveType) {
if (annotation.defaultValue() == null || annotation.defaultValue().isEmpty()) {
stringBuilder.append(" } else {\n");
stringBuilder.append(" " + CONTENT_VALUES + ".putNull(\"" + fieldName + "\");\n");
}
stringBuilder.append(" }\n");
}
}
return stringBuilder.toString();
}
private boolean isTypeOf(TypeMirror typeMirror, Class<?> type) {
if (type.getName().equals(typeMirror.toString()))
return true;
if ((typeMirror.getKind() == TypeKind.ARRAY) && type.isArray())
return typeMirror.toString().equals(type.getComponentType() + "[]");
if (typeMirror instanceof DeclaredType == false)
return false;
DeclaredType declaredType = (DeclaredType) typeMirror;
Element element = declaredType.asElement();
if (element instanceof TypeElement == false)
return false;
TypeElement typeElement = (TypeElement) element;
TypeMirror superType = typeElement.getSuperclass();
if (isTypeOf(superType, type))
return true;
return false;
}
private boolean checkTableModifiers(TypeElement table) {
if (table.getModifiers().contains(Modifier.PRIVATE)) {
error("Classes marked with @Table cannot be private", table);
return false;
}
if (table.getKind() != ElementKind.CLASS) {
error("Only classes can be marked with @Table annotation", table);
return false;
}
return true;
}
private boolean checkColumnModifiers(VariableElement column) {
if (column.getModifiers().contains(Modifier.PRIVATE)) {
error("Field marked with @Column cannot be private", column);
return false;
}
if (column.getModifiers().contains(Modifier.FINAL)) {
error("Field marked with @Column cannot be final", column);
return false;
}
if (column.getModifiers().contains(Modifier.STATIC)) {
error("Field marked with @Column cannot be static", column);
return false;
}
return true;
}
private void error(String message, Element element) {
processingEnv.getMessager().printMessage(Kind.ERROR, message, element);
}
private static String getClassName(TypeElement type, String packageName) {
int packageLen = packageName.length() + 1;
return type.getQualifiedName().toString().substring(packageLen).replace('.', '$');
}
}