Esempio n. 1
0
 /**
  * Get meta annotation from an annotation
  *
  * @param store the annotation which annotated by meta annotation
  * @param clazz the meta annotation
  * @return if store has annotated by meta annotation, return that annotation, otherwise, return
  *     null
  */
 public static <T extends Annotation> T getMetaAnnotation(Annotation store, Class<T> clazz) {
   ClassType annoClass = TypeOracle.Instance.getClassType(store.annotationType());
   if (annoClass != null) {
     return ReflectionUtils.getAnnotation(annoClass.getAnnotations(), clazz);
   }
   return null;
 }
Esempio n. 2
0
  /**
   * Get value from annotation which named "methodName" i.e: Annotation(value="abc")
   *
   * @param annotation annotation
   * @param methodName "value"
   * @return "abc"
   */
  public static Object getAnnotationValueByName(Annotation annotation, String methodName) {
    ClassType type = TypeOracle.Instance.getClassType(annotation.annotationType());
    if (type == null) reflectionRequired(annotation.annotationType().getName(), "");

    Method method = type.findMethod(methodName);
    return method.invoke(annotation);
  }
Esempio n. 3
0
  /**
   * Get the full description of a class by using reflection
   *
   * @param clazz
   * @return
   */
  public static String getDescription(Class<?> clazz) {
    ClassType type = TypeOracle.Instance.getClassType(clazz);
    if (type == null) return clazz.getName() + ": Not Reflection Information available.";

    StringBuilder sb = new StringBuilder();
    printAnnotations(type, sb);
    sb.append(type.getName()).append("\n");
    sb.append("\n");
    sb.append("Fields:").append("\n");
    for (Field field : type.getFields()) {
      printAnnotations(field, sb);
      sb.append(field.getTypeName()).append(" ").append(field.getName()).append("\n");
    }

    sb.append("\n");
    if (type.findConstructor() != null) {
      sb.append("Constructor:").append("\n");
      sb.append(type.findConstructor().toString()).append("\n");
    } else {
      sb.append("No default Contructor\n");
    }

    sb.append("\n");
    sb.append("Methods:").append("\n");
    for (Method method : type.getMethods()) {
      printAnnotations(method, sb);
      sb.append(method.toString()).append("\n");
    }

    return sb.toString();
  }
Esempio n. 4
0
  public static boolean checkReflection(String className) {
    boolean result = TypeOracle.Instance.getClassType(className) != null;

    if (!result) ReflectionUtils.reflectionRequired(className, "");

    return result;
  }
Esempio n. 5
0
  /**
   * Find annotation from array of annotations
   *
   * @param annos the array of annotations
   * @param clazz the class of annotation
   * @return the annotation which meet clazz
   */
  public static <T extends Annotation> T getAnnotation(Annotation[] annos, Class<T> clazz) {
    ClassType classType = TypeOracle.Instance.getClassType(clazz);
    for (Annotation anno : annos) {
      if (anno.annotationType().getName() == classType.getName()) return (T) anno;
    }

    return null;
  }
Esempio n. 6
0
  public static String annotationToString(Annotation anno) {
    StringBuilder sb = new StringBuilder();

    sb.append(anno.annotationType().getName()).append("(");
    ClassType type = TypeOracle.Instance.getClassType(anno.annotationType());
    for (Method method : type.getMethods()) {
      sb.append(method.getName()).append("=").append(method.invoke(anno)).append(";");
    }
    sb.append(")");

    return sb.toString();
  }
Esempio n. 7
0
  public static Map<String, Object> getAnnotationValues(Annotation annotation) {
    ClassType type = TypeOracle.Instance.getClassType(annotation.annotationType());
    if (type == null) reflectionRequired(annotation.annotationType().getName(), "");

    Map<String, Object> result = new HashMap<String, Object>();

    Method[] methods = type.getMethods();
    for (Method method : methods) {
      result.put(method.getName(), getAnnotationValueByName(annotation, method.getName()));
    }

    return result;
  }
Esempio n. 8
0
  /**
   * return true if "classToTest" is assignable to "parentClass"
   *
   * @param parentClass
   * @param classToTest
   * @return
   */
  public static boolean isAssignable(Class<?> parentClass, Class<?> classToTest) {
    checkReflection(classToTest);

    ClassType typeToTest = TypeOracle.Instance.getClassType(classToTest);

    if (testAssignableWithoutSuper(parentClass, typeToTest)) return true;

    for (ClassType type : typeToTest.getImplementedInterfaces()) {
      if (isAssignable(parentClass, type.getDeclaringClass())) return true;
    }

    ClassType parentToTest = typeToTest.getSuperclass();
    while (parentToTest != null) {
      if (isAssignable(parentClass, parentToTest.getDeclaringClass())) return true;

      parentToTest = parentToTest.getSuperclass();
    }

    return false;
  }
Esempio n. 9
0
 public ClassType getAsClassType(String name) throws ReflectionRequiredException {
   String value = getValue(name);
   return TypeOracle.Instance.getClassType(value);
 }
Esempio n. 10
0
  /**
   * Get Annotation.
   *
   * <p>@Entity
   *
   * <p>public class Abc{}
   *
   * <p>getAnnotation(Abc.class, Entity.class) will return Entity annotation
   *
   * @param <T>
   * @param clazz
   * @param annotationClass
   * @return
   */
  public static <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> annotationClass) {
    ClassType type = TypeOracle.Instance.getClassType(clazz);
    if (type == null) reflectionRequired(clazz);

    return type.getAnnotation(annotationClass);
  }
Esempio n. 11
0
  /**
   * Check clazz to see if it have reflection information if not, raise a
   * ReflectionRequiredException
   *
   * @param clazz
   */
  public static void checkReflection(Class<?> clazz) {
    boolean result = TypeOracle.Instance.getClassType(clazz) != null;

    if (!result) ReflectionUtils.reflectionRequired(clazz.getName(), "");
  }