元注解
1.@Retention
标注注解生效的时期
//Retention源码:
@Documented@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
public enum RetentionPolicy {
//此注解类型的信息只会记录在源文件中,编译时将被编译器丢弃
SOURCE,
//编译器将注解记录在类文件中,但不会加载到JVM中。如果一个注解声明没指定范围,则系统
//默认值就是Class
CLASS,
//注解信息会保留在源文件、类文件中,在执行的时也加载到Java的JVM中,因此可以反射性的读取 <最常用>。
RUNTIME
}
2.@Documented
生成javadoc文档信息的时候保留注解信息
3.@Target
用于描述注解的使用目标
//Target源码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
值来自枚举类:ElementType
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
4.@Inherited
说明注解可以被继承,标注父类相当于标注子类。
5.@Repeatable
jdk8之前的注解不能够重复使用,这个注解可以解决复用问题
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {
/**
* Indicates the <em>containing annotation type</em> for the
* repeatable annotation type.
* @return the containing annotation type
*/
Class<? extends Annotation> value();
}
使用时需要指定这个注解的具体类型,如:
@Target(ElementType.METHOD)
@Retention( RetentionPolicy.RUNTIME )
@Repeatable(FileTypes.class )
public @interface FileType {};
自定义注解
自定可修饰对象:确认修饰目标,默认好像都能修饰,确认生效时间
例子:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {
String value(); //使用时必须指定值
String[] key() default {"你好","hello"};//可以不必须指定值
}
使用
@Component(value="app",key={"hello"}) // key-value的形式赋值
//@Component("app") //只包含第一个属性可以不用指出key值
public class AppConfig {
}
官方注解
1,@Override
标注方法上,表明进行了重写,编译阶段如果使用了该注解的方法没有重写则编译报错
2,@Deprecated
用于标明被修饰的类或类成员、类方法已经废弃、过时,不建议使用。
3,@SuppressWarnings
用于关闭对类、方法、成员编译时产生的特定警告。
@SuppressWarnings("unchecked")//执⾏了未检查的转换时的警告,例如当使⽤集合时没有⽤泛型 (Generics) 来指定集合保存的类型。
@SuppressWarnings("unused") //未使⽤的变量
@SuppressWarnings("resource") //有泛型未指定类型
@SuppressWarnings("path") //在类路径、源⽂件路径等中有不存在的路径时的警告
@SuppressWarnings("deprecation") //使⽤了不赞成使⽤的类或⽅法时的警告
@SuppressWarnings("fallthrough") //当 Switch 程序块直接通往下⼀种情况⽽没有 break; 时的警告
@SuppressWarnings("serial")//某类实现Serializable(序列化),但没有定义 serialVersionUID 时的警告
@SuppressWarnings("rawtypes") //没有传递带有泛型的参数
@SuppressWarnings("finally") //任何 finally ⼦句不能正常完成时的警告。
@SuppressWarnings("try") // 没有catch时的警告
@SuppressWarnings("all") //所有类型的警告
// 以下是源码引⽤中见到的,但实际很少⽤到的
@SuppressWarnings("FragmentNotInstantiable")
@SuppressWarnings("ReferenceEquality")
@SuppressWarnings("WeakerAccess")
@SuppressWarnings("UnusedParameters")
@SuppressWarnings("NullableProblems")
@SuppressWarnings("SameParameterValue")
@SuppressWarnings("PointlessBitwiseExpression")