Using Array.newInstance still generates checkcast instructions

Vitaly Davidovich vitalyd at gmail.com
Tue Oct 27 17:23:48 UTC 2015


Hi,

I (intuitively) thought that using Array.newInstance() and specifying a
final class (e.g. String) would remove checkcasts in the caller.  However,
it appears that the check is still generated (at least on 8u51).  Why would
this be? It seems the JIT should know the true runtime type of the array,
and if accesses to it are inlined, the checkcast could be removed.  Am I
missing something?

e.g.

public final class ArrayList<T> {
    private final T[] _items;

    public ArrayList(Class<T> klass, int size) {
         _items = (T[])Array.newInstance(klass, size);
    }

    // rest omitted for brevity

    public T get(int index) { return _items[index]; }

}

ArrayList<String> strings = new ArrayList<>(String.class, 10);
String s = strings.get(0); // why is this checkcast not eliminated?


Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20151027/9e8db3d6/attachment.html>


More information about the hotspot-compiler-dev mailing list