标签:value reference int miss rri 保留 restrict missing 参数
上篇文章,我们简单的实现了一个自定义注解,相信大家对自定义注解有了个简单的认识,这篇,这样介绍下注解中的元注解和内值注解

这个注解大家应该经常用到,主要在子类重写父类的方法,比如toString()方法
package com.kevin.demo;
public class Demo1 {
@Override
public String toString(){
return "demo1";
}
}
@Deprecated可以修饰的范围很广,包括类、方法、字段、参数等,它表示对应的代码已经过时了,程序员不应该使用它,不过,它是一种警告,而不是强制性的。
package com.kevin.demo;
public class Demo1 {
@Deprecated
public void goHome(){
System.out.println("过时的方法");
}
}
idea中调用这些方法,编译器也会显示删除线并警告

@SuppressWarnings表示压制Java的编译警告,它有一个必填参数,表示压制哪种类型的警告.
| 关键字 | 用途 |
|---|---|
| all | to suppress all warnings |
| boxing | to suppress warnings relative to boxing/unboxing operations |
| cast | to suppress warnings relative to cast operations |
| dep-ann | to suppress warnings relative to deprecated annotation |
| deprecation | to suppress warnings relative to deprecation |
| fallthrough | to suppress warnings relative to missing breaks in switch statements |
| finally | to suppress warnings relative to finally block that don?ˉt return |
| hiding | to suppress warnings relative to locals that hide variable |
| incomplete-switch | to suppress warnings relative to missing entries in a switch statement (enum case) |
| nls | to suppress warnings relative to non-nls string literals |
| null | to suppress warnings relative to null analysis |
| rawtypes | to suppress warnings relative to un-specific types when using generics on class params |
| restriction | to suppress warnings relative to usage of discouraged or forbidden references |
| serial | to suppress warnings relative to missing serialVersionUID field for a serializable class |
| static-access | to suppress warnings relative to incorrect static access |
| synthetic-access | to suppress warnings relative to unoptimized access from inner classes |
| unchecked | to suppress warnings relative to unchecked operations |
| unqualified-field-access | to suppress warnings relative to field access unqualified |
| unused | to suppress warnings relative to unused code |
上面的方法,我们就可以增加
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Demo1 demo1 = new Demo1();
demo1.goHome();
}
元注解:注解的注解,即java为注解开发特准备的注解。
我们以上面讲到的java内置注解@Override为例,学习下java元注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
@Target表示注解的目标,@Override的目标是方法(ElementType.METHOD),ElementType是一个枚举,其他可选值有:
目标可以有多个,用{}表示,比如@SuppressWarnings的@Target就有多个,定义为:
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
String[] value();
}
如果没有声明@Target,默认为适用于所有类型。我们上篇文章的demo就没有声明@Target
@Retention表示注解信息保留到什么时候,取值只能有一个,类型为RetentionPolicy,它是一个枚举,有三个取值:
SOURCE:只在源代码中保留,编译器将代码编译为字节码文件后就会丢掉CLASS:保留到字节码文件中,但Java虚拟机将class文件加载到内存时不一定会在内存中保留RUNTIME:一直保留到运行时如果没有声明@Retention,默认为CLASS。
@Override和@SuppressWarnings都是给编译器用的,所以@Retention都是RetentionPolicy.SOURCE。
用于指定javadoc生成API文档时显示该注解信息。Documented是一个标记注解,没有成员。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。
看个栗子
public class Demo1 {
@Inherited
@Retention(RetentionPolicy.RUNTIME)
static @interface Test {
}
@Test
static class Base {
}
static class Child extends Base {
}
public static void main(String[] args) {
System.out.println(Child.class.isAnnotationPresent(Test.class));
}
}
main方法检查Child类是否有Test注解,输出为true,这是因为Test有注解@Inherited,如果去掉,输出就变成false了
好了,这篇先学习到这,我要好好看看这些知识,下篇介绍注解的解析啦。好了。玩的开心!
标签:value reference int miss rri 保留 restrict missing 参数
原文地址:https://www.cnblogs.com/zhenghengbin/p/9797668.html