首页

关于openspaces源码包AnnotationUtils注释工具类获取类及方法的注释通用处理

标签:openspaces,annotationUtils,注释工具类     发布时间:2018-05-27   

一、前言

关于gs-openspaces源码包org.openspaces.core.util.AnnotationUtils注解工具类,获取类或方法的注解对象getAnnotations、查找指定类或方法的注解findAnnotation、通过注解对象或类型获取对象值getValue或默认getDefaultValue,详情参见源码说明。

二、源码说明

package org.openspaces.core.util;@b@@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@    return BridgeMethodResolver.findBridgedMethod(method).getAnnotation(annotationType);@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@      cl = cl.getSuperclass();@b@      if (cl != null) { if (cl.equals(Object.class))@b@          break label66:@b@        try@b@        {@b@          Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());@b@          annotation = getAnnotation(equivalentMethod, annotationType);@b@        }@b@        catch (NoSuchMethodException ex) {@b@        }@b@      }@b@    }@b@    label66: 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@    Class[] arr$ = clazz.getInterfaces(); int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { Class ifc = arr$[i$];@b@      annotation = findAnnotation(ifc, annotationType);@b@      if (annotation != null)@b@        return annotation;@b@    }@b@@b@    if ((clazz.getSuperclass() == null) || (Object.class.equals(clazz.getSuperclass())))@b@      return null;@b@@b@    return findAnnotation(clazz.getSuperclass(), 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 : 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@    for (Annotation annotation : Arrays.asList(clazz.getDeclaredAnnotations()))@b@      if (annotation.annotationType().equals(annotationType)) {@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@    Map attrs = new HashMap();@b@    Method[] methods = annotation.annotationType().getDeclaredMethods();@b@    for (int j = 0; j < methods.length; ++j) {@b@      Method method = methods[j];@b@      if ((method.getParameterTypes().length == 0) && (method.getReturnType() != Void.TYPE))@b@        try {@b@          attrs.put(method.getName(), method.invoke(annotation, new Object[0]));@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 ex) {@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 ex) {@b@    }@b@    return null;@b@  }@b@}