编程语言
首页 > 编程语言> > Java自定义Annotation注解开发详解

Java自定义Annotation注解开发详解

作者:互联网

Java自定义Annotation注解开发详解

目录

介绍

一、运行期的自定义注解

1. Class Level Annotation

2. Method Level Annotation

3. Field Level Annotation

4. 使用自定义注解

5. 处理自定义注解的逻辑

二、编译期的自定义注解

1. 创建自定义注解

2. 实现一个Processor

3. 注册你的Processor

4. 测试你的自定义注解


介绍

Java中的注解是每个开发都会遇到的,但是如果要自定义自己的注解,则需要遵循一些基本的步骤,一般注解的开发有2个基本方法:

  1. 在运行期,通过反射获得当前类,方法,变量上的注解信息来实现自定义注解的功能
  2. 在编译期,通过Annotation Processer预编译生成想要的任何内容或者逻辑

下面将通过2个例子来说明开发一个自定义注解需要哪些步骤。首先我们将看到一个非常简单的例子,我们用这个例子来说明开发自定义annotation的一些基本步骤,我们的第二个例子将介绍自定义注解以及Annotation Processor的一些用法

一、运行期的自定义注解

在下面的例子中,我们将创建3种不同类型自定义注解,以收集所有有自定义注解的类和方法

1. Class Level Annotation

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target(ElementType.TYPE)
  3. public @interface ClassAnnotation {
  4. public String alias() default "";
  5. }

2. Method Level Annotation

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target(ElementType.METHOD)
  3. public @interface MethodAnnotation {
  4. public String alias() default "";
  5. }

3. Field Level Annotation

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target(ElementType.FIELD)
  3. public @interface FieldAnnotation {
  4. public String alias() default "";
  5. }

@Retention注解解释:

@Target注解解释如下,这里只列举了部分@Target 类型,更多的类型请参看JavaDoc。

注解中还有一个alias的string类型参数,缺省值是空字符串,在下一节我们将看到如何使用这个string类型的参数

4. 使用自定义注解

  1. @ClassAnnotation(alias = "test")
  2. public class Test {
  3. @FieldAnnotation
  4. private String name;
  5. @MethodAnnotation(alias="debug")
  6. public String getName() {
  7. return name;
  8. }
  9. }

我们在class, field, method上分别运用我们的自定义注解,并且在method上开启debug日志

5. 处理自定义注解的逻辑

我们已经介绍了如何定义自己的注解,以及如何使用我们的注解,接下来我们将用Java的Reflection API来实现我们自定义注解的逻辑

  1. public void gatherAnnotations {
  2. Map<String, Class> classMap = new HashMap<>();
  3. Class<Test> obj = Test.class;
  4. if(obj.isAnnotationPresent(ClassAnnotation.class)) {
  5. ClassAnnotation classAnnotation = obj.getAnnotation(ClassAnnotation.class);
  6. if ("".equals(classAnnotation.alias())) {
  7. classMap.put(obj.getName(), obj);
  8. }else{
  9. classMap.put(classAnnotation.alias(), obj);
  10. }
  11. }
  12. Map<String, Method> methodMap = new HashMap<>();
  13. for (Method method : obj.getDeclaredMethods()) {
  14. if (method.isAnnotationPresent(MethodAnnotation.class)) {
  15. MethodAnnotation methodAnnotation = method.getAnnotation(MethodAnnotation.class);
  16. if ("".equals(methodAnnotation.alias())) {
  17. methodMap.put(method.getName(), method);
  18. }else{
  19. methodMap.put(methodAnnotation.alias(), method);
  20. }
  21. }
  22. }
  23. }

这里我们运用Java反射收集了所有有我们自定义注解的类和方法,并放到相应的Map中。

至此,我们完成了一个简单的运行期自定义注解的例子,这个例子看上去没有实际的用处,但是在真正的业务场景中,有很多应用都是基于此类逻辑,例如Spring中的@Service和@Autowired注解大都基于这样的逻辑,来进行后续的初始化和注入。

二、编译期的自定义注解

Annotation Processor是代码级别的注解处理器,所以它一般在编译期帮助我们生成我们想要的动态代码,配置文件,文档等,它的使用场景相当广泛,一般包含以下几种,

在这个例子中,我们将创建一个Immutable的类级别的注解,该注解将在编译期检查class中所有的field是否有final关键字修饰

1. 创建自定义注解

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.SOURCE)
  3. public @interface Immutable {
  4. }
  1. 定义了一个Immutable注解
  2. @Target(ElementType.TYPE)表示Immutable注解只能放在类上
  3. @Retention(RetentionPolicy.SOURCE)表示Immutable注解只在编译期生效

2. 实现一个Processor

JDK中已经为我们实现了一个AbstractProcessor, 所以我们要做的是扩展这个Abstract类,并且实现里面的process方法

  1. @SupportedAnnotationTypes("annotation.Immutable")
  2. @SupportedSourceVersion(SourceVersion.RELEASE_11)
  3. @AutoService(Processor.class)
  4. public class ImmutableProcessor extends AbstractProcessor {
  5. @Override
  6. public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  7. List<String> nonPublicFields = new LinkedList<>();
  8. for ( Element element : roundEnv.getElementsAnnotatedWith(Immutable.class)) {
  9. if( element instanceof TypeElement ) {
  10. TypeElement typeElement = (TypeElement) element;
  11. for( final Element enclosedElement: typeElement.getEnclosedElements() ) {
  12. if( enclosedElement instanceof VariableElement) {
  13. VariableElement variableElement = ( VariableElement )enclosedElement;
  14. if( !variableElement.getModifiers().contains( Modifier.FINAL ) ) {
  15. nonPublicFields.add(variableElement.getSimpleName().toString());
  16. }
  17. }
  18. }
  19. if (nonPublicFields.size() > 0) {
  20. processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR,
  21. String.format( "Class %s is not @Immutable, fields %s are not declared as final",
  22. typeElement.getSimpleName(), String.join(",", nonPublicFields)
  23. )
  24. );
  25. }
  26. }
  27. }
  28. return true;
  29. }
  30. }
  1. @SupportedAnnotationTypes("annotation.Immutable")表示ImmutableProcessor 只用于annotation.Immutable注解
  2. @SupportedSourceVersion(SourceVersion.RELEASE_11)表示这个processor支持的JDK最低版本是11
  3. @AutoService(Processor.class)表示使用Google auto-service library注册这个processor,下一节将讨论如何注册你的processor
  4. process方法轮训找到所有有Immutable注解的类,然后遍历所有的方法,查找是否有final关键字,如果没有记录该方法,最后抛出异常

3. 注册你的Processor

Java实际上提供了好几种选择来帮助我们注册自己的Processor使我们的自定义注解生效,这里我们只介绍最常用的方法来注册Processor

  1. 通过Google auto-service library来注册你的processor
  1. @AutoService(Processor.class)
  2. public class ImmutableProcessor extends AbstractProcessor {
  3. // ...
  4. }

      2. 通过Maven plugin来注册你的processor

       使用maven前你的processor必须已经编译,通过其他jar文件的形式加到了dependencies里

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <configuration>
  5. <annotationProcessors>
  6. <annotationProcessor>
  7. annotation.ImmutableProcessor
  8. </annotationProcessor>
  9. </annotationProcessors>
  10. </configuration>
  11. </plugin>

4. 测试你的自定义注解

  1. @Immutable
  2. public class Test {
  3. public String name;
  4. }

在编译期,会收到 Class Test is not @Immutable, fields name are not declared as final 的报错。至此一个简单的使用annotation processor的例子已经完成。

后续文章会继续深入分析介绍Java自定义注解在各个framework中的使用。

https://blog.csdn.net/xchann/article/details/126374273

标签:Processor,Java,自定义,class,Immutable,Annotation,注解,public
来源: https://www.cnblogs.com/sunny3158/p/16651781.html