项目一:
pom.xml
<groupId>hello</groupId>
<artifactId>helloa</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dom4j.version>2.1.1</dom4j.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
</plugins>
</build>
注解类:MyAnnotation.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface MyAnnotation {
String value();
}
注解处理器类:AnnotationProcess.java
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.HashSet;
import java.util.Set;
public class AnnotationProcess extends AbstractProcessor {
private Set<String> supportedAnnotationTypes = new HashSet<String>();
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
supportedAnnotationTypes.add(MyAnnotation.class
.getCanonicalName());
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Messager messager = processingEnv.getMessager();
for (TypeElement typeElement : annotations) {
for (Element element : roundEnv
.getElementsAnnotatedWith(typeElement)) {
String info = "### content = " + element.toString();
messager.printMessage(Diagnostic.Kind.NOTE, info);
//获取Annotation
MyAnnotation myAnnotation = element
.getAnnotation(MyAnnotation.class);
if (myAnnotation != null) {
String value = myAnnotation.value();
messager.printMessage(Diagnostic.Kind.NOTE, value);
}
}
}
return false;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.RELEASE_8;
}
@Override
public Set<String> getSupportedAnnotationTypes() {
return supportedAnnotationTypes;
}
}
resources下建文件:src/main/resources/META-INF/services/javax.annotation.processing.Processor
内容com.xxx.AnnotationProcess
mvn clean install
项目二:使用
pom.xml
<dependencies>
<dependency>
<groupId>hello</groupId>
<artifactId>helloa</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
DemoAnn.java
@MyAnnotation("Hi,annotation")
public class DemoAnn {
public static void main(String[] args) {
System.out.println("args = " + args);
}
}
DemoAnn2.java
@MyAnnotation("Hi,annotation 2!")
public class DemoAnn2 {
public static void main(String[] args) {
System.out.println("args = " + args);
}
}
点击 Build Project: