所以在这里我试图将我的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;
}
}
现在出现了我遇到的实际问题和问题:
我的问题是,是否有人知道或可以指出我如何让进口改变的正确方向?所以它看起来像&#34; testing.MouseListener&#34;等等。