Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main/java/io/xjar/boot/XBootClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.util.Enumeration;
import sun.misc.Resource;
import sun.misc.URLClassPath;

/**
* X类加载器
Expand All @@ -20,6 +23,7 @@
* 2018/11/23 23:04
*/
public class XBootClassLoader extends LaunchedURLClassLoader {
private final URLClassPath ucp;
private final XBootURLHandler xBootURLHandler;

static {
Expand All @@ -28,6 +32,7 @@ public class XBootClassLoader extends LaunchedURLClassLoader {

public XBootClassLoader(URL[] urls, ClassLoader parent, XDecryptor xDecryptor, XEncryptor xEncryptor, XKey xKey) throws Exception {
super(urls, parent);
this.ucp = new URLClassPath(urls);
this.xBootURLHandler = new XBootURLHandler(xDecryptor, xEncryptor, xKey, this);
}

Expand Down Expand Up @@ -58,15 +63,18 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
return super.findClass(name);
} catch (ClassFormatError e) {
URL resource = findResource(name.replace('.', '/') + ".class");
String path = name.replace('.', '/').concat(".class");
URL resource = findResource(path);
if (resource == null) {
throw new ClassNotFoundException(name, e);
}
try (InputStream in = resource.openStream()) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XKit.transfer(in, bos);
byte[] bytes = bos.toByteArray();
return defineClass(name, bytes, 0, bytes.length);
Resource res = ucp.getResource(path, false);
CodeSource cs = new CodeSource(res.getCodeSourceURL(), res.getCodeSigners());
return defineClass(name, bytes, 0, bytes.length, cs);
} catch (Throwable t) {
throw new ClassNotFoundException(name, t);
}
Expand Down