Details
-
Bug
-
Resolution: Unresolved
-
P4
-
5.0, 7, 8
-
None
-
Fix Understood
Description
Reading from JLS 15.12.2.6:
"The result type of the chosen method is determined as follows: [...]
o Otherwise, if unchecked conversion was necessary for the method to be applicable then the result type is the erasure (§4.6) of the method's declared return type.[...]"
Moreover
"The exception types of the throws clause of the chosen method are determined as follows:[...]
* If unchecked conversion was necessary for the method to be applicable then the throws clause is composed of the erasure (§4.6) of the types in the method's declared throws clause."
which means that the following programs should *not* compile
Example 1.
class Test<X> {
X m(Class<X> c) {return null;}
X x = m((Class)String.class);
}
Example 2.
class Test {
<T extends Throwable> void foo(Class<T> c, T t) throws T {}
void test(Exception e) {
try {
foo((Class)String.class, e);
} catch (Exception t) {}
}
}
Example 3.
class Test<T extends Throwable> {
void foo(Class<T> c, T t) throws T {}
void test(Exception e) {
try {
new Test<Exception>().foo((Class)String.class, e);
} catch (Exception t) {}
}
}
"The result type of the chosen method is determined as follows: [...]
o Otherwise, if unchecked conversion was necessary for the method to be applicable then the result type is the erasure (§4.6) of the method's declared return type.[...]"
Moreover
"The exception types of the throws clause of the chosen method are determined as follows:[...]
* If unchecked conversion was necessary for the method to be applicable then the throws clause is composed of the erasure (§4.6) of the types in the method's declared throws clause."
which means that the following programs should *not* compile
Example 1.
class Test<X> {
X m(Class<X> c) {return null;}
X x = m((Class)String.class);
}
Example 2.
class Test {
<T extends Throwable> void foo(Class<T> c, T t) throws T {}
void test(Exception e) {
try {
foo((Class)String.class, e);
} catch (Exception t) {}
}
}
Example 3.
class Test<T extends Throwable> {
void foo(Class<T> c, T t) throws T {}
void test(Exception e) {
try {
new Test<Exception>().foo((Class)String.class, e);
} catch (Exception t) {}
}
}
Attachments
Issue Links
- relates to
-
JDK-6785112 Umbrella: issues with generic method type-inference
- Closed
-
JDK-8135087 Erasure for unchecked invocation happens after inference
- Open
-
JDK-7015422 15.9: Constructor 'throws' clause with type variable is not specified in class instance creation expression
- Closed
-
JDK-7015430 Incorrect thrown type determined for unchecked invocations
- Closed
-
JDK-6817013 Iterative for loop fails to find generics information of cast classes
- Closed