メソッドの長さの限界

メソッドが大きすぎる場合、Eclipseだとこんなエラーが出る。


The code of method [メソッド名] is exceeding the 65535 bytes limit


javacだとこんな感じ。


Hoge.java:2: コードが大きすぎます。
public void method() {
^
エラー 1 個


エラーが出ないぎりぎりのクラスファイルを解析すると、メソッドのバイトコード
このようになった(org.apache.bcel.util.Class2HTMLで確認)。

Byte offsetInstructionArgument
0iconst_0
1istore_1
2iinc%1 1
5iinc%1 1
〜略〜
65531iinc%1 1
65534return
というわけで、メソッドをバイトコードにしたときに
65535バイトを超えるとダメなのかなー?






ちなみに、試したコードはこんなのです。
# もちろん、手で書いたわけではない。


public class Hoge{
public void method() {
int num = 0;
num++;
num++;
〜略〜
num++;
num++; // num++が21844行ある。
}
}


すこし調べた感じだと、これが仕様っぽい。
http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#88659

The amount of code per non-native, non-abstract method is limited to 65536 bytes by the sizes of the indices in the exception_table of the Code attribute (§4.7.3), in the LineNumberTable attribute (§4.7.8), and in the LocalVariableTable attribute (§4.7.9).

65536まではOKだと読み取れるけど・・・。


それとも、こっちのCode Attributeの制限でひかっかるのかなぁ。
http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#40222

The value of the code_length item must be less than 65536.

うーん。