@Target 定义Annotation能够被应用于源码的哪些位置:
- ElementType.TYPE:类或接口
- ElementType.FIELD:字段
- ElementType.METHOD:方法
- ElementType.CONSTRUCTOR:构造方法
- ElementType.PARAMETER:方法参数
@Retention 定义了Annotation的生命周期:
- RetentionPolicy.SOURCE:仅编译期
- RetentionPolicy.CLASS:仅class文件
- RetentionPolicy.RUNTIME:运行期
自定义注解,可在类,接口;构造方法;方法;属性;方法参数上使用
@Retention(RetentionPolicy.RUNTIME)可以在运行期通过反射读取RUNTIME类型的注解,否则运行期无法读取到该注解
@Target({
ElementType.TYPE,
ElementType.CONSTRUCTOR,
ElementType.METHOD,
ElementType.FIELD,
ElementType.PARAMETER
})
@Retention(RetentionPolicy.RUNTIME)
public @interface Alias {
String name() default "";
String value() default "";
String comment() default "";
}
测试用学生类
@Alias(name = "student", comment = "学生类")
public class Student {
@Alias(name = "Name", comment = "名称")
public String name;
@Alias("Age")
private Integer age;
@Alias(name = "student", comment = "student构造方法")
public Student (String name) {
this.name = name;
}
@Alias(name = "GetAge", comment = "获取age属性")
public Integer getAge() {
return age;
}
@Alias(name = "SetAge", comment = "赋值age属性")
public void setAge(@Alias("Age") Integer age) {
this.age = age;
}
}
处理注解,获取注解值
@Test
public void getClassAlias() {
try {
Class<?> cls = Class.forName("com.shpun.annotation.Student");
Annotation[] annotations = cls.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof Alias) {
Alias alias = (Alias) annotation;
String name = alias.name();
String value = alias.value();
String comment = alias.comment();
System.out.println(cls.getName() + " " + name + " " + value + " " + comment);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getConstructorAlias() {
try {
Class<?> cls = Class.forName("com.shpun.annotation.Student");
Constructor<?> constructor = cls.getDeclaredConstructor(String.class);
Annotation[] annotations = constructor.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof Alias) {
Alias alias = (Alias) annotation;
String name = alias.name();
String value = alias.value();
String comment = alias.comment();
System.out.println(cls.getName() + " " + name + " " + value + " " + comment);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getFieldAlias() {
try {
Class<?> cls = Class.forName("com.shpun.annotation.Student");
Field[] fields = cls.getFields();
for (Field field : fields) {
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof Alias) {
Alias alias = (Alias) annotation;
String name = alias.name();
String value = alias.value();
String comment = alias.comment();
System.out.println(field.getName() + " " + name + " " + value + " " + comment);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getMethodAlias() {
try {
Class<?> cls = Class.forName("com.shpun.annotation.Student");
Method[] methods = cls.getMethods();
for (Method method : methods) {
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof Alias) {
Alias alias = (Alias) annotation;
String name = alias.name();
String value = alias.value();
String comment = alias.comment();
System.out.println(method.getName() + " " + name + " " + value + " " + comment);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getParamAlias() {
try {
Class<?> cls = Class.forName("com.shpun.annotation.Student");
Method[] methods = cls.getMethods();
for (Method method : methods) {
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (Annotation[] annotations : parameterAnnotations) {
for (Annotation annotation : annotations) {
if (annotation instanceof Alias) {
Alias alias = (Alias) annotation;
String name = alias.name();
String value = alias.value();
String comment = alias.comment();
System.out.println(cls.getName() + " " + name + " " + value + " " + comment);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
参考:
注解