开发者

Why does javac checkcast arrays twice?

开发者 https://www.devze.com 2022-12-22 02:30 出处:网络
Examining bytecode, I\'ve noticed javac seems to duplicate checkcast instructions when casting to array types.

Examining bytecode, I've noticed javac seems to duplicate checkcast instructions when casting to array types.

Cast.java:
class Cast {
  void test(Object a) {
    Object[] b = (Object[])b;
  }
}

javap disassembly of the javac compiled version

void test(java.lang.Object);
  Code:
   0:   aload_1
   1:   checkcast   #2; //class "[Ljava/lang/Object;"
   4:   checkcast   #2; //class "[Ljava/lang/Object;"
   7:   astore_2
   8:   return

Testing jikes shows the expected single cast

void test(java.lang.Object);
  Code:
   0:   aload_1
   1:   checkcast   #10; //class "[Ljava/lang/Object;"
   4:   astore_2
   5:   return

checkcast is supposed to raise an exception if the object can't be treated as the requested type and otherwise does nothing, so I don't see why it might help to double the cast. I haven't looked at the JDK sources to see how it's produced, and if that he开发者_开发问答lps explain the why (maybe it's meant as a hint).


It is a known bug of javac. But it is mostly harmless.

0

精彩评论

暂无评论...
验证码 换一张
取 消