No unchecked cast is issued here:
import java.lang.invoke.VarHandle;
class VarHandleCast<V> {
VarHandle vh;
V method(Object obj) {
return (V)vh.getAndSet(this, obj);
}
}
The JLS says (15.12.3):
> The compile-time result is determined as follows:
>
> If the signature polymorphic method is either void or has a return type other than Object, the compile-time result is the result of the invocation type of the compile-time declaration (§15.12.2.6).
>
> Otherwise, if the method invocation expression is an expression statement, the compile-time result is void.
>
> Otherwise, if the method invocation expression is the operand of a cast expression (§15.16), the compile-time result is the erasure of the type of the cast expression (§4.6).
>
> Otherwise, the compile-time result is the signature polymorphic method's return type, Object.
So, in the above, the result type is Object. And a cast from Object to V is unchecked. A warning should be issued here.
import java.lang.invoke.VarHandle;
class VarHandleCast<V> {
VarHandle vh;
V method(Object obj) {
return (V)vh.getAndSet(this, obj);
}
}
The JLS says (15.12.3):
> The compile-time result is determined as follows:
>
> If the signature polymorphic method is either void or has a return type other than Object, the compile-time result is the result of the invocation type of the compile-time declaration (§15.12.2.6).
>
> Otherwise, if the method invocation expression is an expression statement, the compile-time result is void.
>
> Otherwise, if the method invocation expression is the operand of a cast expression (§15.16), the compile-time result is the erasure of the type of the cast expression (§4.6).
>
> Otherwise, the compile-time result is the signature polymorphic method's return type, Object.
So, in the above, the result type is Object. And a cast from Object to V is unchecked. A warning should be issued here.
- csr for
-
JDK-8343733 Missing unchecked cast warning in polymorphic method call
-
- Closed
-
- links to
-
Commit(master) openjdk/jdk/cc2acd14
-
Review(master) openjdk/jdk/21914