Ramesh Subramanian
Ramesh Subramanian

Reputation: 1037

ASM Bytecode Method Parameter Values

How to view or access the method parameter values or Objects using ASM Byte Code?

Upvotes: 1

Views: 2735

Answers (2)

Eugene Kuleshov
Eugene Kuleshov

Reputation: 31795

Taking into account method parameter types, you could do something like that:

int off = (access | Opcodes.ACC_STATIC) == 0 ? 0 : 1;
int opcode = Type.getArgumentTypes(desc)[param + off].getOpcode(Opcodes.IALOAD);
mv.visitVarIns(opcode, param);
...

where param is method parameter number and access and desc are values you get from corresponding parameters of ClassVisitor.html#visitMethod.

Upvotes: 1

cHao
cHao

Reputation: 86506

Method arguments are the first few local variables. To access the first arg, the bytecode mnemonic looks like aload_0 or iload_0 or lload_0 etc, depending on the argument's type. For arguments past the fourth, you'd say aload 4 etc.

Note, the first argument to an instance method is a reference to this. So the first argument will be local #1, and you'd get it like aload_1 etc.

However you'd generate bytecode with the ASM stuff... do that. It looks like you'd say something akin to mv.visitVarInsn(ALOAD, 0);, where mv is your MethodVisitor. The 0 would be replaced with the local variable index.

Upvotes: 1

Related Questions