Skip to content

Commit 6121318

Browse files
committed
java/se: add annotation processor sample
1 parent c40c62b commit 6121318

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+562
-296
lines changed
Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,62 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3-
<parent>
4-
<artifactId>javase</artifactId>
5-
<groupId>cn.edu.ntu.java</groupId>
6-
<version>1.0-SNAPSHOT</version>
7-
</parent>
8-
<modelVersion>4.0.0</modelVersion>
9-
<artifactId>javase-agent</artifactId>
10-
<build>
11-
<plugins>
12-
<plugin>
13-
<artifactId>maven-jar-plugin</artifactId>
14-
<version>3.1.2</version>
15-
<configuration>
16-
<archive>
17-
<manifestEntries>
18-
<project-name>${project.name}</project-name>
19-
<project-version>${project.version}</project-version>
20-
<Premain-Class>cn.edu.ntu.javase.agent.IntegrationAgent</Premain-Class>
21-
<Can-Redefine-Classes>true</Can-Redefine-Classes>
22-
<Can-Retransform-Classes>true</Can-Retransform-Classes>
23-
<Boot-Class-Path>javassist-3.27.0-GA.jar</Boot-Class-Path>
24-
</manifestEntries>
25-
</archive>
26-
<skip>true</skip>
27-
</configuration>
28-
</plugin>
29-
<plugin>
30-
<artifactId>maven-shade-plugin</artifactId>
31-
<executions>
32-
<execution>
33-
<phase>package</phase>
34-
<goals>
35-
<goal>shade</goal>
36-
</goals>
37-
<configuration>
38-
<transformers>
39-
<transformer />
40-
</transformers>
41-
</configuration>
42-
</execution>
43-
</executions>
44-
</plugin>
45-
</plugins>
46-
</build>
47-
<dependencies>
48-
<dependency>
49-
<groupId>org.eclipse.jetty.aggregate</groupId>
50-
<artifactId>jetty-all</artifactId>
51-
<version>9.2.11.v20150529</version>
52-
<scope>test</scope>
53-
<exclusions>
54-
<exclusion>
55-
<artifactId>javax.websocket-api</artifactId>
56-
<groupId>javax.websocket</groupId>
57-
</exclusion>
58-
</exclusions>
59-
</dependency>
60-
</dependencies>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<parent>
5+
<artifactId>javase</artifactId>
6+
<groupId>cn.edu.ntu.java</groupId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
<modelVersion>4.0.0</modelVersion>
10+
<artifactId>javase-agent</artifactId>
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<artifactId>maven-jar-plugin</artifactId>
15+
<version>3.1.2</version>
16+
<configuration>
17+
<archive>
18+
<manifestEntries>
19+
<project-name>${project.name}</project-name>
20+
<project-version>${project.version}</project-version>
21+
<Premain-Class>cn.edu.ntu.javase.agent.IntegrationAgent</Premain-Class>
22+
<Can-Redefine-Classes>true</Can-Redefine-Classes>
23+
<Can-Retransform-Classes>true</Can-Retransform-Classes>
24+
<Boot-Class-Path>javassist-3.27.0-GA.jar</Boot-Class-Path>
25+
</manifestEntries>
26+
</archive>
27+
<skip>true</skip>
28+
</configuration>
29+
</plugin>
30+
<plugin>
31+
<artifactId>maven-shade-plugin</artifactId>
32+
<executions>
33+
<execution>
34+
<phase>package</phase>
35+
<goals>
36+
<goal>shade</goal>
37+
</goals>
38+
<configuration>
39+
<transformers>
40+
<transformer/>
41+
</transformers>
42+
</configuration>
43+
</execution>
44+
</executions>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.eclipse.jetty.aggregate</groupId>
51+
<artifactId>jetty-all</artifactId>
52+
<version>9.2.11.v20150529</version>
53+
<scope>test</scope>
54+
<exclusions>
55+
<exclusion>
56+
<artifactId>javax.websocket-api</artifactId>
57+
<groupId>javax.websocket</groupId>
58+
</exclusion>
59+
</exclusions>
60+
</dependency>
61+
</dependencies>
6162
</project>

java/javase/javase-annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor

Lines changed: 0 additions & 1 deletion
This file was deleted.

java/javase/javase-annotation-processor/pom.xml renamed to java/javase/javase-annotation/annotation-processor/pom.xml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<parent>
6-
<artifactId>javase</artifactId>
6+
<artifactId>javase-annotation</artifactId>
77
<groupId>cn.edu.ntu.java</groupId>
88
<version>1.0-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

12-
<artifactId>javase-annotation-processor</artifactId>
12+
<artifactId>annotation-processor</artifactId>
1313

1414
<dependencies>
15-
<!--
16-
<dependency>
17-
<groupId>com.sun</groupId>
18-
<artifactId>tools</artifactId>
19-
<version>${compiler.level}</version>
20-
<scope>system</scope>
21-
<systemPath>${java.home}/../lib/tools.jar</systemPath>
22-
</dependency>
23-
-->
15+
<dependency>
16+
<groupId>cn.edu.ntu.java</groupId>
17+
<artifactId>javase-common</artifactId>
18+
<version>${project.version}</version>
19+
</dependency>
20+
2421
<dependency>
2522
<groupId>org.frgaal</groupId>
2623
<artifactId>compiler</artifactId>
@@ -43,4 +40,4 @@
4340
</plugin>
4441
</plugins>
4542
</build>
46-
</project>
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
package cn.edu.ntu.java.annotation;
2-
3-
import cn.edu.ntu.java.processor.DataProcessor;
1+
package top.hubby.annotations;
42

53
import java.lang.annotation.ElementType;
64
import java.lang.annotation.Retention;
75
import java.lang.annotation.RetentionPolicy;
86
import java.lang.annotation.Target;
97

108
/**
11-
* @see DataProcessor
12-
* @author alice52
9+
* @author zack <br/>
10+
* @create 2022-12-14 00:11 <br/>
11+
* @project annotation <br/>
1312
*/
1413
@Target({ElementType.TYPE})
1514
@Retention(RetentionPolicy.SOURCE)
16-
public @interface SeData {
17-
}
15+
public @interface SeData {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package top.hubby.annotations;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* @author alice52
7+
*/
8+
@Retention(RetentionPolicy.CLASS)
9+
@Target({ElementType.TYPE})
10+
@Inherited
11+
public @interface Serial {
12+
String value() default "";
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package top.hubby.annotations;
2+
3+
import org.slf4j.LoggerFactory;
4+
5+
import java.lang.annotation.*;
6+
7+
/**
8+
* @author alice52
9+
*/
10+
@Documented
11+
@Retention(RetentionPolicy.SOURCE) // 只在编译期有效, 最终不会打进 class 文件中
12+
@Target({ElementType.FIELD}) // 仅允许作用于类属性之上
13+
public @interface TrisceliVersion {}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package top.hubby.processor;
2+
3+
import com.sun.source.tree.Tree;
4+
import com.sun.tools.javac.api.JavacTrees;
5+
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
6+
import com.sun.tools.javac.tree.JCTree;
7+
import com.sun.tools.javac.tree.TreeMaker;
8+
import com.sun.tools.javac.tree.TreeTranslator;
9+
import com.sun.tools.javac.util.Context;
10+
import com.sun.tools.javac.util.Names;
11+
import lombok.extern.slf4j.Slf4j;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
import javax.annotation.processing.AbstractProcessor;
16+
import javax.annotation.processing.ProcessingEnvironment;
17+
import javax.annotation.processing.RoundEnvironment;
18+
import javax.lang.model.element.TypeElement;
19+
import java.lang.annotation.Annotation;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import java.util.Set;
23+
import java.util.function.Function;
24+
25+
/**
26+
* @author zack <br>
27+
* @create 2022-12-13 22:38 <br>
28+
* @project javase <br>
29+
*/
30+
public abstract class BaseProcessor extends AbstractProcessor {
31+
32+
private static final Logger log = LoggerFactory.getLogger(BaseProcessor.class);
33+
34+
public JavacTrees trees;
35+
public TreeMaker treeMaker;
36+
public Names names;
37+
38+
public List<Function<JCTree.JCVariableDecl, JCTree.JCMethodDecl>> customs = new ArrayList<>();
39+
40+
protected abstract Class<? extends Annotation> anno();
41+
42+
protected abstract void initCustom();
43+
44+
@Override
45+
public synchronized void init(ProcessingEnvironment processingEnv) {
46+
super.init(processingEnv);
47+
this.trees = JavacTrees.instance(processingEnv);
48+
Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
49+
this.treeMaker = TreeMaker.instance(context);
50+
this.names = Names.instance(context);
51+
}
52+
53+
@Override
54+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
55+
56+
TreeTranslator translator =
57+
new TreeTranslator() {
58+
@Override
59+
public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
60+
61+
jcClassDecl.defs.stream()
62+
.filter(jcTree1 -> jcTree1.getKind().equals(Tree.Kind.VARIABLE))
63+
.map(jcTree2 -> (JCTree.JCVariableDecl) jcTree2)
64+
.forEach(
65+
jcVariableDecl -> {
66+
customs.forEach(
67+
x -> {
68+
jcClassDecl.defs =
69+
jcClassDecl.defs.prepend(
70+
x.apply(
71+
jcVariableDecl));
72+
73+
log.info("aaa: {}", x);
74+
}
75+
);
76+
});
77+
78+
super.visitClassDef(jcClassDecl);
79+
}
80+
};
81+
82+
roundEnv.getElementsAnnotatedWith(anno()).stream()
83+
.map(element -> trees.getTree(element))
84+
.forEach(jcTree -> jcTree.accept(translator));
85+
86+
return true;
87+
}
88+
}

java/javase/javase-annotation-processor/src/main/java/cn/edu/ntu/java/processor/DataProcessor.java renamed to java/javase/javase-annotation/annotation-processor/src/main/java/top/hubby/processor/DataProcessor.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package cn.edu.ntu.java.processor;
1+
package top.hubby.processor;
2+
23

3-
import cn.edu.ntu.java.annotation.SeData;
44
import com.sun.source.tree.Tree;
55
import com.sun.tools.javac.api.JavacTrees;
66
import com.sun.tools.javac.code.Flags;
@@ -10,6 +10,7 @@
1010
import com.sun.tools.javac.tree.TreeMaker;
1111
import com.sun.tools.javac.tree.TreeTranslator;
1212
import com.sun.tools.javac.util.*;
13+
import top.hubby.annotations.SeData;
1314

1415
import javax.annotation.processing.*;
1516
import javax.lang.model.SourceVersion;
@@ -19,10 +20,10 @@
1920
/**
2021
* 该代码使用 jdk 8u20 编译会有bug ,bug详情参见:https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8062747
2122
*
22-
* @author : alice52
23+
* @author : cuixiuyin
2324
* @date : 2020/1/10
2425
*/
25-
@SupportedAnnotationTypes("cn.edu.ntu.java.annotation.SeData")
26+
@SupportedAnnotationTypes("org.jvm.processor.lombok.annotations.Data")
2627
@SupportedSourceVersion(SourceVersion.RELEASE_8)
2728
public class DataProcessor extends AbstractProcessor {
2829
private JavacTrees trees;
@@ -67,7 +68,7 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
6768
* @param jcVariableDecl
6869
* @return
6970
*/
70-
private JCTree.JCMethodDecl addGetterMethod(JCTree.JCVariableDecl jcVariableDecl) {
71+
private JCTree.JCMethodDecl addGetterMethod(JCTree.JCVariableDecl jcVariableDecl) {
7172
//方法的访问级别
7273
JCTree.JCModifiers modifiers = treeMaker.Modifiers(Flags.PUBLIC);
7374
//方法名称
@@ -131,4 +132,4 @@ private Name setMethodName(Name name) {
131132
String s = name.toString();
132133
return names.fromString("set" + s.substring(0, 1).toUpperCase() + s.substring(1, name.length()));
133134
}
134-
}
135+
}

0 commit comments

Comments
 (0)