title: How to create a local variable with ASM?
body: <p>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:</p>
  
  <pre><code>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 &amp;&amp;
                  "javax/naming/InitialContext".equals(owner) &amp;&amp;
                  "&lt;init&gt;".equals(name) &amp;&amp;
                  "()V".equals(desc)){
  
              System.out.println("In mod");
              //  83: new #436; //class javax/naming/InitialContext
              //  86: dup
  
  
              mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "javax/naming/InitialContext", "&lt;init&gt;", "()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", "&lt;init&gt;", "()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);
          }
      }
  }
  </code></pre>
  
  <p>When I run this method adapter against <code>CheckClassAdapter</code> it states:</p>
  
  <pre><code>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.&lt;init&gt; ()V
  00051 R R . . .  : R  :     ASTORE 9
  </code></pre>
  
  <p>I think I misuse the visitLocalVariable, but I can not find out where I'm supposed to call it.</p>
  
  <p>When I javap generated bytecode (without checking), I get the following local variables table:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>As you may notice, my variable is here but topmost ?!
  Any idea ?</p>