Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve loading of classes in RunnerClassLoader #40033

Merged
merged 1 commit into from
Apr 16, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
*/
public final class RunnerClassLoader extends ClassLoader {

static {
registerAsParallelCapable();
}

/**
* A map of resources by dir name. Root dir/default package is represented by the empty string
*/
Expand Down Expand Up @@ -101,18 +105,55 @@ public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundExce
continue;
}
definePackage(packageName, resources);
try {
return defineClass(name, data, 0, data.length, resource.getProtectionDomain());
} catch (LinkageError e) {
loaded = findLoadedClass(name);
if (loaded != null) {
return loaded;
return defineClass(name, data, resource);
}
}
return getParent().loadClass(name);
}

private void definePackage(String pkgName, ClassLoadingResource[] resources) {
if ((pkgName != null) && getDefinedPackage(pkgName) == null) {
for (ClassLoadingResource classPathElement : resources) {
ManifestInfo mf = classPathElement.getManifestInfo();
if (mf != null) {
try {
definePackage(pkgName, mf.getSpecTitle(),
mf.getSpecVersion(),
mf.getSpecVendor(),
mf.getImplTitle(),
mf.getImplVersion(),
mf.getImplVendor(), null);
} catch (IllegalArgumentException e) {
var loaded = getDefinedPackage(pkgName);
if (loaded == null) {
throw e;
}
}
return;
}
}
try {
definePackage(pkgName, null, null, null, null, null, null, null);
} catch (IllegalArgumentException e) {
var loaded = getDefinedPackage(pkgName);
if (loaded == null) {
throw e;
}
}
}
return getParent().loadClass(name);
}

private Class<?> defineClass(String name, byte[] data, ClassLoadingResource resource) {
Class<?> loaded;
try {
return defineClass(name, data, 0, data.length, resource.getProtectionDomain());
} catch (LinkageError e) {
loaded = findLoadedClass(name);
if (loaded != null) {
return loaded;
}
throw e;
}
}

private void accessingResource(final ClassLoadingResource resource) {
Expand Down Expand Up @@ -219,28 +260,6 @@ protected Enumeration<URL> findResources(String name) {
return Collections.enumeration(urls);
}

private void definePackage(String pkgName, ClassLoadingResource[] resources) {
if ((pkgName != null) && getPackage(pkgName) == null) {
synchronized (getClassLoadingLock(pkgName)) {
if (getPackage(pkgName) == null) {
for (ClassLoadingResource classPathElement : resources) {
ManifestInfo mf = classPathElement.getManifestInfo();
if (mf != null) {
definePackage(pkgName, mf.getSpecTitle(),
mf.getSpecVersion(),
mf.getSpecVendor(),
mf.getImplTitle(),
mf.getImplVersion(),
mf.getImplVendor(), null);
return;
}
}
definePackage(pkgName, null, null, null, null, null, null, null);
}
}
}
}

private String getPackageNameFromClassName(String className) {
final int index = className.lastIndexOf('.');
if (index == -1) {
Expand Down
Loading