Java注解总结

概念

注解 :附加在代码上的信息,用于代码在编译或者运行时,对代码起到说明或者配置的作用

  • JDK支持版本:1.5以后

  • 定义:

public @interface AnnotationDemo{
}

元注解 : 修饰注解的注解
常用元注解 有 @Retention @Target @Document @Inherited 4种

@Retention

定义注解的保留策略,描述注解在什么时间生效

  • RetentionPolicy.SOURCE 注解只在源码阶段保留,字节码文件不包含。源码有效 编译无效 运行无效
  • RetentionPolicy.CLASS 默认保留策略,注解被保留到编译时,类加载时丢失。源码有效,编译有效,运行无效
  • RetentionPolicy.RUNTIME 常用模式,注解可以被保留到程序运行时。 源码有效,编译有效,运行有效

@Target

定义注解的修饰范围

  • ElementType.TYPE 可以给比如类、接口、枚举进行注解
  • ElementType.FIELD 可以给属性进行注解
  • ElementType.METHOD 可以给方法进行注解
  • ElementType.PARAMETER 可以给形参进行注解
  • ElementType.LOCAL_VARIABLE 可以给局部变量进行注解
  • ElementType.CONSTRUCTOR 可以给构造方法进行注解
  • ElementType.PACKAGE 可以给包进行注解
  • ElementType.ANNOTATION_TYPE 可以给注解进行注解

@Documented

如果一个注解定义时,使用过@Documented元注解修饰,那么代码使用这个注解时,javadoc文档能将其同@return,@param一样提取成文档

@Inherited

允许子类继承父类中的注解,即拥有此注解的元素其子类可以继承父类的注解

注解的属性

  • 注解只有成员变量,没有方法。
  • 注解的属性定义 以“无形参的抽象方法来声明” 方法名=变量名,返回值=变量的类型。
  • 注解的属性可以有默认值 ,使用default 指定
  • 注解里只有 一个属性value时,使用注解时可以不指定属性名,直接在()里赋值.。
//定义一个注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationDemo{
    public int id();
    public String name() default "helloworld";
}

//测试注解使用
@AnnotationDemo(id=1,name="hello")
public class Test {
}

注解的提取

注解的提取使用反射技术

  • 判断元素是否使用了指定类型的注解
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {}
  • 获取元素上指定类型的注解
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {}
  • 获取元素上所有注解
public Annotation[] getAnnotations() {}

eg.

@AnnotationDemo(value="testclass",name="xiaohei")
public class Demo {

    @AnnotationDemo(value = "testField",name="xiaobai")
    private String aa;

    @AnnotationDemo(value = "testfunction",name="xiaobai")
    public void sayHello(){
    }
}

class Test{
    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {

        //判断目标类是否使用了某个注解
        boolean isAnnotationPresent = Demo.class.isAnnotationPresent(AnnotationDemo.class);
        System.out.println(isAnnotationPresent);


        //获取目标类上的指定注解
        Annotation annotation = Demo.class.getAnnotation(AnnotationDemo.class);
        System.out.println(annotation);
        //获取注解上的属性
        if(annotation != null){
            System.out.println(((AnnotationDemo) annotation).name());
            System.out.println(((AnnotationDemo) annotation).value());
        }


        //获取目标类上所有注解
        Annotation[] annotations = Demo.class.getAnnotations();
        for (Annotation annotation1 : annotations) {
            System.out.println(annotation1);
        }


        //获取方法上的注解
        Method sayHello = Demo.class.getDeclaredMethod("sayHello");
        //判断目标方法是否使用了某个注解
        boolean annotationPresent = sayHello.isAnnotationPresent(cn.gjxblog.annotation.AnnotationDemo.class);
        System.out.println(annotationPresent);


        //获取属性上的注解
        Field aa = Demo.class.getDeclaredField("aa");
        //获取属性上的所有注解
        Annotation[] declaredAnnotations = aa.getDeclaredAnnotations();
        for (Annotation declaredAnnotation : declaredAnnotations) {
            System.out.println(declaredAnnotation);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值