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

(ann) Class.getAnnotation() throws an ArrayStoreException when the annotation class not present

    Details

    • Subcomponent:
    • CPU:
      generic, x86
    • OS:
      generic, windows_7

      Description

      SYNOPSIS
      --------
      Class.getAnnotation() throws an ArrayStoreException when the annotation class is not present in the classpath

      OPERATING SYSTEM
      ----------------
      All (tested on Windows and Solaris)

      FULL JDK VERSION
      ----------------
      JDK 5 and greater, including JDK 8
      Note: The ArrayStoreException begin with 5.0u6 (See CR 6322301 fix).

      PROBLEM DESCRIPTION from LICENSEE
      ---------------------------------
      A call to Class.getAnnotation() throws the following java.lang.ArrayStoreException when an annotation's Class is not present on the classpath. The following stack is from JDK8-b39:

      Exception in thread "main" java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
              at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:677)
              at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:484)
              at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:308)
              at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:242)
              at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:88)
              at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70)
              at java.lang.Class.initAnnotationsIfNecessary(Class.java:3093)
              at java.lang.Class.getAnnotation(Class.java:3052)
              at AnnotationTest.doTest(AnnotationTest.java:9)
              at AnnotationTest.main(AnnotationTest.java:5)

      The API specification for java.lang.Class states that Class.getAnnotation() returns this element's annotation for the specified type if such an annotation is present, else null. ArrayStoreException is not a checked Exception so it doesn't have to be declared, but it effectively prevents the API from following the specification - either the annotation should be returned, or null. Currently neither happens.

      The application could catch the ArrayStoreException and handle it, but Licensee prefers it to be dealt with in the JDK's internal implementation.

      TESTCASE AND REPRODUCTION INSTRUCTIONS
      --------------------------------------
      Attached

      WORKAROUND
      ----------
      Catch the undocumented ArrayStoreException and handle it.

        Issue Links

          Activity

          Hide
          bshannon Bill Shannon added a comment -
          You may be right. The reason to ignore the missing class is to be consistent with other cases where missing *annotation* classes are silently ignored. But yes, this case is different.

          It's not clear the current spec covers this case, so it should probably be enhanced as well.
          Show
          bshannon Bill Shannon added a comment - You may be right. The reason to ignore the missing class is to be consistent with other cases where missing *annotation* classes are silently ignored. But yes, this case is different. It's not clear the current spec covers this case, so it should probably be enhanced as well.
          Hide
          jlahoda Jan Lahoda added a comment -
          8-defer-request: The possible fix(es) will need more discussion, and there are compatibility concerns around them. It would be preferable to do behavioural change like this earlier in a major release so that there is more time to learn about possible consequence.
          Show
          jlahoda Jan Lahoda added a comment - 8-defer-request: The possible fix(es) will need more discussion, and there are compatibility concerns around them. It would be preferable to do behavioural change like this earlier in a major release so that there is more time to learn about possible consequence.
          Hide
          ssides Steve Sides added a comment -
          8-defer-SQE-OK - This is not a regression and not clearly a violation of the api spec. The "missing" class is not an annotation but a class with is the value of an annotation.
          This test, http://hg.openjdk.java.net/jdk8/tl/jdk/file/60e3cdbe8cdf/test/java/lang/annotation/Missing/MissingTest.java, has a tests case on line 122 which covers this situation.
          Show
          ssides Steve Sides added a comment - 8-defer-SQE-OK - This is not a regression and not clearly a violation of the api spec. The "missing" class is not an annotation but a class with is the value of an annotation. This test, http://hg.openjdk.java.net/jdk8/tl/jdk/file/60e3cdbe8cdf/test/java/lang/annotation/Missing/MissingTest.java, has a tests case on line 122 which covers this situation.
          Hide
          sogoel Sonali Goel added a comment -
          One more comment:
          Annotation a = this.getClass().getAnnotation(Annotation.class);
          System.out.println("Annotation: " + a);

          Annotation.class is present in the path; its the class value member (Missing.class) that is removed from the test dir.
          So, I would expect that 'a' will get an Annotation object, however, when I try to use 'a' to get its members, here, Missing.class then an exception is fine.
          Show
          sogoel Sonali Goel added a comment - One more comment: Annotation a = this.getClass().getAnnotation(Annotation.class); System.out.println("Annotation: " + a); Annotation.class is present in the path; its the class value member (Missing.class) that is removed from the test dir. So, I would expect that 'a' will get an Annotation object, however, when I try to use 'a' to get its members, here, Missing.class then an exception is fine.
          Hide
          maxelsso Mathias Axelsson (Inactive) added a comment -
          Release team: Approved for deferral. I couldn't find where you wanted to defer this so I've moved it to 9. If that was incorrect then please update FixVersion
          Show
          maxelsso Mathias Axelsson (Inactive) added a comment - Release team: Approved for deferral. I couldn't find where you wanted to defer this so I've moved it to 9. If that was incorrect then please update FixVersion

            People

            • Assignee:
              jlahoda Jan Lahoda
              Reporter:
              dkorbel David Korbel
            • Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

              Dates

              • Due:
                Created:
                Updated:
                Imported:
                Indexed: