|
| 1 | +package com.quarkworks.android.realmtypesafequery; |
| 2 | + |
| 3 | +import com.quarkworks.android.realmtypesafequery.annotations.GenerateRealmStringFields; |
| 4 | +import com.quarkworks.android.realmtypesafequery.annotations.GenerateRealmFields; |
| 5 | +import com.squareup.javapoet.CodeBlock; |
| 6 | +import com.squareup.javapoet.FieldSpec; |
| 7 | +import com.squareup.javapoet.JavaFile; |
| 8 | +import com.squareup.javapoet.TypeSpec; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.lang.reflect.Type; |
| 12 | +import java.util.LinkedHashSet; |
| 13 | +import java.util.LinkedList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Set; |
| 16 | + |
| 17 | +import javax.annotation.processing.AbstractProcessor; |
| 18 | +import javax.annotation.processing.RoundEnvironment; |
| 19 | +import javax.lang.model.element.Element; |
| 20 | +import javax.lang.model.element.Modifier; |
| 21 | +import javax.lang.model.element.TypeElement; |
| 22 | +import javax.lang.model.element.VariableElement; |
| 23 | +import javax.lang.model.util.ElementFilter; |
| 24 | +import javax.tools.Diagnostic; |
| 25 | + |
| 26 | +import io.realm.annotations.Ignore; |
| 27 | + |
| 28 | +//@AutoService(Processor.class) this causes some compiler error so we are now doing this by hand |
| 29 | +public class AnnotationProcessor extends AbstractProcessor { |
| 30 | + |
| 31 | + @Override |
| 32 | + public Set<String> getSupportedAnnotationTypes() { |
| 33 | + Set<String> set = new LinkedHashSet<>(); |
| 34 | + set.add(GenerateRealmStringFields.class.getCanonicalName()); |
| 35 | + set.add(GenerateRealmFields.class.getCanonicalName()); |
| 36 | + |
| 37 | + return set; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public synchronized boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 42 | + for (Element element : roundEnv.getElementsAnnotatedWith(GenerateRealmStringFields.class)) { |
| 43 | + if (!(element instanceof TypeElement)) continue; |
| 44 | + |
| 45 | + TypeElement typeElement = (TypeElement) element; |
| 46 | + List<VariableElement> variableElements = ElementFilter.fieldsIn(typeElement.getEnclosedElements()); |
| 47 | + List<FieldSpec> fieldSpecs = new LinkedList<>(); |
| 48 | + |
| 49 | + for (VariableElement variableElement : variableElements) { |
| 50 | + |
| 51 | + boolean isIgnored = variableElement.getAnnotation(Ignore.class) != null; |
| 52 | + if (isIgnored) continue; |
| 53 | + |
| 54 | + Type type = String.class; |
| 55 | + |
| 56 | + |
| 57 | + String name = variableElement.getSimpleName().toString(); |
| 58 | + name = name.replaceAll("([A-Z])", "_$1").toUpperCase(); |
| 59 | + Modifier[] modifiers = {Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL}; |
| 60 | + CodeBlock codeBlock = CodeBlock.of("$S", variableElement.getSimpleName()); |
| 61 | + |
| 62 | + FieldSpec fieldSpec = FieldSpec.builder(type, name, modifiers) |
| 63 | + .initializer(codeBlock) |
| 64 | + .build(); |
| 65 | + |
| 66 | + fieldSpecs.add(fieldSpec); |
| 67 | + } |
| 68 | + |
| 69 | + String packageName = "com.quarkworks.android.realmtypesafequery.generated"; |
| 70 | + String className = typeElement.getSimpleName() + "StringFields"; |
| 71 | + |
| 72 | + TypeSpec typeSpec = TypeSpec.classBuilder(className) |
| 73 | + .addFields(fieldSpecs) |
| 74 | + .addModifiers(Modifier.PUBLIC) |
| 75 | + .build(); |
| 76 | + |
| 77 | + JavaFile javaFile = JavaFile.builder(packageName, typeSpec).build(); |
| 78 | + |
| 79 | + try { |
| 80 | + javaFile.writeTo(this.processingEnv.getFiler()); |
| 81 | + } catch (IOException e) { |
| 82 | + this.reportError(element, e.toString()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + private void reportError(Element element, CharSequence message) { |
| 90 | + this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element); |
| 91 | + } |
| 92 | + |
| 93 | + private void reportWarning(Element element, CharSequence message) { |
| 94 | + this.processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, message, element); |
| 95 | + } |
| 96 | + |
| 97 | + private void log(CharSequence message) { |
| 98 | + this.processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, message); |
| 99 | + } |
| 100 | +} |
0 commit comments