gpt4 book ai didi

java - 检测 ApachHttpClient 时出现 ASM 5.2 : Java. lang.linkage 错误

转载 作者:行者123 更新时间:2023-12-02 02:30:38 28 4
gpt4 key购买 nike

我正在尝试使用 ASM 5.2 来检测依赖关系。由于强制使用 JDK 1.8,我被迫在类编写器中使用 COMPUTE_FRAMES 选项。

当使用默认的 ClassWriter 时,它无法找到某些类并抛出以下异常

java.lang.RuntimeException: java.lang.ClassNotFoundException: XXX
at org.objectweb.asm.ClassWriter.getCommonSuperClass(Unknown Source)
at org.objectweb.asm.ClassWriter.a(Unknown Source)
at org.objectweb.asm.Frame.a(Unknown Source)
at org.objectweb.asm.Frame.a(Unknown Source)
at org.objectweb.asm.MethodWriter.visitMaxs(Unknown Source)
.....

为了解决此问题,我通过创建自定义 classWriter 类并向其传递来自 transform() 的类加载器来重写 ClassWriter 的 getCommonSuperClass() 方法。我遵循了该线程的建议: ASM 5.0.3 With Java 1.8 incorrect maxStack with Java.lang.VerifyError: Operand stack overflow

这是 CustomClassWriter 代码:

class CustomClassWriter extends ClassWriter {


ClassLoader classLoader;

public CustomClassWriter(int writerFlag, ClassLoader loader)
{
super(writerFlag);
this.classLoader = loader;
System.out.println("Trace: Registering class loader from Code injector");
}

/**
* This method returns common super class for both the classes. If no super class
* is present it returns java/lang/Object class.
* @param className1
* @param className2
* @return The String for the common super class of both the classes
*/
protected String getCommonSuperClass(String className1, String className2)
{
Class class1;
Class class2;
//System.out.println("Trace: Default Class loader :" + getClass().getClassLoader().toString());
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
System.out.println("Trace: Context class loader is " + contextClassLoader.toString());
System.out.println("Trace: Loaded class loaded :" + classLoader.toString());

try
{
class1 = Class.forName(className1.replace('/', '.'), false, this.classLoader);
class2 = Class.forName(className2.replace('/', '.'), false, this.classLoader);
System.out.println("Trace: Class 1" + class1.toString());
System.out.println("Trace: Class 2" + class2.toString());
}
catch (Exception th) {
throw new RuntimeException(th.getMessage());
}

if (class1.isAssignableFrom(class2)) {
return className1;
}
if (class2.isAssignableFrom(class1)) {
return className2;
}

if ((class1.isInterface()) || (class2.isInterface())) {
return "java/lang/Object";
}

do {
class1 = class1.getSuperclass();
}
while (!(class1.isAssignableFrom(class2)));
return class1.getName().replace('.', '/');
}


}

但是,完成此操作后,当我尝试将 java 代理与 Eureka Netflix 库一起使用时,我遇到了奇怪的问题:

Caused by: java.lang.LinkageError: loader (instance of  sun/misc/Launcher$AppClassLoader): attempted  duplicate class definition for name: "org/apache/http/impl/client/AbstractHttpClient"
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_144]
at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_144]
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_144]
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) ~[na:1.8.0_144]
at java.net.URLClassLoader.access$100(URLClassLoader.java:73) ~[na:1.8.0_144]
at java.net.URLClassLoader$1.run(URLClassLoader.java:368) ~[na:1.8.0_144]
at java.net.URLClassLoader$1.run(URLClassLoader.java:362) ~[na:1.8.0_144]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_144]
at java.net.URLClassLoader.findClass(URLClassLoader.java:361) ~[na:1.8.0_144]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_144]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_144]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_144]
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_144]
at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_144]
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_144]
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) ~[na:1.8.0_144]
at java.net.URLClassLoader.access$100(URLClassLoader.java:73) ~[na:1.8.0_144]
at java.net.URLClassLoader$1.run(URLClassLoader.java:368) ~[na:1.8.0_144]
at java.net.URLClassLoader$1.run(URLClassLoader.java:362) ~[na:1.8.0_144]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_144]
at java.net.URLClassLoader.findClass(URLClassLoader.java:361) ~[na:1.8.0_144]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_144]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_144]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_144]
at com.sun.jersey.client.apache4.ApacheHttpClient4.createDefaultClientHandler(ApacheHttpClient4.java:236) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
at com.sun.jersey.client.apache4.ApacheHttpClient4.create(ApacheHttpClient4.java:181) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
at com.netflix.discovery.shared.transport.jersey.EurekaJerseyClientImpl.<init>(EurekaJerseyClientImpl.java:52) ~[eureka-client-1.4.12.jar:1.4.12]
... 58 common frames omitted

我不确定这里的确切问题是什么。类加载器如何获取不同的重复定义。有什么方法可以解决这个问题?任何帮助将不胜感激。

编辑:除了上述上下文之外,我已经将 http 客户端依赖项重新打包到自定义命名空间中,以便代理不会检测我们自己的调用。

当我尝试检测 Apache HTTP 客户端时,问题就出现了。目前我有 Apache HTTP Client 4.2 和 4.3 的检测,当我通过删除 4.2 的检测来启动应用程序时,一切正常。

以下是我尝试使用的定义:

private final static String HTTP_CLIENT_43_CLASS_NAME = "org/apache/http/impl/client/InternalHttpClient";
private final static String HTTP_CLIENT_METHOD_43_NAME = "doExecute";
private final static String HTTP_CLIENT_METHOD_43_SIGNATURE = "(Lorg/apache/http/HttpHost;Lorg/apache/http/HttpRequest;Lorg/apache/http/protocol/HttpContext;)Lorg/apache/http/client/methods/CloseableHttpResponse;";

private final static String HTTP_CLIENT_42_CLASS_NAME = "org/apache/http/impl/client/AbstractHttpClient";
private final static String HTTP_CLIENT_METHOD_42_NAME = "execute";
private final static String HTTP_CLIENT_METHOD_42_SIGNATURE = "(Lorg/apache/http/HttpHost;Lorg/apache/http/HttpRequest;Lorg/apache/http/protocol/HttpContext;)Lorg/apache/http/HttpResponse;";

最佳答案

问题是您在检测期间使用 ASM 的通用类型解析器加载类。这样,您就可以加载当前正在检测的类。一旦您的检测完成,JVM 就会尝试定义该类,但正如之前定义的那样,这不再可能。

因此,您应该避免在检测期间加载类。

关于java - 检测 ApachHttpClient 时出现 ASM 5.2 : Java. lang.linkage 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47169763/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com