mirror of
https://gitee.com/arthas/arthas.git
synced 2024-12-04 05:09:54 +08:00
support jdk 11
This commit is contained in:
parent
e330bdc2f6
commit
637f2b53a9
@ -0,0 +1,48 @@
|
||||
package com.taobao.arthas.bytekit.utils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author hengyunabc 2017-10-12
|
||||
*
|
||||
*/
|
||||
public class ClassLoaderUtils {
|
||||
@SuppressWarnings({ "restriction", "unchecked" })
|
||||
public static URL[] getUrls(ClassLoader classLoader) {
|
||||
if (classLoader instanceof URLClassLoader) {
|
||||
return ((URLClassLoader) classLoader).getURLs();
|
||||
}
|
||||
|
||||
// jdk9
|
||||
if (classLoader.getClass().getName().startsWith("jdk.internal.loader.ClassLoaders$")) {
|
||||
try {
|
||||
Field field = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
field.setAccessible(true);
|
||||
Unsafe unsafe = (Unsafe) field.get(null);
|
||||
|
||||
// jdk.internal.loader.ClassLoaders.AppClassLoader.ucp
|
||||
Field ucpField = classLoader.getClass().getDeclaredField("ucp");
|
||||
long ucpFieldOffset = unsafe.objectFieldOffset(ucpField);
|
||||
Object ucpObject = unsafe.getObject(classLoader, ucpFieldOffset);
|
||||
|
||||
// jdk.internal.loader.URLClassPath.path
|
||||
Field pathField = ucpField.getType().getDeclaredField("path");
|
||||
long pathFieldOffset = unsafe.objectFieldOffset(pathField);
|
||||
ArrayList<URL> path = (ArrayList<URL>) unsafe.getObject(ucpObject, pathFieldOffset);
|
||||
|
||||
return path.toArray(new URL[path.size()]);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -31,10 +31,8 @@ public class VerifyUtils {
|
||||
public static Object instanceVerity(byte[] bytes) throws Exception {
|
||||
String name = Type.getObjectType(AsmUtils.toClassNode(bytes).name).getClassName();
|
||||
|
||||
URLClassLoader systemClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
ClassbyteClassLoader cl = new ClassbyteClassLoader(systemClassLoader.getURLs(),
|
||||
ClassbyteClassLoader cl = new ClassbyteClassLoader(ClassLoaderUtils.getUrls(ClassLoader.getSystemClassLoader()),
|
||||
ClassLoader.getSystemClassLoader().getParent());
|
||||
|
||||
cl.addClass(name, bytes);
|
||||
|
Loading…
Reference in New Issue
Block a user