なんでー

仕様書の中に JVM の制限についてまとめた節があって、それによると

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).

だそうだ。メソッド長の 64KB 制限は、コードの長さを記録するフィールドが 2 バイトしかないせいだと思っていたんだけど、思い込みだったみたい。確かめてみるとコードの長さはちゃんと 4 バイトで表現されていた。ということは、例外ハンドラがなければ 232-1 バイトまでいけても良さそうなもんだが… 何か他に制約があるのかな。

あと、ぴったり 65536 バイトのメソッドを作ったら

$ java Method65536 
Exception in thread "main" java.lang.ClassFormatError: Invalid method Code length 65536 in class file Method65536

と言われて実行できなかった(65535 なら大丈夫)。これって Mac だから?