Questions about type annotation on type casts.

Srikanth srikanth.adayapalam at oracle.com
Thu Dec 10 04:19:09 UTC 2015


Hello experts, JLS-meisters,

While working on https://bugs.openjdk.java.net/browse/JDK-8144168,
a few questions have surfaced regarding code generation for a type
annotated cast.

(1) If the expression that is being type-cast and the target type of the 
cast
are determined to be the same at compile time, javac does not emit a
checkcast instruction at all. This looks lossy to me.   ECJ always emits a
cast if there is a type annotation involved. Which approach is also
problematic since ...

(2) ... Since checkcast necessarily works only on reference operands
on the stack. So ECJ ends up generating bad code for

import java.lang.annotation.*;

public class X {
     @Target(ElementType.TYPE_USE)
     public @interface T {
     }

     public static void main(String[] args) {
         int i = (@T int) 10;

     }
}

attempting to emit a checkcast due to the policy of an annotated cast
always being emitted into the classfile.

(3) JVMS 4.7.20.1 states:

  ...

type_argument_target {
     u2 offset;
     u1 type_argument_index;
}

The value of the offset item specifies the code array offset of either the
bytecode instruction corresponding to the cast expression, the new bytecode
instruction corresponding to the new expression, ...

So what exactly intended to be the value of the offset item for a cast ?
ATM, javac seems to interpret this as the offset of the first 
instruction of chunk
of code generated to evaluate the the expression being typecast, while
ECJ makes the offset point to the checkcast instruction itself.

Thanks in advance for any clarifications.
Srikanth


More information about the compiler-dev mailing list