Issue when using LocalVariablesSorter
Hello everyone!
I'm trying to use LocalVariablesSorter
to transform a function in such a way local variable indexes are used consecutively. I checked the test cases but I have no luck to make it work. My code yields exactly the same input bytecode. Kindly, I'd like to ask you if you could tell me what I'm missing.
Bytecode:
public static int foo(long, int);
Code:
0: lload_0
1: iload_2
2: invokestatic #3 // Method java/lang/Long.toString:(JI)Ljava/lang/String;
5: astore_3
6: aload_3
7: invokestatic #4 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
10: ireturn
My intent is to generate a new function in which we use local variable indexes: 0, 1 and 2 instead of 0, 2 and 3.
ClassReader cr = new ClassReader(stream);
ClassWriter cw = new ClassWriter(cr, 0);
ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM8, cw){
@Override
public MethodVisitor visitMethod(
final int access,
final String name,
final String descriptor,
final String signature,
final String[] exceptions) {
MethodVisitor methodVisitor =
super.visitMethod(access, name, descriptor, signature, exceptions);
return new LocalVariablesSorter(access, descriptor, methodVisitor);
}
};
cr.accept(classVisitor, ClassReader.EXPAND_FRAMES);
FileUtils.writeByteArrayToFile(new File("mypath/..."), cw.toByteArray());
Best, Manuel.