package top.bluesword;
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.Context;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Names;
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.ArrayList;
import java.util.List;
import java.util.Set;
/**
* JCTree实战注解处理器
* @author hu_pf
**/
@SupportedSourceVersion(SourceVersion.RELEASE_11)
@SupportedAnnotationTypes("top.bluesword.CombatJCTree")
public class CombatJcTreeProcessor extends AbstractProcessor {
private Messager messager;
private JavacTrees javacTrees;
private TreeMaker treeMaker;
private Names names;
/**
* 1. Message 主要是用来在编译时期打log用的
* 2. JavacTrees 提供了待处理的抽象语法树
* 3. TreeMaker 封装了创建AST节点的一些方法
* 4. Names 提供了创建标识符的方法
*/
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
this.messager = processingEnv.getMessager();
messager.printMessage(Diagnostic.Kind.NOTE,"sword-test-build");
this.javacTrees = JavacTrees.instance(processingEnv);
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> elementsAnnotatedWith = roundEnv.getElementsAnnotatedWith(CombatJCTree.class);
for (Element e : elementsAnnotatedWith) {
JCTree tree = javacTrees.getTree(e);
tree.accept(new TreeTranslator() {
@Override
public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
List<JCTree> jcTrees = generateParameters();
jcTrees.forEach(jcTree -> {
messager.printMessage(Diagnostic.Kind.NOTE, jcTree.getTree().toString());
jcClassDecl.defs = jcClassDecl.defs.prepend(jcTree);
});
super.visitClassDef(jcClassDecl);
}
});
}
return true;
}
private List<JCTree> generateParameters(){
List<JCTree> jcTrees = new ArrayList<>();
// 生成参数 例如:private String age
JCTree.JCVariableDecl jcVariableDecl = treeMaker.VarDef(treeMaker.Modifiers(Flags.PRIVATE), names.fromString("age"), treeMaker.Ident(names.fromString("String")), null);
/*
生成方法 例如:public void getAge(){
String name = "BuXueWuShu";
return this.age;
}
*/
ListBuffer<JCTree.JCStatement> statements = new ListBuffer<>();
// 创建一个new语句 CombatJCTreeMain jcTreeMain = new CombatJCTreeMain();
JCTree.JCNewClass jcTreeMain = treeMaker.NewClass(
null,
// 泛型参数列表
com.sun.tools.javac.util.List.nil(),
// 创建的类名
treeMaker.Ident(names.fromString("CombatJCTreeMain")),
// 参数列表
com.sun.tools.javac.util.List.nil(),
null
);
JCTree.JCVariableDecl jcVariableDecl1 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("jcTreeMain"),
treeMaker.Ident(names.fromString("CombatJCTreeMain")),
jcTreeMain
);
statements.append(jcVariableDecl1);
// 创建一个方法调用 jcTreeMain.test()
// 定义发方法体 i++
statements.append(treeMaker.VarDef(treeMaker.Modifiers(Flags.PARAMETER),names.fromString("i"),treeMaker.Ident(names.fromString("Integer")),treeMaker.Literal(1)));
statements.append(treeMaker.Exec(treeMaker.Unary(JCTree.Tag.PREINC,treeMaker.Ident(names.fromString("i")))));
// 定义方法体 add = "a"+"b"
statements.append(treeMaker.VarDef(treeMaker.Modifiers(Flags.PARAMETER),names.fromString("add"),treeMaker.Ident(names.fromString("String")),null));
statements.append(treeMaker.Exec(treeMaker.Assign(treeMaker.Ident(names.fromString("add")),treeMaker.Binary(JCTree.Tag.PLUS,treeMaker.Literal("a"),treeMaker.Literal("b")))));
//add += "test"
statements.append(treeMaker.Exec(treeMaker.Assignop(JCTree.Tag.PLUS_ASG, treeMaker.Ident(names.fromString("add")), treeMaker.Literal("test"))));
// 定义方法体 String name = "BuXueWuShu"
statements.append(treeMaker.VarDef(treeMaker.Modifiers(Flags.PARAMETER),names.fromString("name"),treeMaker.Ident(names.fromString("String")),treeMaker.Literal("BuXueWuShu")));
/*
创建一个if语句
if("BuXueWuShu".equals(name)){
add = "a" + "b";
}else{
add += "test";
}
*/
// "BuXueWuShu".equals(name)
JCTree.JCMethodInvocation apply = treeMaker.Apply(
com.sun.tools.javac.util.List.nil(),
treeMaker.Select(
// . 左边的内容
treeMaker.Literal("BuXueWuShu"),
// . 右边的内容
names.fromString("equals")
),
com.sun.tools.javac.util.List.of(treeMaker.Ident(names.fromString("name")))
);
// add = "a" + "b"
JCTree.JCExpressionStatement exec3 = treeMaker.Exec(treeMaker.Assign(treeMaker.Ident(names.fromString("add")), treeMaker.Binary(JCTree.Tag.PLUS, treeMaker.Literal("a"), treeMaker.Literal("b"))));
// add += "test"
JCTree.JCExpressionStatement exec1 = treeMaker.Exec(treeMaker.Assignop(JCTree.Tag.PLUS_ASG, treeMaker.Ident(names.fromString("add")), treeMaker.Literal("test")));
JCTree.JCIf anIf = treeMaker.If(
// if语句里面的判断语句
apply,
// 条件成立的语句
exec3,
// 条件不成立的语句
exec1
);
statements.append(anIf);
// 定义方法体 return this.age
statements.append(treeMaker.Return(treeMaker.Select(treeMaker.Ident(names.fromString("this")),names.fromString("age"))));
JCTree.JCBlock body = treeMaker.Block(0, statements.toList());
// 组成方法,第一个参数意思是public,第二个参数是方法名getAge,第三个参数是方法返回类型String
JCTree.JCMethodDecl jcMethodDecl = treeMaker.MethodDef(treeMaker.Modifiers(Flags.PUBLIC), names.fromString("getAge"), treeMaker.Ident(names.fromString("String")), com.sun.tools.javac.util.List.nil(), com.sun.tools.javac.util.List.nil(), com.sun.tools.javac.util.List.nil(), body, null);
/*
无参无返回值的方法生成
public void test(){
}
*/
// 定义方法体
ListBuffer<JCTree.JCStatement> testStatement = new ListBuffer<>();
JCTree.JCBlock testBody = treeMaker.Block(0, testStatement.toList());
JCTree.JCMethodDecl test = treeMaker.MethodDef(
// 方法限定值
treeMaker.Modifiers(Flags.PUBLIC),
// 方法名
names.fromString("test"),
// 返回类型
treeMaker.Type(new Type.JCVoidType()),
com.sun.to
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是一个综合性的代码实验室设计源码,以Java为核心,涵盖多种编程语言,包括Java、JavaScript、HTML、Go、Vue、Shell、Python、TypeScript、CSS、C和微信小程序。源码库中共有1276个文件,其中Java源文件占比最大,达729个,其他文件类型包括XML、JSON、属性文件、JavaScript、HTML等。该实验室旨在提供一个多语言编程环境,便于学习和实践不同编程语言。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java为主的多语言代码实验室设计源码 (1283个子文件)
rocket.base64 6KB
rocket.base64 6KB
清理并生成实体.bat 84B
build.bat 72B
start.bat 52B
build.bat 33B
start.bat 30B
start.bat 23B
bluesword.bpmn 4KB
add.c 132B
c.c 63B
app.conf 63B
index.css 828B
index.css 131B
spring.factories 115B
test.ftlh 371B
StaticModels.ftlh 191B
.gitignore 363B
.gitignore 224B
.gitignore 224B
.gitignore 45B
.gitignore 44B
.gitignore 27B
.gitignore 19B
.gitignore 18B
.gitignore 17B
.gitignore 11B
.gitignore 8B
.gitignore 7B
.gitignore 7B
.gitignore 7B
file-go.go 1KB
Cli.go 895B
main.go 616B
GoLibrary.go 347B
auto-close.go 324B
ioutil.go 315B
strings.go 267B
GetTest.go 245B
default.go 243B
add.go 240B
hmac.go 220B
interfaces.go 201B
router.go 150B
hex.go 149B
fmt.go 144B
add2.go 113B
main.go 106B
time.go 99B
FormatInt.go 99B
rename.go 98B
main.go 73B
build.gradle 1003B
build.gradle 1003B
build.gradle 792B
build.gradle 792B
build.gradle 463B
settings.gradle 17B
settings.gradle 17B
Inspiration.groovy 310B
JsonSlurperTest.groovy 295B
GroovyMainTest.groovy 193B
GroovyMainTest.groovy 131B
test.groovy 112B
property.groovy 33B
HELP 72B
index.hml 114B
index.hml 114B
index.html 69KB
持有基金2021-09-17报告.html 10KB
环形进度条.html 3KB
index.html 3KB
番茄钟.html 2KB
关系图实例.html 2KB
index.html 1KB
折线图实例.html 1KB
index.html 1KB
倒计时.html 1KB
力导向图.html 1KB
sample.html 1KB
柱状图实例.html 1KB
excelTemplate.html 966B
折线图.html 838B
flexbox-wrap.html 691B
index.html 641B
flexbox-dynamic.html 615B
Json编辑器.html 595B
flexbox-column.html 553B
flexbox-base.html 552B
page.html 474B
es6.html 364B
index.html 340B
index.html 293B
index.html 289B
index.html 278B
index.html 278B
test1.html 256B
index.html 252B
test.html 196B
static.html 183B
共 1283 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
csbysj2020
- 粉丝: 2470
- 资源: 5462
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功