Search code examples
javajvmbytecodebytecode-manipulationjvm-bytecode

How can I load a reference from a local variable at an index which is unknown at compile time?


As far as I understood the JVM bytecode specification, aload index pushes a reference found at index onto the current stackframe. In my case, I need to aload a reference found at an index which is stored in a local variable - something like this: aload (iload 2).

(How) Is this possible?


Solution

  • This is not possible. Otherwise verifier would not be able to prove the correctness of the bytecode.

    To access values by index use array objects with the corresponding bytecodes, e.g. aaload.