一、前言
基于spring框架spring-core(4.1.4)中的org.springframework.core.annotation.AnnotationUtils工具类,通过其获取Annotation数组、搜索指定注释Annotation、获取相关对象等常用方法。
二、代码说明
package org.springframework.core.annotation;@b@@b@import TA;;@b@import java.lang.annotation.Annotation;@b@import java.lang.reflect.Method;@b@import java.util.Arrays;@b@import java.util.HashMap;@b@import java.util.Map;@b@import org.springframework.core.BridgeMethodResolver;@b@import org.springframework.util.Assert;@b@@b@public abstract class AnnotationUtils@b@{@b@ static final String VALUE = "value";@b@@b@ public static Annotation[] getAnnotations(Method method)@b@ {@b@ return BridgeMethodResolver.findBridgedMethod(method).getAnnotations();@b@ }@b@@b@ public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType)@b@ {@b@ Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);@b@ Annotation ann = resolvedMethod.getAnnotation(annotationType);@b@ if (ann == null)@b@ for (Annotation metaAnn : resolvedMethod.getAnnotations()) {@b@ ann = metaAnn.annotationType().getAnnotation(annotationType);@b@ if (ann != null)@b@ break;@b@ }@b@@b@@b@ return ann;@b@ }@b@@b@ public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType)@b@ {@b@ Annotation annotation = getAnnotation(method, annotationType);@b@ Class cl = method.getDeclaringClass();@b@ if (annotation == null)@b@ annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());@b@@b@ while (annotation == null) {@b@ cl = cl.getSuperclass();@b@ if (cl == null) break; if (cl == Object.class)@b@ break;@b@ try@b@ {@b@ Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());@b@ annotation = getAnnotation(equivalentMethod, annotationType);@b@ if (annotation != null) break label85;@b@ label85: annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());@b@ }@b@ catch (NoSuchMethodException localNoSuchMethodException)@b@ {@b@ }@b@ }@b@@b@ return annotation;@b@ }@b@@b@ private static <A extends Annotation> A searchOnInterfaces(Method method, Class<A> annotationType, Class[] ifcs) {@b@ Annotation annotation = null;@b@ for (Class iface : ifcs) {@b@ Method equivalentMethod = null;@b@ try {@b@ equivalentMethod = iface.getDeclaredMethod(method.getName(), method.getParameterTypes());@b@ annotation = getAnnotation(equivalentMethod, annotationType);@b@ }@b@ catch (NoSuchMethodException localNoSuchMethodException)@b@ {@b@ }@b@ if (annotation != null)@b@ break;@b@ }@b@@b@ return annotation;@b@ }@b@@b@ public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType)@b@ {@b@ Assert.notNull(clazz, "Class must not be null");@b@ Annotation annotation = clazz.getAnnotation(annotationType);@b@ if (annotation != null)@b@ return annotation;@b@@b@ for (Class ifc : clazz.getInterfaces()) {@b@ annotation = findAnnotation(ifc, annotationType);@b@ if (annotation != null)@b@ return annotation;@b@ }@b@@b@ if (!(Annotation.class.isAssignableFrom(clazz)))@b@ for (Annotation ann : clazz.getAnnotations()) {@b@ annotation = findAnnotation(ann.annotationType(), annotationType);@b@ if (annotation != null)@b@ return annotation;@b@ }@b@@b@@b@ Class superClass = clazz.getSuperclass();@b@ if ((superClass == null) || (superClass.equals(Object.class)))@b@ return null;@b@@b@ return ((TA)findAnnotation(superClass, annotationType));@b@ }@b@@b@ public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, Class<?> clazz)@b@ {@b@ Assert.notNull(annotationType, "Annotation type must not be null");@b@ if ((clazz == null) || (clazz.equals(Object.class)))@b@ return null;@b@@b@ return ((isAnnotationDeclaredLocally(annotationType, clazz)) ? clazz : @b@ findAnnotationDeclaringClass(annotationType, clazz.getSuperclass()));@b@ }@b@@b@ public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz)@b@ {@b@ Assert.notNull(annotationType, "Annotation type must not be null");@b@ Assert.notNull(clazz, "Class must not be null");@b@ boolean declaredLocally = false;@b@ label60: for (Annotation annotation : Arrays.asList(clazz.getDeclaredAnnotations())) {@b@ if (!(annotation.annotationType().equals(annotationType))) break label60;@b@ declaredLocally = true;@b@ break;@b@ }@b@@b@ return declaredLocally;@b@ }@b@@b@ public static boolean isAnnotationInherited(Class<? extends Annotation> annotationType, Class<?> clazz)@b@ {@b@ Assert.notNull(annotationType, "Annotation type must not be null");@b@ Assert.notNull(clazz, "Class must not be null");@b@ return ((clazz.isAnnotationPresent(annotationType)) && (!(isAnnotationDeclaredLocally(annotationType, clazz))));@b@ }@b@@b@ public static Map<String, Object> getAnnotationAttributes(Annotation annotation)@b@ {@b@ return getAnnotationAttributes(annotation, false);@b@ }@b@@b@ public static Map<String, Object> getAnnotationAttributes(Annotation annotation, boolean classValuesAsString)@b@ {@b@ Map attrs = new HashMap();@b@ Method[] methods = annotation.annotationType().getDeclaredMethods();@b@ for (Method method : methods)@b@ if ((method.getParameterTypes().length == 0) && (method.getReturnType() != Void.TYPE))@b@ try {@b@ Object value = method.invoke(annotation, new Object[0]);@b@ if (classValuesAsString)@b@ if (value instanceof Class) {@b@ value = ((Class)value).getName();@b@ }@b@ else if (value instanceof Class[]) {@b@ Class[] clazzArray = (Class[])value;@b@ String[] newValue = new String[clazzArray.length];@b@ for (int i = 0; i < clazzArray.length; ++i)@b@ newValue[i] = clazzArray[i].getName();@b@@b@ value = newValue;@b@ }@b@@b@ attrs.put(method.getName(), value);@b@ }@b@ catch (Exception ex) {@b@ throw new IllegalStateException("Could not obtain annotation attribute values", ex);@b@ }@b@@b@@b@ return attrs;@b@ }@b@@b@ public static Object getValue(Annotation annotation)@b@ {@b@ return getValue(annotation, "value");@b@ }@b@@b@ public static Object getValue(Annotation annotation, String attributeName)@b@ {@b@ Method method;@b@ try@b@ {@b@ method = annotation.annotationType().getDeclaredMethod(attributeName, new Class[0]);@b@ return method.invoke(annotation, new Object[0]);@b@ } catch (Exception localException) {@b@ }@b@ return null;@b@ }@b@@b@ public static Object getDefaultValue(Annotation annotation)@b@ {@b@ return getDefaultValue(annotation, "value");@b@ }@b@@b@ public static Object getDefaultValue(Annotation annotation, String attributeName)@b@ {@b@ return getDefaultValue(annotation.annotationType(), attributeName);@b@ }@b@@b@ public static Object getDefaultValue(Class<? extends Annotation> annotationType)@b@ {@b@ return getDefaultValue(annotationType, "value");@b@ }@b@@b@ public static Object getDefaultValue(Class<? extends Annotation> annotationType, String attributeName)@b@ {@b@ Method method;@b@ try@b@ {@b@ method = annotationType.getDeclaredMethod(attributeName, new Class[0]);@b@ return method.getDefaultValue();@b@ } catch (Exception localException) {@b@ }@b@ return null;@b@ }@b@}