Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8038784

Access to annotation on Methods is clumsy and opaque.

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Unresolved
    • P4
    • None
    • 9
    • core-libs
    • generic
    • generic

    Description

      A DESCRIPTION OF THE REQUEST :
      Java lacks an elegant way to access the annotations for the current method significantly reducing usefulness of these.


      JUSTIFICATION :
      In contrast it is relatively easy to access annotation on classes. e.g.

      final Class<? extends AnnotatedClass> annotatedClass = this.getClass();
      final Annotation annotation = annotatedClass.getAnnotation(MyAnnotation.class);


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -

      Method method = System.instructionPointer().getMethodName()
      MyAnnotation annotation = method .getAnnotation(MyAnnotation.class);
      MyAnnotationValueType value = annotation.attribute();

      --- and/or ---

      ExecutionContext executionContext = Thread.currentThread().executionContext().
      Method method = executionContext.getCurrentMethod()
      MyAnnotation annotation = method .getAnnotation(MyAnnotation.class);
      MyAnnotationValueType value = annotation.attribute();
      // or perhaps the fill list of methods.
      Method[] methods = executionContext.getMethodStack()

      MyAnnotation annotation = method .getAnnotation(MyAnnotation.class);
      MyAnnotationValueType value = annotation.attribute();


      CUSTOMER SUBMITTED WORKAROUND :

      The following is a minimal code example or what is currently needed to get annotations for the current method. It's functionality is opaque, converluted and inelegant.

          public static String getTestName() {

              StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

              Method testMethod = findTestMethod(stackTrace);

              AutomatedTestName automatedTestName = testMethod
                      .getAnnotation(AutomatedTestName.class);

              return automatedTestName.value() == null ? EXPECTED_TEST_NAME
                      : automatedTestName.value();
          }

          /**
           * Find test method.
           *
           * @param stackTrace the stack trace
           * @return the method
           */
          private static Method findTestMethod(final StackTraceElement[] stackTrace) {
              Method testMethod = null;

              try {
                  for (StackTraceElement stackTraceElement : stackTrace)
                  {
                      String candidateClassName = stackTraceElement.getClassName();
                      Class<?> candidateClass = Class.forName(candidateClassName);

                      if (candidateClass
                              .isAnnotationPresent(AutomatedTestScript.class)) {
                          Class<?> anAnnotatedClass = candidateClass;

                          String candidateMethodName = stackTraceElement
                                  .getMethodName();

                          Method[] methods = anAnnotatedClass.getMethods();
                          for (Method method : methods) {
                              if (method.getName().equals(candidateMethodName))
                              {
                                  if (method.isAnnotationPresent(Test.class))
                                  {
                                      if (method
                                              .isAnnotationPresent(AutomatedTestName.class))
                                      {
                                          testMethod = method;
                                      }
                                  }
                              }
                          }
                      }
                  }
              } catch (ClassNotFoundException classNotFoundException) {
                  classNotFoundException.printStackTrace();
                  throw new RuntimeException(classNotFoundException);
              } catch (SecurityException securityException) {
                  securityException.printStackTrace();
                  throw new RuntimeException(securityException);
              }
              return testMethod;
          }


      Attachments

        Activity

          People

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: