|
1 | 1 | package org.springframework.lock.processor; |
2 | 2 |
|
3 | 3 | import com.google.auto.service.AutoService; |
4 | | -import com.sun.tools.javac.model.JavacElements; |
| 4 | + |
| 5 | +import com.sun.source.tree.Tree; |
| 6 | +import com.sun.tools.javac.api.JavacTrees; |
| 7 | +import com.sun.tools.javac.code.Flags; |
| 8 | +import com.sun.tools.javac.code.Type; |
5 | 9 | import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
6 | 10 | import com.sun.tools.javac.tree.JCTree; |
7 | 11 | import com.sun.tools.javac.tree.TreeMaker; |
8 | | -import com.sun.tools.javac.util.Context; |
9 | | -import org.springframework.lock.annotation.ReadLock; |
| 12 | +import com.sun.tools.javac.tree.TreeTranslator; |
| 13 | +import com.sun.tools.javac.util.*; |
10 | 14 |
|
11 | 15 | import javax.annotation.processing.*; |
12 | 16 | import javax.lang.model.SourceVersion; |
13 | 17 | import javax.lang.model.element.Element; |
| 18 | +import javax.lang.model.element.ExecutableElement; |
14 | 19 | import javax.lang.model.element.Name; |
15 | 20 | import javax.lang.model.element.TypeElement; |
16 | | -import javax.lang.model.type.TypeMirror; |
17 | | -import javax.lang.model.util.Elements; |
18 | 21 | import javax.tools.Diagnostic; |
19 | | -import java.util.ArrayList; |
| 22 | +import java.util.HashSet; |
20 | 23 | import java.util.Set; |
| 24 | +import java.util.concurrent.locks.ReentrantReadWriteLock; |
21 | 25 |
|
| 26 | +import static com.sun.tools.javac.tree.JCTree.*; |
| 27 | +import static com.sun.tools.javac.util.List.nil; |
22 | 28 |
|
23 | 29 | @SupportedAnnotationTypes("org.springframework.lock.annotation.ReadLock") |
24 | 30 | @SupportedSourceVersion(SourceVersion.RELEASE_8) |
25 | 31 | @AutoService(Processor.class) |
26 | 32 | public class ReadLockProcessor extends AbstractProcessor { |
| 33 | + |
| 34 | + private Messager messager; // 编译时期输入日志的 |
| 35 | + private JavacTrees javacTrees; // 提供了待处理的抽象语法树 |
| 36 | + private TreeMaker treeMaker; // 封装了创建AST节点的一些方法 |
| 37 | + private Names names; // 提供了创建标识符的方法 |
| 38 | + |
| 39 | + @Override |
| 40 | + public synchronized void init(ProcessingEnvironment processingEnv) { |
| 41 | + super.init(processingEnv); |
| 42 | + this.messager = processingEnv.getMessager(); |
| 43 | + this.javacTrees = JavacTrees.instance(processingEnv); |
| 44 | + Context context = ((JavacProcessingEnvironment) processingEnv).getContext(); |
| 45 | + this.treeMaker = TreeMaker.instance(context); |
| 46 | + this.names = Names.instance(context); |
| 47 | + } |
| 48 | + |
27 | 49 | @Override |
28 | 50 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
29 | | - Messager messager = this.processingEnv.getMessager(); |
30 | 51 | for (TypeElement annotation : annotations) { |
31 | 52 | Name qualifiedName = annotation.getQualifiedName(); |
32 | | - messager.printMessage(Diagnostic.Kind.NOTE,"正在处理注解" + qualifiedName); |
33 | | - // TODO 处理注解 |
| 53 | + Set<TypeElement> cls = new HashSet<TypeElement>(); |
34 | 54 | Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(annotation); |
| 55 | + // 找到都有哪些类里面的方法用到了这个注解 |
35 | 56 | for (Element element : elements) { |
36 | | - // TODO 处理被注解的方法 |
37 | | - messager.printMessage(Diagnostic.Kind.NOTE,"-----------" + element.getSimpleName()); |
| 57 | + ExecutableElement method = (ExecutableElement) element; |
| 58 | + TypeElement clz = (TypeElement) method.getEnclosingElement(); |
| 59 | + messager.printMessage(Diagnostic.Kind.NOTE, "发现需要包含注解" + annotation.getQualifiedName() + "的类" + clz.getQualifiedName()); |
| 60 | + cls.add(clz); |
| 61 | + } |
| 62 | + for (TypeElement clz : cls) { |
| 63 | + JCTree tree = javacTrees.getTree(clz); |
| 64 | + tree.accept(new TreeTranslator() { |
| 65 | + @Override |
| 66 | + public void visitClassDef(JCTree.JCClassDecl jcClassDecl) { |
| 67 | + // 在抽象树中找出所有的变量 |
| 68 | + boolean foundLock = false; |
| 69 | + for (JCTree jcTree : jcClassDecl.defs) { |
| 70 | + if (jcTree.getKind().equals(Tree.Kind.VARIABLE)) { |
| 71 | + JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) jcTree; |
| 72 | + if ("java.util.concurrent.locks.ReadWriteLock".equalsIgnoreCase(var.vartype.type.toString()) || "java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock".equals(var.vartype.type.toString())) { |
| 73 | + // 找到了类中的读锁,不修改语法树 |
| 74 | + messager.printMessage(Diagnostic.Kind.NOTE, "已发现" + clz.getQualifiedName() + "类中的读锁" + var.name); |
| 75 | + foundLock = true; |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + if (!foundLock){ |
| 81 | + messager.printMessage(Diagnostic.Kind.NOTE, "将为类" + clz.getQualifiedName() + "动态生成读锁"); |
| 82 | + // TODO 修改语法树 |
| 83 | + JCVariableDecl lock = makeReadWriteLock(); |
| 84 | + jcClassDecl.defs = jcClassDecl.defs.prepend(lock); |
| 85 | + } |
| 86 | + super.visitClassDef(jcClassDecl); |
| 87 | + } |
| 88 | + }); |
38 | 89 | } |
39 | | - messager.printMessage(Diagnostic.Kind.NOTE,"处理注解完毕" + qualifiedName); |
40 | 90 | break; |
41 | 91 | } |
42 | 92 | return Boolean.TRUE; |
43 | 93 | } |
| 94 | + |
| 95 | + private JCVariableDecl makeReadWriteLock(){ |
| 96 | + JCModifiers modifiers = this.treeMaker.Modifiers(Flags.PRIVATE); |
| 97 | + JCVariableDecl var = this.treeMaker.VarDef( |
| 98 | + modifiers, |
| 99 | + this.names.fromString("lock"), |
| 100 | + this.memberAccess("java.util.concurrent.locks.ReentrantReadWriteLock"), |
| 101 | + this.treeMaker.NewClass(null, nil(), treeMaker.Ident(names.fromString("ReentrantReadWriteLock")), nil(), null) |
| 102 | + ); |
| 103 | + return var; |
| 104 | + } |
| 105 | + |
| 106 | + private JCExpression memberAccess(String components) { |
| 107 | + String[] componentArray = components.split("\\."); |
| 108 | + JCTree.JCExpression expr = treeMaker.Ident(this.names.fromString(componentArray[0])); |
| 109 | + for (int i = 1; i < componentArray.length; i++) { |
| 110 | + expr = treeMaker.Select(expr, this.names.fromString(componentArray[i])); |
| 111 | + } |
| 112 | + return expr; |
| 113 | + } |
44 | 114 | } |
0 commit comments