首页 > 编程语言 >Java元注解

Java元注解

时间:2023-09-04 11:56:37浏览次数:33  
标签:Java annotated annotation 注解 type class

工作过程中遇到以下需求:校验某些类的某些字符串属性的长度。
由于不想对所有的类和属性进行枚举检查,因此,我想通过在类上添加自定义注解的方式过滤出这些类以及属性。故学习一下Java的元注解。

Java元注解

这里需要说一下Java的内置注解,因为Java里有几个针对注解的注解,即元注解。

@Retention

Indicates how long annotations with the annotated type are to be retained. If no Retention annotation is present on an annotation type declaration, the retention policy defaults to RetentionPolicy.CLASS.
A Retention meta-annotation has effect only if the meta-annotated type is used directly for annotation. It has no effect if the meta-annotated type is used as a member type in another annotation type.

这是Java关于@Retention的注解说明,此注解用于指示被@Retention注解的注解保留多久,默认是RententionPolicy.CLASS。具体可选值如下:

Annotations are to be discarded by the compiler.

SOURCE:会被编译器忽略

Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. This is the default behavior.

CLASS:编译器会保留但是JVM会忽略

Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.

RUNTIME:运行时依旧保留,所以可以通过反射获得此注解

@Documented

Indicates that annotations with a type are to be documented by javadoc and similar tools by default. This type should be used to annotate the declarations of types whose annotations affect the use of annotated elements by their clients. If a type declaration is annotated with Documented, its annotations become part of the public API of the annotated elements.

有此注解的注解将默认被javadoc或类似工具记录。

@Target

此注解用于指示当前被注解的注解可以使用的范围,例如:

@Target(ElementType.TYPE)
public @interface AAA{
  // TODO
}

以上代码中,AAA注解可用于其他类上。
其可选参数为java.lang.annotation.ElementType枚举类,具体说明不列出来了,大概说明下常用的几个:

  • TYPE:注解可用于类、接口等
  • FIELD:注解可用于字段和枚举常量
  • METHOD:注解可用于方法
  • PARAMETER:注解可用于方法参数
  • CONSTRUCTOR:注解可用于构造器
  • LOCAL_VARIABLE:注解可用于局部变量
  • ANNOTATION_TYPE:注解可用于其他注解
  • TYPE_PARAMETER:注解可用于泛型参数

@Inherited

Indicates that an annotation type is automatically inherited. If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.

举例说明下,一个注解@A,它的注解上有@Inherited时,那么当类ClassA被@A注解时,ClassA的子类ClassB也将继承@A,但仅限于超类子类中,接口和类实现接口里@Inherited不起作用。

@Repeatable

被此注解修饰的注解可重复使用,直接举例:

@Repeatable(AAAs.class)
public @interface AAA{
  String value();
}

public @interface AAAs{
  AAA[] value();
}

@AAA("bbb")
@AAA("ccc")
public class BBB{
  // TODO
}

此注解在1.8时加入,实际为语法糖。

标签:Java,annotated,annotation,注解,type,class
From: https://www.cnblogs.com/liu-im/p/17676539.html

相关文章

  • Java 20 maven项目基本配置
     pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://ma......
  • Java 快速排序
    思路通过一趟排序将无序数组划分成独立的两部分,其中一部分的所有元素比另外一部分的所有元素都要小,然后再按此方法对这两部分元素分别进行快速排序,整个排序过程可以递归进行,以此达到整个无序数组变成有序数组的目的。快速排序主要分为以下步骤:从无序数组中取出一个元素作为基......
  • Java 二分查找
    思路问题描述:在采用顺序存储结构的有序数组中,查找目标元素,如果目标元素存在,返回对应的数组下标。假设查找的有序数组为升序,二分查找采用以下的思路进行解决:将数组中间位置的元素与目标元素比较,如果二者相等,则查找成功;否则,从中间位置将数组分为前、后两个数组;如果中间位置......
  • Java 归并排序
    思路数组排序主要分为两个部分:划分数组和归并排序。划分数组:将待排序的无序数组分为左右两个部分,如果无序数组的起始元素下标为first,最后一个元素的下标为last,那么左右两部分之间的临界点下标mid=(first+last)/2,这两部分分别是arr[first…mid]和arr[mid+1…last];将上......
  • Java 堆排序
    思路从最后的非叶子节点开始,从后向前构建一个堆(大顶堆/小顶堆);即最后的非叶子节点和其下的叶子节点构成一个大顶堆,然后再找前面一个非叶子节点继续此时根节点是最大的数据,然后将根节点和最后一位进行交换交换后,排除最后一位最大值,再从根节点开始构建大顶堆重复2,3步骤代码......
  • 20230529 java.lang.reflect.InvocationHandler
    介绍java.lang.reflect.InvocationHandlerpublicinterfaceInvocationHandlerAPIpublicinvokeinvokeDefault调用接口的default方法......
  • 20230529 java.lang.reflect.AnnotatedElement
    介绍java.lang.reflect.AnnotatedElementpublicinterfaceAnnotatedElementAPIisAnnotationPresentgetAnnotationgetAnnotationsgetAnnotationsByTypegetDeclaredAnnotationgetDeclaredAnnotationsByTypegetDeclaredAnnotations......
  • maven-resources-production:webapi: java.lang.NegativeArraySizeException
    maven-resources-production:webapi:java.lang.NegativeArraySizeException打开项目启动时,发现报这个错误,基于此,我分析了一下,首先原本好好的项目突然这样子,首先查看代码更新的情况,发现代码并没有作任何变化。分析代码jar包的问题,首先mvnclean和mvninstall直接一起上。代码可......
  • java多线程爬取笔趣阁所有小说
    可以选择下载的数量,全部下载下来够呛,首先没那么大的盘新版本:https://wws.lanzous.com/iAEMoghsgeb密码:7vjzjar包:https://wws.lanzous.com/ilphyghsgcj密码:f38a <dependency><!--jsoupHTMLparserlibrary[url=home.php?mod=space&uid=402414]@[/url]htt......
  • 全开源风车im源码(前端uniapp可发布H5及app/后端java含视频搭建教程)
    互联网彻底改变了我们的沟通方式,电子邮件是迄今为止采用最快的通信形式。不到二十年前,还没有多少人听说过它。现在,我们中的许多人都用电子邮件而不是写信,甚至打电话给别人,世界各地的人们每天发送数十亿封电子邮件。源码:ms.jstxym.top但有时甚至电子邮件也不够快。您可能不知道您......