美文网首页
注解和反射

注解和反射

作者: jiting45 | 来源:发表于2017-03-16 18:33 被阅读0次

(1)注解@Interface
(2)@Retention 注解可以保留多长时间:
RetentionPolicy.SOURCE:Annotation只保留在源代码中,编译器编译时,直接丢弃这种Annotation。
RetentionPolicy.CLASS:编译器把Annotation记录在class文件中。当运行Java程序时,JVM中不再保留该Annotation。
RetentionPolicy.RUNTIME:编译器把Annotation记录在class文件中。当运行Java程序时,JVM会保留该Annotation,程序可以通过反射获取该Annotation的信息。
(3)@Target指定Annotation用于修饰哪些程序元素。
ElementType.TYPE:能修饰类、接口或枚举类型
ElementType.FIELD:能修饰成员变量
ElementType.METHOD:能修饰方法
ElementType.PARAMETER:能修饰参数
ElementType.CONSTRUCTOR:能修饰构造器
ElementType.LOCAL_VARIABLE:能修饰局部变量
ElementType.ANNOTATION_TYPE:能修饰注解
ElementType.PACKAGE:能修饰包
(4)@Documented
如果定义注解A时,使用了@Documented修饰定义,则在用javadoc命令生成API文档后,所有使用注解A修饰的程序元素,将会包含注解A的说明。
(5)@Inherited指定Annotation具有继承性。
父类使用过注解,那么子类即使不直接使用注解,也会拥有注解。

通过反射提取注解信息:
主要方法有:
1、isAnnotationPresent(Class<? extends Annotation> annotationClass):判断该程序元素上是否存在指定类型的注解,如果存在则返回true,否则返回false。
2、getAnnotation(Class<T> annotationClass):返回该程序元素上存在的指定类型的注解,如果该类型的注解不存在,则返回null
3、Annotation[] getAnnotations():返回该程序元素上存在的所有注解。
为了获取注解信息,必须使用反射知识。

PS:如果想要在运行时提取注解信息,在定义注解时,该注解必须使用@Retention(RetentionPolicy.RUNTIME)修饰。

给定一个类的全额限定名,加载类,并列出该类中被注解@MyTag修饰的方法和没被修饰的方法。
注解定义:

package com.demo1;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyTag {

}

注解处理:

package com.demo1;

import java.lang.reflect.Method;

public class ProcessTool {
    public static void process(String clazz) {
        Class targetClass = null;

        try {
            targetClass = Class.forName(clazz);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        for (Method m : targetClass.getMethods()) {
            if (m.isAnnotationPresent(MyTag.class)) {
                System.out.println("被MyTag注解修饰的方法名:" + m.getName());
            } else {
                System.out.println("没被MyTag注解修饰的方法名:" + m.getName());
            }
        }
    }
}

测试类:

package com.demo1;

public class Demo {
    public static void m1() {

    }

    @MyTag
    public static void m2() {

    }
}

package com.demo1;

public class Test {

    public static void main(String[] args) {
        ProcessTool.process("com.demo1.Demo");
    }
}

运行结果:

没被MyTag注解修饰的方法名:m1
被MyTag注解修饰的方法名:m2
没被MyTag注解修饰的方法名:wait
没被MyTag注解修饰的方法名:wait
没被MyTag注解修饰的方法名:wait
没被MyTag注解修饰的方法名:equals
没被MyTag注解修饰的方法名:toString
没被MyTag注解修饰的方法名:hashCode
没被MyTag注解修饰的方法名:getClass
没被MyTag注解修饰的方法名:notify
没被MyTag注解修饰的方法名:notifyAll

getMethods():返回类的所有的共有方法
getDeclaredMethods():返回类的所有的方法。

相关文章

  • 注解和反射

    注解和反射

  • 一文搞懂反射泛型和反射注解以及通过注解方式写一个BaseDao

    反射泛型和反射注解概括起来就三步: 自定义注解 通过反射获取注解值 使用自定义注解 最终案例 通过自定义注解,将数...

  • 从Java到Kotlin(七)

    反射和注解 目录 1.反射1.1类引用1.2函数引用1.3属性引用 2.注解2.1声明注解2.2构造函数 1.反射...

  • 注解和反射

    (1)注解@Interface(2)@Retention 注解可以保留多长时间:RetentionPolicy....

  • 注解和反射

    注解 什么是注解? 通俗易懂的来说:注释是给人看的,注解可以给机器看。 注解是从JDK5.0引入的新技术。 内置注...

  • 注解和反射

    1、注解的基本理解https://www.jianshu.com/p/9471d6bcf4cf[https://w...

  • 注解和反射

    来源于狂神说笔记

  • 注解和反射

    注解 定义数据 定义数据 输入错误会提示 错误提示 下面解释 @Target:注解的作用目标 参数原文注解Elem...

  • 注解和反射

    https://www.bilibili.com/video/BV1p4411P7V3?p=1[https://w...

  • AS运行Annotation

    AS 运行 Annotation 注解分为运行时注解和编译时注解 运行时注解 原理就是反射和代理(Proxy ) ...

网友评论

      本文标题:注解和反射

      本文链接:https://www.haomeiwen.com/subject/sdsogttx.html