Skip to content

Instantly share code, notes, and snippets.

@wreulicke
Forked from anonymous/Test.java
Last active December 21, 2016 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wreulicke/39360163ca9f1fbf64637bcba549d15d to your computer and use it in GitHub Desktop.
Save wreulicke/39360163ca9f1fbf64637bcba549d15d to your computer and use it in GitHub Desktop.
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);
}
}
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);
}
}
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