-
CSR
-
Resolution: Approved
-
P4
-
None
-
behavioral
-
minimal
-
Java API
-
SE
Summary
Method java.lang.reflect.Executable::getAnnotatedExceptionTypes
is not behaving according to its specification and should be synchronized with it. In particular the method's output is incorrect in some cases for inner class constructors if type annotations are applied to exception types.
Problem
Method java.lang.reflect.Executable::getAnnotatedExceptionTypes
is not in sync with its specification, in particular for inner class constructors. The method is shifting the type annotations applied to exception types to the right by one place. For example given the test below:
class Test {
@Retention(value = RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface Anno {}
class Exception1 extends Exception {}
class Exception2 extends Exception {}
class Exception3 extends Exception {}
class Inner {
public Inner() throws @Anno Exception1, Exception2, Exception3 {}
}
public static void main(String[] args) throws Exception {
foo(Inner.class.getDeclaredConstructor(Test.class));
}
static void foo(Executable c) {
for (AnnotatedType exceptionType : c.getAnnotatedExceptionTypes())
System.out.println(exceptionType);
}
}
// its output will be:
Test$Exception1
@Test$Anno() Test$Exception2
Test$Exception3
// while the correct output should be:
@Test$Anno() Test$Exception1
Test$Exception2
Test$Exception3
if the constructor above was declared as:
Inner() throws Exception1, @Anno Exception2, Exception3 {}
then our test's output would be:
Test$Exception1
Test$Exception2
@Test$Anno() Test$Exception3
Finally for a declaration like:
Inner() throws Exception1, Exception2, @Anno Exception3 {...}
then our test prints:
Test$Exception1
Test$Exception2
Test$Exception3
In other words the output of the method is partially correct, it returns the exceptions in the right position but the type annotations applied to them are off by one position to the right.
Solution
Synchronize method j.l.r.Executable::getAnnotatedExceptionTypes
with its specification. In other words, fix the current state which is not compatible with its specification. Fixing this issue will provoke a behavioral change as client code using j.l.r.Executable::getAnnotatedExceptionTypes
will get a different output for cases when there are type annotations applied to exception types declared in inner constructors. Client code not dealing with these cases will see no difference in the method's output.
Specification
As a reference, the related specification is:
/**
* Returns an array of {@code AnnotatedType} objects that represent the use
* of types to specify the declared exceptions of the method/constructor
* represented by this Executable. The order of the objects in the array
* corresponds to the order of the exception types in the declaration of
* the method/constructor.
*
* Returns an array of length 0 if the method/constructor declares no
* exceptions.
*
* @return an array of objects representing the declared
* exceptions of the method or constructor represented by this {@code
* Executable}
*/
which will be kept unchanged.
- csr of
-
JDK-8213905 reflection not working for type annotations applied to exception types in the inner class constructor
-
- Resolved
-