How can I tell the bytecode size of that method?
One way is to just add them up :-)
Each bytecode instruction consists of 1 byte for the primary instruction plus a fixed number of operand bytes.
A more practical way is to dump the classfile containing the bytecodes using javap -c
. The output includes byte offsets for each instruction.
Reference: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html
1) I can add ALOAD 0 ASTORE 4 as 4 bytes, but what do I do with with ARRAYLENGTH or INVOKESTATIC method-name?
The instructions are listed in Section 6.5 of the JVM spec - http://docs.oracle.com/javase/specs/jvms/se7/html/index.html
- Scroll down to the relevant part of the index.
- Click on the link for the instruction.
- Read the "format" and "description" to figure out how many bytes are used.
Following this procedure, I deduced that ARRAYLENGTH is 1 byte, and INVOKESTATIC is 3 bytes.
2) I tried to use javap but for some reason I get class not found (it's inside a jar and I passed -classpath filename.jar to javap but it didn't work).
Read the javap
manual entry again. It does work if you use it correctly. (Perhaps you didn't supply the fully-qualified classname in the correct format.)