定义:注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。
自定义注解
public @interface MyAnnotation {
属性列表
}
如果一个注解当中有属性,那么使用这个注解时,必须给这个属性赋值,除非该属性使用default指定的默认值。
注解中属性的类型可以是byte、short 、int、 long、 float、 double、 boolean、 char、 String、 Class、 枚举类型;以及以上类型的数组形式。
public @interface MyAnnotation {
// int是属性的类型,value是属性名,不是方法名
int value();
String name() default "";
}
public class MyAnnotationTest {
@MyAnnotation(value = 2)
public void test() {
}
}
如果一个属性的名字是value,并且使用时只有这一个属性,那么该属性名可以省略。
public class MyAnnotationTest {
@MyAnnotation(2)
public void test() {
}
}
内置注解
在java.lang包下内置了三种注解
@Override 表示当前的方法定义将覆盖父类中的方法,如果你不小心拼写错误,或者方法签名对不上被覆盖的方法,编译器就会发出错误提示
@Deprecated 如果你使用了被这个注解标注的元素,那么编译器会发出警告信息,通常是因为它是危险的,或者因为存在更好的替代方法。
@SuppressWarnings 关闭不当的编译器警告信息
元注解
元注解专职注解其他的注解,元注解一共有五种。
@Target:表示被@Target“标注的注解”可以出现在什么位置。可能的ElementType参数包括:
CONSTRUCTOR:构造器的声明
FIELD:域声明(包括enum实例)
LOCAL_VARIABLE:局部变量声明
METHOD:方法声明
PACKAGE:包声明
PARAMETER:参数声明
TYPE:类、接口(包括注解类型)或enum声明
@Retention:表示需要在什么级别保存该注解信息。可选的RetentionPolicy参数包括:
SOURCE:注解只保存在java源文件中,会被编译器丢弃。
CLASS:表示该注解被保存在class文件中,会被VM丢弃。
RUNTIME:VM在运行期也保留注解,因此可以通过反射机制读取“被标注的注解”的信息。
@Documented:将“被标注的注解”包含在javadoc中
@Inherited:允许子类继承父类中的注解
@Repeatable:用于声明“被标注的注解”为可重复类型注解,可以在同一个地方多次使用。
通过反射获取注解
自定义注解:
package com.xs.annot;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
// 保证这个注解可以被反射
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String name() default "x";
int value();
}
注解作用的类:
package com.xs.annot;
@MyAnnotation(5)
public class MyAnnotationTest {
public void test() {
}
}
通过反射获取注解:
package com.xs.annot;
public class ReflectAnnotationTest {
public static void main(String[] args) {
try {
// 获取MyAnnotationTest类的Class对象
Class c = Class.forName("com.xs.annot.MyAnnotationTest");
// 判断类上面是否有@MyAnnotation
System.out.println(c.isAnnotationPresent(MyAnnotation.class)); // true
if (c.isAnnotationPresent(MyAnnotation.class)) {
// 获取该注解对象
MyAnnotation myAnnotation = (MyAnnotation) c.getAnnotation(MyAnnotation.class);
System.out.println(myAnnotation); // @com.xs.annot.MyAnnotation(name=x, value=5)
int value = myAnnotation.value();
String name = myAnnotation.name();
System.out.println(value); // 5
System.out.println(name); // x
}
} catch (Exception e) {
e.printStackTrace();
}
}
}