1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| package com.jimo;
import com.google.auto.service.AutoService; import com.sun.source.tree.Tree; import com.sun.tools.javac.api.JavacTrees; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.code.Type; import com.sun.tools.javac.processing.JavacProcessingEnvironment; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.tree.TreeTranslator; import com.sun.tools.javac.util.*;
import javax.annotation.processing.*; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic; import java.util.Map; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors;
@SupportedAnnotationTypes(value = {"com.jimo.Data"}) @SupportedSourceVersion(SourceVersion.RELEASE_8) @AutoService(Processor.class) public class CustomProcessor extends AbstractProcessor {
private JavacTrees trees;
private TreeMaker treeMaker;
private Names names;
private Messager messager;
@Override public synchronized void init(ProcessingEnvironment processingEnv) { super.init(processingEnv); trees = JavacTrees.instance(processingEnv); final Context context = ((JavacProcessingEnvironment) processingEnv).getContext(); treeMaker = TreeMaker.instance(context); names = Names.instance(context); messager = processingEnv.getMessager(); }
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { System.out.println("begin");
final Set<? extends Element> dataAnnotations = roundEnv.getElementsAnnotatedWith(Data.class);
dataAnnotations.stream().map(element -> trees.getTree(element)).forEach( tree -> tree.accept(new TreeTranslator() { @Override public void visitMethodDef(JCTree.JCMethodDecl jcMethodDecl) { System.out.println("-------------2"); messager.printMessage(Diagnostic.Kind.NOTE, jcMethodDecl.toString()); super.visitMethodDef(jcMethodDecl); }
@Override public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
System.out.println("-------------1"); final Map<Name, JCTree.JCVariableDecl> treeMap = jcClassDecl.defs.stream().filter(k -> k.getKind().equals(Tree.Kind.VARIABLE)) .map(tree -> (JCTree.JCVariableDecl) tree) .collect(Collectors.toMap(JCTree.JCVariableDecl::getName, Function.identity()));
treeMap.forEach((k, var) -> { messager.printMessage(Diagnostic.Kind.NOTE, "var:" + k); System.out.println("-------------3"); try { jcClassDecl.defs = jcClassDecl.defs.prepend(getter(var)); jcClassDecl.defs = jcClassDecl.defs.prepend(setter(var));
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage()); } });
super.visitClassDef(jcClassDecl); } }) ); return true; }
private JCTree setter(JCTree.JCVariableDecl var) throws ClassNotFoundException, IllegalAccessException, InstantiationException { final JCTree.JCModifiers modifiers = treeMaker.Modifiers(Flags.PUBLIC);
final Name varName = var.getName(); Name methodName = methodName(varName, "set");
ListBuffer<JCTree.JCStatement> jcStatements = new ListBuffer<>(); jcStatements.append(treeMaker.Exec(treeMaker.Assign( treeMaker.Select(treeMaker.Ident(names.fromString("this")), varName), treeMaker.Ident(varName) ))); final JCTree.JCBlock block = treeMaker.Block(0, jcStatements.toList());
JCTree.JCExpression returnType = treeMaker.Type((Type) (Class.forName("com.sun.tools.javac.code.Type$JCVoidType").newInstance()));
List<JCTree.JCTypeParameter> typeParameters = List.nil();
final JCTree.JCVariableDecl paramVars = treeMaker.VarDef(treeMaker.Modifiers(Flags.PARAMETER, List.nil()), var.name, var.vartype, null); final List<JCTree.JCVariableDecl> params = List.of(paramVars);
List<JCTree.JCExpression> throwClauses = List.nil(); return treeMaker.MethodDef(modifiers, methodName, returnType, typeParameters, params, throwClauses, block, null); }
private Name methodName(Name varName, String prefix) { return names.fromString(prefix + varName.toString().substring(0, 1).toUpperCase() + varName.toString().substring(1)); }
private JCTree getter(JCTree.JCVariableDecl var) { final JCTree.JCModifiers modifiers = treeMaker.Modifiers(Flags.PUBLIC);
final Name methodName = methodName(var.getName(), "get");
ListBuffer<JCTree.JCStatement> statements = new ListBuffer<>(); statements.append(treeMaker.Return(treeMaker.Select(treeMaker.Ident(names.fromString("this")), var.getName()))); final JCTree.JCBlock block = treeMaker.Block(0, statements.toList());
final JCTree.JCExpression returnType = var.vartype;
List<JCTree.JCTypeParameter> typeParameters = List.nil();
List<JCTree.JCVariableDecl> params = List.nil();
List<JCTree.JCExpression> throwClauses = List.nil();
return treeMaker.MethodDef(modifiers, methodName, returnType, typeParameters, params, throwClauses, block, null); } }
|