Skip to content

Commit ffb58fe

Browse files
committed
Propagate the cause of the failure from loadClass
If currently the bundle can not be resolved the method Bundle#loadClass fails with a quite generic exception that it hard to understand as it does not contain the name of the class to load nor the actual root cause that the bundle is unable to resolve. This now move the actual logging out of the place of fetching and resolving method to the loadCLass method that allows to also throw a proper exception message here.
1 parent 6ff2397 commit ffb58fe

File tree

1 file changed

+32
-15
lines changed
  • bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework

1 file changed

+32
-15
lines changed

bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/EquinoxBundle.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ public URL getResource(String name) {
614614
return null;
615615
}
616616

617-
ModuleClassLoader classLoader = getModuleClassLoader(false);
617+
ModuleClassLoader classLoader = getModuleClassLoader().moduleClassLoader;
618618
if (classLoader != null) {
619619
return classLoader.getResource(name);
620620
}
@@ -644,13 +644,23 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
644644
if (isFragment()) {
645645
throw new ClassNotFoundException("Can not load a class from a fragment bundle: " + this); //$NON-NLS-1$
646646
}
647+
ResolvedModuleClassLoader resolvedClassLoader = getModuleClassLoader();
648+
ModuleClassLoader classLoader = resolvedClassLoader.moduleClassLoader;
649+
if (classLoader == null) {
650+
ResolutionReport report = resolvedClassLoader.report;
651+
if (report != null) {
652+
String reportMessage = report.getResolutionReportMessage(module.getCurrentRevision());
653+
BundleException bundleException = new BundleException(reportMessage, BundleException.RESOLVE_ERROR);
654+
equinoxContainer.getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, this, bundleException);
655+
throw new ClassNotFoundException(name, bundleException);
656+
}
657+
throw new ClassNotFoundException("No class loader available for the bundle: " + this); //$NON-NLS-1$
658+
}
647659
try {
648-
ModuleClassLoader classLoader = getModuleClassLoader(true);
649-
if (classLoader != null) {
650-
if (name.length() > 0 && name.charAt(0) == '[')
651-
return Class.forName(name, false, classLoader);
652-
return classLoader.loadClass(name);
660+
if (name.length() > 0 && name.charAt(0) == '[') {
661+
return Class.forName(name, false, classLoader);
653662
}
663+
return classLoader.loadClass(name);
654664
} catch (ClassNotFoundException e) {
655665
// This is an equinox-ism, check compatibility flag
656666
boolean compatibilityLazyTrigger = equinoxContainer.getConfiguration().compatibilityLazyTriggerOnFailLoad;
@@ -665,17 +675,11 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
665675
}
666676
throw e;
667677
}
668-
throw new ClassNotFoundException("No class loader available for the bundle: " + this); //$NON-NLS-1$
669678
}
670679

671-
private ModuleClassLoader getModuleClassLoader(boolean logResolveError) {
680+
private ResolvedModuleClassLoader getModuleClassLoader() {
672681
ResolutionReport report = resolve();
673-
if (logResolveError && !Module.RESOLVED_SET.contains(module.getState())) {
674-
String reportMessage = report.getResolutionReportMessage(module.getCurrentRevision());
675-
equinoxContainer.getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, this,
676-
new BundleException(reportMessage, BundleException.RESOLVE_ERROR));
677-
}
678-
return AccessController.doPrivileged(new PrivilegedAction<ModuleClassLoader>() {
682+
ModuleClassLoader cl = AccessController.doPrivileged(new PrivilegedAction<ModuleClassLoader>() {
679683
@Override
680684
public ModuleClassLoader run() {
681685
ModuleWiring wiring = getModule().getCurrentRevision().getWiring();
@@ -688,6 +692,7 @@ public ModuleClassLoader run() {
688692
return null;
689693
}
690694
});
695+
return new ResolvedModuleClassLoader(cl, report);
691696
}
692697

693698
@Override
@@ -701,7 +706,7 @@ public Enumeration<URL> getResources(String name) throws IOException {
701706
if (isFragment()) {
702707
return null;
703708
}
704-
ModuleClassLoader classLoader = getModuleClassLoader(false);
709+
ModuleClassLoader classLoader = getModuleClassLoader().moduleClassLoader;
705710
Enumeration<URL> result = null;
706711
if (classLoader != null) {
707712
result = classLoader.getResources(name);
@@ -1121,4 +1126,16 @@ public String toString() {
11211126
name = "unknown"; //$NON-NLS-1$
11221127
return (name + '_' + getVersion() + " [" + getBundleId() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
11231128
}
1129+
1130+
static class ResolvedModuleClassLoader {
1131+
1132+
final ModuleClassLoader moduleClassLoader;
1133+
final ResolutionReport report;
1134+
1135+
public ResolvedModuleClassLoader(ModuleClassLoader moduleClassLoader, ResolutionReport report) {
1136+
this.moduleClassLoader = moduleClassLoader;
1137+
this.report = report;
1138+
}
1139+
1140+
}
11241141
}

0 commit comments

Comments
 (0)