title: Injecting Mouse Listener not working using ASM
body: <p>So here i am trying to inject my Mouse Listener class into this other class so i can do things with it. Below are my classes...</p>
  
  <p>This is my first class: THE class in which i want to inject my own listener into</p>
  
  <pre><code>package testing;
  
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseMotionListener;
  import java.awt.event.MouseWheelEvent;
  import java.awt.event.FocusEvent;
  import java.awt.event.FocusListener;
  import java.applet.Applet;
  
  public class MouseListenerTest extends Applet implements java.awt.event.MouseListener, MouseMotionListener, FocusListener
  {
      private String field;
      private int num;      //Just some fields why not
      private int test;
  
      /*
       * OTher methods and fields and stuff just generated for testing
       *
       */
  
      @Override
      public void mouseDragged(MouseEvent e)
      {
          //doStuff
  
      }
      @Override
      public void mouseMoved(MouseEvent e)
      {
          //doStuff
  
      }
      @Override
      public void mouseClicked(MouseEvent e)
      {
          //doStuff
  
      }
      @Override
      public void mouseEntered(MouseEvent e)
      {
          //doStuff
  
      }
      @Override
      public void mouseExited(MouseEvent e)
      {
          //doStuff
  
      }
      @Override
      public void mousePressed(MouseEvent e)
      {
          //doStuff
  
      }
  
      @Override
      public void mouseReleased(MouseEvent e)
      {
          //doStuff
  
      }
      @Override
      public void focusGained(FocusEvent arg0)
      {
          // TODO Auto-generated method stub
  
      }
      @Override
      public void focusLost(FocusEvent arg0)
      {
          // TODO Auto-generated method stub
  
      }
  }
  </code></pre>
  
  <p>This is my class that i made which is going to be used to override that classes mouselistener</p>
  
  <pre><code>    package testing;
  
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseMotionListener;
  import java.awt.event.MouseWheelEvent;
  
  
  public abstract class MouseListener extends FocusListener implements java.awt.event.MouseListener, MouseMotionListener
  {
      private int x;
      private int y;
  
      public abstract void _mouseClicked(MouseEvent paramMouseEvent);
  
      public abstract void _mouseDragged(MouseEvent paramMouseEvent);
  
      public abstract void _mouseEntered(MouseEvent paramMouseEvent);
  
      public abstract void _mouseExited(MouseEvent paramMouseEvent);
  
      public abstract void _mouseMoved(MouseEvent paramMouseEvent);
  
      public abstract void _mousePressed(MouseEvent paramMouseEvent);
  
      public abstract void _mouseReleased(MouseEvent paramMouseEvent);
  
      public abstract void _mouseWheelMoved(MouseWheelEvent paramMouseEvent);
  
      @Override
      public void mouseDragged(MouseEvent e)
      {
          _mouseDragged(e);
          e.consume();
      }
  
      @Override
      public void mouseMoved(MouseEvent e)
      {
          _mouseMoved(e);
          e.consume();
      }
  
      @Override
      public void mouseClicked(MouseEvent e)
      {
          _mouseClicked(e);
          e.consume();
  
      }
  
      @Override
      public void mouseEntered(MouseEvent e)
      {
          _mouseEntered(e);
          e.consume();
      }
  
      @Override
      public void mouseExited(MouseEvent e)
      {
          _mouseExited(e);
          e.consume();
      }
  
      @Override
      public void mousePressed(MouseEvent e)
      {
          x++;
          y++;
          _mousePressed(e);
          e.consume();
      }
  
      @Override
      public void mouseReleased(MouseEvent e)
      {
          _mouseReleased(e);
          e.consume();
      }
  
  }
  </code></pre>
  
  <p>And this is the class i am using to change the fields and methods. (NOTICE i just included a frame for it. This isn't actually how i am doing it. (with this class that is) i load the class and <code>.accept(cn, ...)</code> somewhere else where i pass it into this class) </p>
  
  <pre><code>package testing;
  
  import java.util.List;
  import java.util.ListIterator;
  
  import org.objectweb.asm.Opcodes;
  import org.objectweb.asm.tree.AbstractInsnNode;
  import org.objectweb.asm.tree.ClassNode;
  import org.objectweb.asm.tree.MethodInsnNode;
  import org.objectweb.asm.tree.MethodNode;
  
  public class Injector
  {
  
      public static ClassNode getClassNode()
      {return new ClassNode(Opcodes.ASM5);}
  
      public static ClassNode transformClass(ClassNode cn)
      {
          List&lt;MethodNode&gt; a = cn.methods;
          for(MethodNode mn : a)
          {
              if(mn.name.startsWith("mouse"))
              {
                  ClassNode superClass = getClassNode(); //loaded the classNode just there for emphasis
                  if(superClass!=null &amp;&amp; !superClass.superName.equals("testing/MouseListener"))
                  {
                      superClass.superName="testing/MouseListener";
                      System.out.println("Changed superclass of : "+superClass.name+" = "+superClass.superName);
                      List&lt;MethodNode&gt; c = superClass.methods;
                      for (MethodNode node : c)
                      {
                          if (node.name.equals("&lt;init&gt;"))
                          {
                              ListIterator&lt;AbstractInsnNode&gt; ili = node.instructions.iterator();
                              while (ili.hasNext())
                              {
                                  AbstractInsnNode ain = ili.next();
                                  if (ain.getOpcode() == Opcodes.INVOKESPECIAL)
                                  {
                                      MethodInsnNode min = (MethodInsnNode) ain;
                                      if (min.owner.equals("java/lang/Object"))
                                      {
                                          System.out.println("Changed initialization owner.");
                                          min.owner = "testing/MouseListener";
                                          break;
                                      }
                                  }
                              }
                              break;
                          }
                      }
                  }
                  mn.name="_"+mn.name;
                  System.out.println("Hooked method : "+mn.name);
              }
          }
          return cn;
      }
  }
  </code></pre>
  
  <p>Now here comes the actual question and problem that i am having:</p>
  
  <ol>
  <li>The injector works semi-properly it changes the methods names to the abstract ones in my Mouse Listener as it should but here comes the problem</li>
  <li>It does not change the IMPORTS on top so that therefore it knows that it is mine. This causes the program that i am injecting to throw a bunch of AbstractErrors as it does not override all the methods of Mouse Listener because i changed them.</li>
  </ol>
  
  <p>My question is does anyone know or can point me in the right direction on how to get the imports to change? so it would look like "testing.MouseListener" and etc..</p>