Search code examples
javavariableslocalbytecodejava-bytecode-asm

How to create a local variable with ASM?


I'm trying to patch a class with ASM. I need to add some logic in a function. This logic needs a new local variable. Here is what I've done:

class CreateHashTableMethodAdapter extends MethodAdapter {
    @Override  
    public void visitMethodInsn(int opcode, String owner,String name, String desc){
        System.out.println(opcode + "/" + owner + "/" + name + "/" + desc);

        if(opcode == Opcodes.INVOKESPECIAL &&
                "javax/naming/InitialContext".equals(owner) &&
                "<init>".equals(name) &&
                "()V".equals(desc)){

            System.out.println("In mod");
            //  83: new #436; //class javax/naming/InitialContext
            //  86: dup


            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "javax/naming/InitialContext", "<init>", "()V");
            mv.visitVarInsn(Opcodes.ASTORE, 1);

            Label start_patch = new Label();
            Label end_patch = new Label();

            mv.visitLabel(start_patch);



            mv.visitTypeInsn(Opcodes.NEW,"java/util/Hashtable");
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/Hashtable", "<init>", "()V");  
            mv.visitVarInsn(Opcodes.ASTORE,9);

            // ........ sNip ..........

            mv.visitLabel(end_patch);
            mv.visitLocalVariable("env","Ljava/util/Hashtable;",null,start_patch,end_patch,9);

            //    127:  astore_1

        }
        else {
            mv.visitMethodInsn(opcode, owner, name, desc);
        }
    }
}

When I run this method adapter against CheckClassAdapter it states:

org.objectweb.asm.tree.analysis.AnalyzerException: Error at instruction 51: Trying to access an inexistant local variable 9
.... sNiP ....
00050 R R . . .  : R R  :     INVOKESPECIAL java/util/Hashtable.<init> ()V
00051 R R . . .  : R  :     ASTORE 9

I think I misuse the visitLocalVariable, but I can not find out where I'm supposed to call it.

When I javap generated bytecode (without checking), I get the following local variables table:

LocalVariableTable: 
Start  Length  Slot  Name   Signature
91      40      9    env       Ljava/util/Hashtable;
0      343      0    this       Lpmu/jms/ServerJMS;
132      146      1    initialContext       Ljavax/naming/InitialContext;
153      125      2    topicConnectionFactory       Ljavax/jms/TopicConnectionFactory;
223      55      3    topic       Ljavax/jms/Topic;
249      29      4    topicSubscriber       Ljavax/jms/TopicSubscriber;
279      55      1    ex       Ljava/lang/Exception;
281      53      2    codeMessage       I
289      45      3    params       Lpmu/data/Parameters;
325      9      4    messageError       Ljava/lang/String;

As you may notice, my variable is here but topmost ?! Any idea ?


Solution

  • One convenient way to create new local variables is to extend LocalVariablesSorter instead of MethodAdapter. Then you can allocate local variables as needed using newLocal() without interfering with existing variables. See section 3.3.3 of the ASM 4.0 A Java bytecode engineering library on the ASM homepage for details.