Skip to content

Instantly share code, notes, and snippets.

@Nithanim
Created February 3, 2017 21:55
Show Gist options
  • Save Nithanim/f74aa40dbe97b06523840b28ba6c6468 to your computer and use it in GitHub Desktop.
Save Nithanim/f74aa40dbe97b06523840b28ba6c6468 to your computer and use it in GitHub Desktop.
@MetaInfServices(JavacAnnotationHandler.class)
@HandlerPriority(1024)
public class DetermineListenerTypesProcessor extends JavacAnnotationHandler<DetermineListenerTypes> {
@Override
public void handle(AnnotationValues<DetermineListenerTypes> av, JCTree.JCAnnotation jca, JavacNode jn) {
JavacNode parent = jn.up();
JCTree.JCClassDecl classDecl = (JCClassDecl) parent.get();
for (JCTree tree : classDecl.defs) {
if (tree instanceof JCTree.JCMethodDecl) {
JCTree.JCMethodDecl methodDecl = (JCTree.JCMethodDecl) tree;
System.out.println(methodDecl);
JCTree.JCBlock body = methodDecl.getBody();
body.accept(new TreeScanner() {
@Override
public void visitApply(JCTree.JCMethodInvocation methdocall) {
if (methdocall.meth instanceof JCTree.JCFieldAccess) {
JCTree.JCFieldAccess m = (JCTree.JCFieldAccess) methdocall.meth;
//TODO check type!
if (m.name.contentEquals("registerListener")) {
List<JCTree.JCExpression> methodcallargs = methdocall.getArguments();
if (methodcallargs.size() == 1) {
JCTree.JCExpression arg0 = methodcallargs.get(0);
if (arg0 instanceof JCTree.JCLambda) {
JCTree.JCLambda lambda = (JCTree.JCLambda) arg0;
ListBuffer<JCTree.JCExpression> newargs = new ListBuffer<>();
newargs.add(lambda);
JCTree.JCFieldAccess arg1
= jn.getTreeMaker().Select(
lambda.params.get(0).vartype,
jn.toName("class")
);
newargs.add(arg1);
methdocall.args = newargs.toList();
}
}
}
}
super.visitApply(methdocall);
}
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment