惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - 有容乃大

GitHub相关 2022又要重新找工作开始新的历程 java之面向对象详解(转) java关于for循环的效率优化 JAVA线程池ThreadPoolExecutor的分析和使用(新手踩坑和推荐方案) JAVA常量池 Java String的intern()注意事项(分JDK1.6及JDK1.7) JAVA的类加载过程 使用RabbitMQ实现延迟任务 JAVA三元运算符空指针引用的坑 Java中static块、构造块、构造函数的执行顺序 Ansj中文分词 关于HashMap、HashSet和ArrayList集合对象容量初始值设置及扩容演示 将异常对象转为字符串 JVM的内存区域划分 深入理解Java String类(综合) 理解JAVA的IO JAVA中Integer的==和equals注意 JVM原理摘要
理解Java注解类型
有容乃大 · 2019-03-13 · via 博客园 - 有容乃大

一. 理解Java注解

注解本质是一个继承了Annotation的特殊接口,其具体实现类是Java运行时生成的动态代理类。而我们通过反射获取注解时,返回的是Java运行时生成的动态代理对象$Proxy1。通过代理对象调用自定义注解(接口)的方法,会最终调用AnnotationInvocationHandler的invoke方法。该方法会从memberValues这个Map中索引出对应的值。而memberValues的来源是Java常量池。
实际上Java注解与普通修饰符(public、static、void等)的使用方式并没有多大区别,下面的例子是常见的注解

public class AnnotationDemo {
   
   @Test
   public static void A(){
       System.out.println("Test.....");
   }
   @Deprecated
   @SuppressWarnings("uncheck")
   public static void B(){
 
   }
}

通过在方法上使用@Test注解后,在运行该方法时,测试框架会自动识别该方法并单独调用,@Test实际上是一种标记注解,起标记作用,运行时告诉测试框架该方法为测试方法。而对于@Deprecated和@SuppressWarnings(“uncheck”),则是Java本身内置的注解,在代码中,可以经常看见它们,但这并不是一件好事,毕竟当方法或是类上面有@Deprecated注解时,说明该方法或是类都已经过期不建议再用,@SuppressWarnings 则表示忽略指定警告,比如@SuppressWarnings(“uncheck”),这就是注解的最简单的使用方式,那么下面我们就来看看注解定义的基本语法

二. 基本语法
声明注解与元注解

我们先来看看前面的Test注解是如何声明的:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface OpenAPIMeta {
    /**
     * 字符参数长度(仅需要对字符串类型进行设置)。
     */
    String limit();

    /**
     * 参数示例值(很重要不要随意赋值,用于生成示例json)。
     */
    String demo();

    /**
     * 参数说明。
     */
    String remark();

    /**
     * 参数是否必填项(0为非必填 ;1为必填 )
     */
    int isRequired() default 0;

    /**
     * 是否有扩展参数说明
     */
    boolean hasExtend() default false;

    /**
     * 枚举对象完整类型限定名
     */
    String enumType() default "";
}

三. 注解支持的数据类型
所有基本类型(int,float,boolean,byte,double,char,long,short)
String
Class (如:Class<?> 或 Class<T>)
enum
Annotation
上述类型的数组

四. 获取类级注解

DomainEventHandlerMessageQueue dehmqAnnotation = null;
Annotation[] annotations = obj.getClass().getAnnotations();
for (Annotation x : annotations) {
if (x.annotationType() == DomainEventHandlerMessageQueue.class) {
    dehmqAnnotation = x;
    break;
} 

五. 获取方法注解

Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
    OpenAPIMeta apiAnnotation = null;
    for (Annotation x : field.getDeclaredAnnotations()) {
        if (x.annotationType() == OpenAPIMeta.class) {
            apiAnnotation = (OpenAPIMeta) x;
            break;
        }
    }
}