注入鼠标侦听器无法使用ASM工作

时间:2016-02-21 07:36:58

标签: java java-bytecode-asm

所以在这里我试图将我的Mouse Listener类注入到另一个类中,这样我就可以用它来做事了。以下是我的课程......

这是我的第一堂课:我想把自己的听众注入

的班级
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

    }
}

这是我创建的类,它将用于覆盖类mouselistener

    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();
    }

}

这是我用来改变字段和方法的类。 (注意我刚刚为它添加了一个框架。这实际上并不是我在做什么。(这个类是这样的)我加载了类,.accept(cn, ...)在其他地方我把它传递到这个类中)

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<MethodNode> 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 && !superClass.superName.equals("testing/MouseListener"))
                {
                    superClass.superName="testing/MouseListener";
                    System.out.println("Changed superclass of : "+superClass.name+" = "+superClass.superName);
                    List<MethodNode> c = superClass.methods;
                    for (MethodNode node : c)
                    {
                        if (node.name.equals("<init>"))
                        {
                            ListIterator<AbstractInsnNode> 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;
    }
}

现在出现了我遇到的实际问题和问题:

  1. 注入器工作半正确它将方法名称更改为我的鼠标侦听器中的抽象名称,但应该会出现问题
  2. 它不会改变顶部的IMPORTS,因此它知道它是我的。这导致我注入的程序抛出一堆AbstractErrors,因为它没有覆盖鼠标侦听器的所有方法,因为我更改了它们。
  3. 我的问题是,是否有人知道或可以指出我如何让​​进口改变的正确方向?所以它看起来像&#34; testing.MouseListener&#34;等等。

0 个答案:

没有答案