All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.appland.appmap.transform.annotations.AnnotationUtil Maven / Gradle / Ivy

There is a newer version: 1.27.1
Show newest version
package com.appland.appmap.transform.annotations;

import static com.appland.appmap.util.ClassUtil.safeClassForName;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;

import javassist.CtBehavior;

/**
 * Utility classes for accessing runtime Annotation information.
 */
public class AnnotationUtil {
  /**
   * Obtains the default {@code value()} from an Annotation.
   * @param behavior The declaring behavior
   * @param annotationClass The annotation class
   * @param defaultValue A default value to use if an error occurs
   * @return The resulting value of the Annotations {@code value()} method or the default value
   *         supplied.
   */
  public static Object getValue(CtBehavior behavior,
                                Class annotationClass,
                                Object defaultValue) {
    return getObject(behavior, annotationClass, "value", defaultValue);
  }

  public static Integer getPosition(CtBehavior behavior,
                                Class annotationClass,
                                Object defaultValue) {
    return (Integer)getObject(behavior, annotationClass, "position", defaultValue);
  }

  public static Object getObject(CtBehavior behavior,
                                Class annotationClass,
                                String annotationName,
                                Object defaultValue) {
    try {
      Object annotation = behavior.getAnnotation(annotationClass);
      if (annotation == null) {
        annotation = behavior.getDeclaringClass().getAnnotation(annotationClass);
      }

      if (annotation == null) {
        return defaultValue;
      }

      Method valueMethod = annotationClass.getMethod(annotationName);
      if (valueMethod == null) {
        return defaultValue;
      }

      return valueMethod.invoke(annotation);
    } catch (Exception e) {
      return defaultValue;
    }
  }

  @SuppressWarnings("unchecked")
  public static boolean hasAnnotation(String annotationName, AnnotatedElement elt) {
    Class annotation = safeClassForName(AnnotationUtil.class.getClassLoader(), annotationName);
    return annotation != null && elt.isAnnotationPresent((Class) annotation);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy