Created
May 15, 2015 05:27
-
-
Save jgnt32/e84c0a61d090ac0ecf55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AutoService(Processor.class) | |
public class RalmKitAnnotationProcessor extends AbstractProcessor { | |
private Types typeUtils; | |
private Elements elementUtils; | |
private Filer filer; | |
private Messager messager; | |
@Override | |
public synchronized void init(ProcessingEnvironment processingEnv) { | |
super.init(processingEnv); | |
typeUtils = processingEnv.getTypeUtils(); | |
elementUtils = processingEnv.getElementUtils(); | |
filer = processingEnv.getFiler(); | |
messager = processingEnv.getMessager(); | |
} | |
@Override | |
public Set<String> getSupportedAnnotationTypes() { | |
Set<String> annotataions = new LinkedHashSet<String>(); | |
annotataions.add(RealmKitObject.class.getCanonicalName()); | |
return annotataions; | |
} | |
@Override | |
public SourceVersion getSupportedSourceVersion() { | |
return SourceVersion.latestSupported(); | |
} | |
@Override | |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | |
for (Element e : roundEnv.getElementsAnnotatedWith(RealmKitObject.class)) { | |
try { | |
RealmKitObjectGenerator generator = new RealmKitObjectGenerator(e); | |
generator.writeTo(filer); | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RealmKitObjectGenerator { | |
private JavaFile javaFile; | |
public RealmKitObjectGenerator(Element element) { | |
String kitName = element.getSimpleName().toString(); | |
MethodSpec createOrUpdate = MethodSpec.methodBuilder("createOrUpdate") | |
.addModifiers(Modifier.PUBLIC) | |
.returns(void.class) | |
// .addParameter(type, kitName.toLowerCase()) | |
.addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet! " + kitName) | |
.build(); | |
TypeSpec realmKit = TypeSpec.classBuilder(kitName + "Kit") | |
.addModifiers(Modifier.PUBLIC) | |
.addMethod(createOrUpdate) | |
.build(); | |
javaFile = JavaFile.builder("com.timewastingguru.customannotations", realmKit) | |
.build(); | |
} | |
public void writeTo(Filer filer) throws IOException { | |
javaFile.writeTo(filer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment