support jdk 11

This commit is contained in:
hengyunabc 2020-04-20 22:07:50 +08:00
parent e330bdc2f6
commit 637f2b53a9
2 changed files with 49 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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);