package com.allens.netty;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.model.JavacElements;
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.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Names;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
*
* 参考:
* - https://blog.csdn.net/a_zhenzhen/article/details/86065063#TreeMaker.Block
* - https://zhuanlan.zhihu.com/p/121031315
* - https://www.cnblogs.com/kanyun/p/11541826.html
* - http://www.javaet.com/blog/136.htm
* - https://blog.csdn.net/A_zhenzhen/article/details/86065063
*
* 编译器修改的语法树,Intellij idea识别不出来,会飘红
* https://stackoverflow.com/questions/15357362/can-i-modify-the-ast-before-the-java-compiler-compiling-the-ast-to-class-file
* https://stackoverflow.com/questions/48605327/intellij-cannot-recognize-classes-modified-by-annotation-processor-in-target-cla
*
* @author yahui.lv lvyahui8@gmail.com
* @date 2020/4/8 21:05
*/
public class SingletonProcessor extends AbstractProcessor {
private Messager messager;
private JavacElements elementUtils;
private TreeMaker treeMaker;
private Names names;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
messager = processingEnv.getMessager();
this.elementUtils = (JavacElements) processingEnv.getElementUtils();
Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
this.treeMaker = TreeMaker.instance(context);
this.names = Names.instance(context);
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Singleton.class);
messager.printMessage(Diagnostic.Kind.NOTE,"process singleton, size:" + elements.size());
if (! elements.isEmpty()) {
for (Element element : elements) {
try {
JCTree.JCClassDecl classDecl = (JCTree.JCClassDecl) elementUtils.getTree(element);
String className = element.getSimpleName().toString();
String staticFieldName = "instance";
messager.printMessage(Diagnostic.Kind.NOTE,"singleton name:" + className);
JCTree.JCNewClass jcNewClass = treeMaker.NewClass(null,
/* 暂时不支持类型参数 */
List.nil(),
treeMaker.Ident(names.fromString(className)),
/* 暂时部支持构造函数入参 */
List.nil(),
null
);
classDecl.defs = classDecl.defs.append(treeMaker.VarDef(treeMaker.Modifiers(Flags.STATIC + Flags.PRIVATE),
names.fromString(staticFieldName),treeMaker.Ident(names.fromString(className)),jcNewClass));
ListBuffer<JCTree.JCStatement> methodBlockStatements = new ListBuffer<>();
JCTree.JCReturn jcReturn = treeMaker.Return(treeMaker.Ident(names.fromString(staticFieldName)));
methodBlockStatements.add(jcReturn);
JCTree.JCBlock methodBlock = treeMaker.Block(0,methodBlockStatements.toList());
classDecl.defs = classDecl.defs.append(treeMaker.MethodDef(treeMaker.Modifiers(Flags.PUBLIC + Flags.STATIC),
names.fromString("getInstance"),
treeMaker.Ident(names.fromString(className)),
List.nil(),
List.nil(),
List.nil(),
methodBlock,
null
));
} catch (Exception e) {
messager.printMessage(Diagnostic.Kind.ERROR,"singleton handle error: " + e.getMessage(),element);
}
}
}
return false;
}
@Override
public Set<String> getSupportedAnnotationTypes() {
return new HashSet<>(Collections.singletonList(Singleton.class.getCanonicalName()));
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
lombok 实现原理(代码)
共40个文件
class:10个
java:9个
lst:5个
需积分: 2 1 下载量 8 浏览量
2023-04-21
09:18:18
上传
评论
收藏 88KB ZIP 举报
温馨提示
可以参考:https://blog.csdn.net/yh4494/article/details/126871988 这篇文章进行学习交流。
资源推荐
资源详情
资源评论
收起资源包目录
allens-library.zip (40个子文件)
allens-library
allens-library
allens-camunda
target
classes
pom.xml 1KB
allens-annotation-test
pom.xml 1KB
src
test
java
main
resources
java
com
allens
annotation
test
Test.java 600B
target
classes
com
allens
annotation
test
Test.class 907B
maven-status
maven-compiler-plugin
compile
default-compile
createdFiles.lst 0B
inputFiles.lst 123B
generated-sources
annotations
allens-annotation-test.iml 3KB
allens-library.iml 878B
allens-annotation
pom.xml 3KB
src
test
java
main
resources
META-INF
services
javax.annotation.processing.Processor 27B
java
com
allens
netty
EchoClient.java 58B
LogProcess.java 4KB
AllensLog.java 197B
Singleton.java 237B
SingletonProcessor.java 5KB
ModuleLoggerAutoGeneration.java 306B
allens-annotation.iml 4KB
target
classes
META-INF
services
com.allens.netty.LogProcess 28B
com
allens
netty
AllensLog.class 488B
SingletonProcessor.class 8KB
Singleton.class 424B
LogProcess.class 8KB
EchoClient.class 289B
ModuleLoggerAutoGeneration.class 519B
LogProcess$1.class 2KB
allens-annotation-1.0-SNAPSHOT.jar 11KB
maven-status
maven-compiler-plugin
compile
default-compile
createdFiles.lst 308B
inputFiles.lst 706B
testCompile
default-testCompile
inputFiles.lst 0B
maven-archiver
pom.properties 74B
generated-sources
annotations
allens-camunda
pom.xml 2KB
src
test
java
TestSome.java 1KB
main
resources
bpmn
approve-payment.dmn 2KB
payment.bpmn 7KB
java
com
allens
camunda
ChargeCardWorker.java 2KB
target
classes
bpmn
approve-payment.dmn 2KB
payment.bpmn 7KB
com
allens
camunda
ChargeCardWorker.class 4KB
test-classes
TestSome.class 422B
generated-test-sources
test-annotations
generated-sources
annotations
allens-camunda.iml 4KB
共 40 条
- 1
资源评论
澄风
- 粉丝: 9360
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功