编译期注解简单使用

项目一:

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值