简体   繁体   English

自定义类加载器和Java代理

[英]Custom Classloader And Java Agent

I want to modifly the class in app which is running. 我想修改正在运行的应用程序中的类。 So I use java agent to do this. 因此,我使用Java代理来执行此操作。 My java code like this: 我的java代码是这样的:

    /**
 * 
 * @param args
 * @param inst
 */
public static void agentmain(String args, Instrumentation inst) {
    try {
        inst.addTransformer(new GameClassFileTransformer(), true);
        parseFiles();
        List<Class<?>> retransformClasses = new ArrayList<>();
        for (JarClassInfo jarClassInfo : GameAgentMain.bugFiles.values()) {
            retransformClasses.add(Class.forName(jarClassInfo.getClassName()));
        }
        Class<?>[] classes = new Class[retransformClasses.size()];
        retransformClasses.toArray(classes);
        inst.retransformClasses(classes);
    } catch (Exception e) {
        LogUtil.error(e);
    }
}

The JarClassInfo is a class about the class I want to reload.There hava two fields in JarClassInfo ,first is className,second is bytecode(java type is byte[]),The classname is which class want to reload.But the class is loaded by JarClassInfo是有关我要重新加载的类的类。JarClassInfo中有两个字段,第一个是className,第二个是字节码(java类型是byte []),该类名是要重新加载的类。但是要加载该类通过

custom classLoad 自定义classLoad

.When the app run at : 当应用在以下位置运行时:

retransformClasses.add(Class.forName(jarClassInfo.getClassName())); retransformClasses.add(的Class.forName(jarClassInfo.getClassName()));

That is a exception be thrown: 这是一个例外:

Exception in thread "Attach Listener" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:382)
        at sun.instrument.InstrumentationImpl.loadClassAndCallAgentmain(InstrumentationImpl.java:407)
Caused by: java.lang.ClassFormatError: Incompatible magic value 2329931675 in class file com/jingqi/game/module/chat/command/SendChatMsgCMD
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$1(URLClassLoader.java:409)
        at java.net.URLClassLoader$2.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$2.run(URLClassLoader.java:1)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:190)
        at com.jingqi.game.agent.GameAgentMain.agentmain(GameAgentMain.java:60)
        ... 6 more

My custom classloader(EncrypClassLoader) is used to decode my class. 我的自定义类加载器(EncrypClassLoader)用于解码我的类。 So the methd call stack indicate sun.misc.Launcher$AppClassLoader to load SendChatMsgCMD .The SendChatMsgCMD is not a normal class file(because I encrypt this class file). 因此,方法调用堆栈指示sun.misc.Launcher $ AppClassLoader加载SendChatMsgCMD .SendChatMsgCMD不是普通的类文件(因为我加密了该类文件)。

So ,what method can be solved 。 那么,什么方法可以解决。

Your custom classloader isn't doing what it's supposed to do: decrypt classes. 您的自定义类加载器没有执行应做的事情:解密类。
That's why you're​ getting​ the java.lang.ClassFormatError about the invalid magic value. 这就是为什么您会收到有关无效魔术值的java.lang.ClassFormatError的原因。

EDIT: you need to also realise that Class.forName() will load the class referred by the classname argument using the current class's classloader. 编辑:您还需要意识到Class.forName()将使用当前类的类加载器加载由类名参数引用的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM