我正在尝试使用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('.', '/');
}
}
然而,在我这样做之后,当我尝试在Eureka Netflix库中使用java代理时,我遇到了weiered问题:
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 Client时,问题就出现了。目前我有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;";
答案 0 :(得分:2)
问题是您在使用ASM的常见类型解析器进行检测期间加载类。这样,您正在加载当前正在检测的类。一旦您的检测完成,JVM就会尝试定义类,但是之前已经定义了这个类,这是不可能的。
出于这个原因,您应该避免在检测期间加载类。