Class AnnotationReflectionHelper

java.lang.Object
io.opentelemetry.instrumentation.api.annotation.support.AnnotationReflectionHelper

public class AnnotationReflectionHelper extends Object
Helper class for reflecting over annotations at runtime..
  • Method Details

    • forNameOrNull

      @Nullable public static Class<? extends Annotation> forNameOrNull(ClassLoader classLoader, String className)
      Returns the Class<? extends Annotation> for the name of the Annotation at runtime, otherwise returns null.
    • bindAnnotationElementMethod

      public static <A extends Annotation, T> Function<A,T> bindAnnotationElementMethod(MethodHandles.Lookup lookup, Class<? extends Annotation> annotationClass, String methodName, Class<T> returnClass) throws Throwable
      Binds a lambda of the functional interface Function<A extends Annotation, T> to the element of an Annotation class by name which, when invoked with an instance of that annotation, will return the value of that element.

      For example, calling this method as follows:

      
       Function<WithSpan, String> function = AnnotationReflectionHelper.bindAnnotationElementMethod(
           MethodHandles.lookup(),
           WithSpan.class,
           "value",
           String.class);
       

      is equivalent to the following Java code:

      
       Function<WithSpan, String> function = WithSpan::value;
       
      Type Parameters:
      A - the type of the annotation
      T - the type of the annotation element
      Parameters:
      lookup - the MethodHandles.Lookup of the calling method, e.g. MethodHandles.lookup()
      annotationClass - the Class of the Annotation
      methodName - name of the annotation element method
      returnClass - type of the annotation element
      Returns:
      Instance of Function<Annotation, T> that is bound to the annotation element method
      Throws:
      NoSuchMethodException - the annotation element method was not found
      Throwable - on failing to bind to the