Annotations 2

simple example, IDE為IntelliJ

  • 創建項目
    創建一個標準的帶maven的Java項目
    項目的結構如下:
SimpleAnnotationProcessor
--.idea
--src
----main
------java
------resources
----test
--pom.xml
--SimpleAnnotationProcessor.imi

更改Source Folders


java0.png
  • 創建package和class

package:com.wsl.annotation
class:SimpleAnnotation、SimpleProcessor

目錄結構如下


java1.png
  • SimpleAnnotation
    自定義Annotation, 保留規則為SOURCE, 編譯期被丟棄
package com.wsl.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.SOURCE)
public @interface SimpleAnnotation {
}
  • SimpleProcessor
    這里process的邏輯比較簡單,僅僅是打印一下目標Element的name
public class SimpleProcessor extends AbstractProcessor {

    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
        Messager messager = processingEnv.getMessager();

        for (TypeElement te : annotations) {
            for (Element e : env.getElementsAnnotatedWith(te)) {
                messager.printMessage(Diagnostic.Kind.NOTE, "Printing: " + e.toString());
            }
        }
        return true;
    }

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        Set<String> annotations = new LinkedHashSet<String>();
        annotations.add(SimpleAnnotation.class.getCanonicalName());
        return annotations;
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }
}
  • javax.annotation.processing.Processor
    resources目錄下新建META-INF/services,services目錄下新建javax.annotation.processing.Processor,Processor指定自定義的processor
com.wsl.annotation.SimpleProcessor
  • package JAR
    這里用maven打包,修改pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>SimpleAnnotationProcessor</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--增加如下代碼-->
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <!-- Disable annotation processing for ourselves. -->
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

執行mvn clean package, 在target目錄生成JAR

  • 驗證
    編寫驗證類Test.java,如下
import com.wsl.annotation.SimpleAnnotation;
@SimpleAnnotation
public class Test {
  @SimpleAnnotation
  public static void a1(int x) {
  }

  public static void a2(String[] arr) {
  }
}

wushuanglongdeMac-mini:SimpleAnnotationProcessor wushuanglong$ javac -cp ~/tmp/Simple0.jar Test.java
注: Printing: Test
注: Printing: a1(int)

NOTE:Artifact方式打出來的包沒有MATA-INF,具體原因需要進一步研究

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容