1、AnnotatedElement接口
- boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
- 这里的传参实际上就是传入 ”某个注解.class“
/** * Represents an annotated element of the program currently running in this * VM. This interface allows annotations to be read reflectively. All * annotations returned by methods in this interface are immutable and * serializable. It is permissible for the caller to modify the * arrays returned by accessors for array-valued enum members; it will * have no affect on the arrays returned to other callers. * *If an annotation returned by a method in this interface contains * (directly or indirectly) a {@link Class}-valued member referring to * a class that is not accessible in this VM, attempting to read the class * by calling the relevant Class-returning method on the returned annotation * will result in a {@link TypeNotPresentException}. * *
Similarly, attempting to read an enum-valued member will result in * a {@link EnumConstantNotPresentException} if the enum constant in the * annotation is no longer present in the enum type. * *
Finally, Attempting to read a member whose definition has evolved * incompatibly will result in a {@link * java.lang.annotation.AnnotationTypeMismatchException} or an * {@link java.lang.annotation.IncompleteAnnotationException}. * * @since 1.5 * @author Josh Bloch */
/**
* Represents an annotated element of the program currently running in this
* VM. This interface allows annotations to be read reflectively. All
* annotations returned by methods in this interface are immutable and
* serializable. It is permissible for the caller to modify the
* arrays returned by accessors for array-valued enum members; it will
* have no affect on the arrays returned to other callers.
*
*If an annotation returned by a method in this interface contains
* (directly or indirectly) a {@link Class}-valued member referring to
* a class that is not accessible in this VM, attempting to read the class
* by calling the relevant Class-returning method on the returned annotation
* will result in a {@link TypeNotPresentException}.
*
*Similarly, attempting to read an enum-valued member will result in
* a {@link EnumConstantNotPresentException} if the enum constant in the
* annotation is no longer present in the enum type.
*
*Finally, Attempting to read a member whose definition has evolved
* incompatibly will result in a {@link
* java.lang.annotation.AnnotationTypeMismatchException} or an
* {@link java.lang.annotation.IncompleteAnnotationException}.
*
* @since 1.5
* @author Josh Bloch
*/
- Class 类
- Constructor 构造器
- Field 类的成员变量
- Method 类的方法
- Package 类的包
- //判断该程序元素上是否包含指定类型的注解,存在返回true,否则返回false
- boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);
- //返回该程序元素上存在的、指定类型的注解,如果该类型注解不存在,则返回null
- <T extends Annotation> T getAnnotation(Class<T> annotationClass);
- //返回该程序元素上存在的所有注解
- Annotation[] getAnnotations();
- //返回直接存在于该元素上的所有注解(与该接口中其他方法不同,该方法将忽略继承的注解)
- //如果没有注解直接存在于此元素上,则返回长度为零的一个数组
- //该方法调用者可以随意修改返回的数组,不会对其他调用者返回的数组产生影响
- Annotation[] getDeclaredAnnotations();
2、AnnotatedElement使用示例
2.1 自定义注解
/** * 水果名称注解 */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface FruitName { String value() default "";}/** * 水果颜色注解 */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface FruitColor { /** * 颜色枚举 */ public enum Color {BULE, RED, GREEN} /** * 颜色属性 */ public Color fruitColor() default Color.GREEN;}/** * 水果供应商注解 */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface FruitProvider { /** * 供应商编号 */ public int id() default -1; /** * 供应商名称 */ public String name() default ""; /** * 供应商地址 */ public String address() default "";}
/**
* 水果名称注解
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {
String value() default "";
}
/**
* 水果颜色注解
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor {
/**
* 颜色枚举
*/
public enum Color { BULE, RED, GREEN}
/**
* 颜色属性
*/
public Color fruitColor() default Color.GREEN;
}
/**
* 水果供应商注解
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitProvider {
/**
* 供应商编号
*/
public int id() default -1;
/**
* 供应商名称
*/
public String name() default "";
/**
* 供应商地址
*/
public String address() default "";
}
2.2 使用自定义注解的类
/** * 苹果类 */public class Apple { /** * 苹果名称 */ @FruitName("红富士") private String appleName; /** * 苹果颜色 */ @FruitColor(fruitColor = FruitColor.Color.RED) private String appleColor; /** * 苹果供应商 */ @FruitProvider(id = 1, name = "陕西红富士集团", address = "陕西省西安市延安路89号红富士大厦") private String appleProvider; public String getAppleName() { return appleName; } public void setAppleName(String appleName) { this.appleName = appleName; } public String getAppleColor() { return appleColor; } public void setAppleColor(String appleColor) { this.appleColor = appleColor; } public String getAppleProvider() { return appleProvider; } public void setAppleProvider(String appleProvider) { this.appleProvider = appleProvider; }}
/**
* 苹果类
*/
public class Apple {
/**
* 苹果名称
*/
@FruitName("红富士")
private String appleName;
/**
* 苹果颜色
*/
@FruitColor(fruitColor = FruitColor.Color.RED)
private String appleColor;
/**
* 苹果供应商
*/
@FruitProvider(id = 1, name = "陕西红富士集团", address = "陕西省西安市延安路89号红富士大厦")
private String appleProvider;
public String getAppleName() {
return appleName;
}
public void setAppleName(String appleName) {
this.appleName = appleName;
}
public String getAppleColor() {
return appleColor;
}
public void setAppleColor(String appleColor) {
this.appleColor = appleColor;
}
public String getAppleProvider() {
return appleProvider;
}
public void setAppleProvider(String appleProvider) {
this.appleProvider = appleProvider;
}
}
2.3 注解处理
public class FruitInfoUtil { /** * 获取水果信息 * @param clazz 类对象 */ public static void getFruitInfo(Class clazz) { String strFruitName; String strFruitColor; String strFruitProvider; //获取该类对象包含的所有属性对象 Field[] fields = clazz.getDeclaredFields(); //遍历属性对象 for (Field field : fields) { //如果属性包含的注解是FruitName if (field.isAnnotationPresent(FruitName.class)) { FruitName fruitName = field.getAnnotation(FruitName.class); strFruitName = "水果名称:" + fruitName.value(); System.out.println(strFruitName); } //如果属性包含的注解是FruitColor else if (field.isAnnotationPresent(FruitColor.class)) { FruitColor fruitColor = field.getAnnotation(FruitColor.class); strFruitColor = "水果颜色:" + fruitColor.fruitColor().toString(); System.out.println(strFruitColor); } //如果属性包含的注解是FruitProvider else if (field.isAnnotationPresent(FruitProvider.class)) { FruitProvider fruitProvider = field.getAnnotation(FruitProvider.class); strFruitProvider = "供应商编号:" + fruitProvider.id() + ";供应商名称:" + fruitProvider.name() + ";供应商地址:" + fruitProvider.address(); System.out.println(strFruitProvider); } } } }
public class FruitInfoUtil {
/**
* 获取水果信息
* @param clazz 类对象
*/
public static void getFruitInfo(Class clazz) {
String strFruitName;
String strFruitColor;
String strFruitProvider;
//获取该类对象包含的所有属性对象
Field[] fields = clazz.getDeclaredFields();
//遍历属性对象
for (Field field : fields) {
//如果属性包含的注解是FruitName
if (field.isAnnotationPresent(FruitName.class)) {
FruitName fruitName = field.getAnnotation(FruitName.class);
strFruitName = "水果名称:" + fruitName.value();
System.out.println(strFruitName);
}
//如果属性包含的注解是FruitColor
else if (field.isAnnotationPresent(FruitColor.class)) {
FruitColor fruitColor = field.getAnnotation(FruitColor.class);
strFruitColor = "水果颜色:" + fruitColor.fruitColor().toString();
System.out.println(strFruitColor);
}
//如果属性包含的注解是FruitProvider
else if (field.isAnnotationPresent(FruitProvider.class)) {
FruitProvider fruitProvider = field.getAnnotation(FruitProvider.class);
strFruitProvider = "供应商编号:" + fruitProvider.id()
+ ";供应商名称:" + fruitProvider.name()
+ ";供应商地址:" + fruitProvider.address();
System.out.println(strFruitProvider);
}
}
}
}
2.4 测试和结果输出
public class TestAnnotation { public static void main(String[] args) { FruitInfoUtil.getFruitInfo(Apple.class); }}//结果输出水果名称:红富士水果颜色:RED供应商编号:1;供应商名称:陕西红富士集团;供应商地址:陕西省西安市延安路89号红富士大厦
public class TestAnnotation {
public static void main(String[] args) {
FruitInfoUtil.getFruitInfo(Apple.class);
}
}
//结果输出
水果名称:红富士
水果颜色:RED
供应商编号:1;供应商名称:陕西红富士集团;供应商地址:陕西省西安市延安路89号红富士大厦