-
-
Save wreulicke/39360163ca9f1fbf64637bcba549d15d to your computer and use it in GitHub Desktop.
This file contains 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 Test{ | |
@interface var{} | |
public Object a = new function().get(); | |
class function implements java.util.function.Supplier<Integer>{ | |
@Override | |
public Integer get(){ | |
return 2; | |
} | |
} | |
public static void main(String[] args){ | |
System.out.println(new Test().a); | |
} | |
} |
This file contains 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 Test{ | |
@interface var{} | |
public var a = function(); | |
class function implements java.util.function.Supplier<Integer>{ | |
@Override | |
public Integer get(){ | |
return 2; | |
} | |
} | |
public static void main(String[] args){ | |
System.out.println(new Test().a); | |
} | |
} |
This file contains 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
import java.util.Set; | |
import javax.annotation.processing.AbstractProcessor; | |
import javax.annotation.processing.Processor; | |
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.TypeElement; | |
import com.google.auto.service.AutoService; | |
import com.sun.source.tree.MethodInvocationTree; | |
import com.sun.source.tree.VariableTree; | |
import com.sun.source.util.TreePath; | |
import com.sun.source.util.TreeScanner; | |
import com.sun.source.util.Trees; | |
import com.sun.tools.javac.model.JavacElements; | |
import com.sun.tools.javac.processing.JavacProcessingEnvironment; | |
import com.sun.tools.javac.tree.JCTree.JCExpression; | |
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; | |
import com.sun.tools.javac.tree.JCTree.JCNewClass; | |
import com.sun.tools.javac.tree.JCTree.JCVariableDecl; | |
import com.sun.tools.javac.tree.TreeMaker; | |
import com.sun.tools.javac.util.List; | |
@AutoService(Processor.class) | |
@SupportedSourceVersion(SourceVersion.RELEASE_8) | |
@SupportedAnnotationTypes("*") | |
public class TestProcessor extends AbstractProcessor { | |
@Override | |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | |
JavacProcessingEnvironment environment = (JavacProcessingEnvironment) processingEnv; | |
Trees trees = Trees.instance(environment); | |
roundEnv.getRootElements().stream().map(trees::getPath).map(TreePath::getCompilationUnit).forEach((t) -> { | |
t.accept(new Visitor(environment), null); | |
}); | |
return false; | |
} | |
private class Visitor extends TreeScanner<Void, Void> { | |
public Visitor(JavacProcessingEnvironment environment) { | |
super(); | |
maker = TreeMaker.instance(environment.getContext()); | |
elements = JavacElements.instance(environment.getContext()); | |
} | |
TreeMaker maker; | |
JavacElements elements; | |
@Override | |
public Void visitVariable(VariableTree node, Void p) { | |
System.out.println(node); | |
if (node instanceof JCVariableDecl) { | |
JCVariableDecl declaration = (JCVariableDecl) node; | |
System.out.println(declaration); | |
System.out.println(declaration.vartype); | |
System.out.println(declaration.getClass()); | |
JCExpression expression = declaration.getInitializer(); | |
if (expression != null) { | |
expression.getTree().accept(new TreeScanner<Void, Void>() { | |
public Void visitMethodInvocation(MethodInvocationTree methodInvocationTree, Void p2) { | |
JCMethodInvocation methodInvocation = (JCMethodInvocation) methodInvocationTree; | |
System.out.println(methodInvocation.getMethodSelect().toString()); | |
JCNewClass class1 = maker.NewClass(null, (List.<JCExpression> nil()), | |
maker.Ident(elements.getName(methodInvocation.getMethodSelect().toString())), | |
List.<JCExpression> nil(), null); | |
JCMethodInvocation invocation = maker.Apply(List.nil(), | |
maker.Select(class1, elements.getName("get")), List.nil()); | |
System.out.println(invocation); | |
declaration.init = invocation; | |
System.out.println("methodInvocationTree2"); | |
return null; | |
}; | |
}, null); | |
} | |
if (declaration.vartype.toString().equals("var")) { | |
JCExpression id = maker.Ident(elements.getName("java")); | |
id = maker.Select(id, elements.getName("lang")); | |
id = maker.Select(id, elements.getName("Object")); | |
System.out.println(id); | |
declaration.vartype = maker.Ident(elements.getTypeElement("java.lang.Object")); | |
} | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment