java基础源码 --reflect包-AnnotatedElement接口
Posted 李科技
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java基础源码 --reflect包-AnnotatedElement接口相关的知识,希望对你有一定的参考价值。
接口:AnnotatedElement
* 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. The arrays returned by methods of this interface may be modified * by callers without affecting the arrays returned to other callers. 表示当前在此虚拟机中运行的程序的注释元素,该界面允许反射读取注释,通过该接口方法返回的所有注释
都是不可变并且可序列化。通过此接口的方法返回的陈列可以由呼叫着,而不会影响其他调用者返回阵列进行修改。
*/ public interface AnnotatedElement { /** * Returns true if an annotation for the specified type * is <em>present</em> on this element, else false. This method * is designed primarily for convenient access to marker annotations. 如果此元素上存在指定类型的注释,则返回true,否则返回false,该方法主要用于方便
访问标记注释
*/ default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { return getAnnotation(annotationClass) != null; } /** * Returns this element\'s annotation for the specified type if * such an annotation is <em>present</em>, else null. 如果这样的注解存在,就返回该注解,否则返回Null*/ <T extends Annotation> T getAnnotation(Class<T> annotationClass); /** * Returns annotations that are <em>present</em> on this element. *返回此元素上存在的注解 */ Annotation[] getAnnotations(); /** * Returns annotations that are <em>associated</em> with this element. 返回与此注解相关联的注解*/ default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { //默认的调用getDeclaredAnnotationsByte传入annotationClass作为参数 T[] result = getDeclaredAnnotationsByType(annotationClass); //如果返回的数组长度大于零,则返回数组,
//如果返回的数组是零长度而这个AnnotationElement是一个类,
//并且参数类型是可继承的注解类型,并且该AnnotatedElement的AnnotatedElement是非空的
//则返回结果是在父类上调用getAnnotationsByType的结果,具有annotationClass作为论证
//否则返回零长度的数组
if (result.length == 0 && // Neither directly nor indirectly present this instanceof Class && // the element is a class AnnotationType.getInstance(annotationClass).isInherited()) { // Inheritable Class<?> superClass = ((Class<?>) this).getSuperclass(); if (superClass != null) { // Determine if the annotation is associated with the // superclass result = superClass.getAnnotationsByType(annotationClass); } } return result; } /** * Returns this element\'s annotation for the specified type if * such an annotation is <em>directly present</em>, else null. 如果这样的注解直接存在,则返回指定类型的元素注解,否则返回null,此方法忽略继承的注解*/ default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); // Loop over all directly-present annotations looking for a matching one for (Annotation annotation : getDeclaredAnnotations()) { if (annotationClass.equals(annotation.annotationType())) { //更强大,在编译期间进行动态转换 return annotationClass.cast(annotation); } } return null; } default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); return AnnotationSupport. getDirectlyAndIndirectlyPresent(Arrays.stream(getDeclaredAnnotations()). collect(Collectors.toMap(Annotation::annotationType, Function.identity(), ((first,second) -> first), LinkedHashMap::new)), annotationClass); } /** * Returns annotations that are <em>directly present</em> on this element. * This method ignores inherited annotations. 返回直接存在于此元素上的注释*/ Annotation[] getDeclaredAnnotations(); }
isAnnotationPresent方法的示例:
@Retention(RetentionPolicy.RUNTIME) @interface Cus{ public String name(); public String value(); } @Cus(name = "SampleClass",value = "Sample Class Annotation") class SampClass{ private String sampleFileld; @Cus(name = "sampleMethod",value = "sample Method Annotation") public String sampleMethod(){ return "sample"; } public String getSampleFileld(){ return sampleFileld; } public void setSampleFileld(String sa){ this.sampleFileld=sa; } } public class AccessibleObjectDemo { public static void main(String [] args) throws NoSuchMethodException { AccessibleObject sampleMethod = SampClass.class.getMethod("sampleMethod"); System.out.println("sampleMethod.isAnnotationPresent:"+sampleMethod.isAnnotationPresent(Cus.class)); //输出结果:sampleMethod.isAnnotationPresent:true } }
getAnnotations()示例:
@Retention(RetentionPolicy.RUNTIME) @interface Tt{ public String name(); public String value(); } @Tt(name = "SampleClass",value = "Sample Class Annotation") class SampClass2{ private String sampleFileld; @Tt(name = "sampleMethod",value = "sample Method Annotation") public String sampleMethod(){ return "sample"; } public String getSampleFileld(){ return sampleFileld; } public void setSampleFileld(String sa){ this.sampleFileld=sa; } } public class AccessibleObjectDemo2 { public static void main(String [] args) throws NoSuchMethodException { AccessibleObject sampleMethod = SampClass2.class.getMethod("sampleMethod"); Annotation[] annotations = sampleMethod.getAnnotations(); for(int i=0;i<annotations.length;i++){ if(annotations[i] instanceof Tt){ Tt cus2= (Tt) annotations[i]; System.out.println(cus2.name()); System.out.println(cus2.value()); /* 输出结果: sampleMethod sample Method Annotation * */ } } } }
以上是关于java基础源码 --reflect包-AnnotatedElement接口的主要内容,如果未能解决你的问题,请参考以下文章