Skip to content

Commit

Permalink
Fix NPE when on the bootclasspath (fixes #481)
Browse files Browse the repository at this point in the history
To reduce runtime overhead due to fields needed only by certain
configurations, code generation is used and reflection loads the
optimal implementation classes. This trades off a small amount of
disk space for reduced memory usage.

Unfortunately the wrong classloader method was used, which is not
available on the bootclasspath (null value). This fails when the
cache is used by a compiler, e.g. ErrorProne's javac fork. Instead
Class.forName is now used which should resolve to a non-null,
acceptable class loader. It appears as if this should be the same
loader as before, or else the system loader if on the bootclasspath.
  • Loading branch information
ben-manes committed Dec 8, 2020
1 parent f77b9e2 commit c1ed088
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ private LocalCacheSelectorCode expires() {
private LocalCacheSelectorCode selector() {
block
.beginControlFlow("try")
.addStatement("$T<?> clazz = $T.class.getClassLoader().loadClass(sb.toString())",
Class.class, LOCAL_CACHE_FACTORY)
.addStatement("Class<?> clazz = Class.forName(sb.toString())")
.addStatement("$T<?> ctor = clazz.getDeclaredConstructor($T.class, $T.class, $T.class)",
Constructor.class, BUILDER, CACHE_LOADER.rawType, TypeName.BOOLEAN)
.add("@SuppressWarnings($S)\n", "unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ private NodeSelectorCode maximum() {
private NodeSelectorCode selector() {
block
.beginControlFlow("try")
.addStatement("$T<?> clazz = $T.class.getClassLoader().loadClass(sb.toString())",
Class.class, NODE_FACTORY.rawType)
.addStatement("Class<?> clazz = Class.forName(sb.toString())")
.add("@SuppressWarnings($S)\n", "unchecked")
.addStatement("$1T factory = ($1T) clazz.getDeclaredConstructor().newInstance()",
NODE_FACTORY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public JCacheClassLoader() {
public Class<?> loadClass(String name) throws ClassNotFoundException {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
ClassNotFoundException error = null;
if (contextClassLoader != null && contextClassLoader != DEFAULT_CLASS_LOADER) {
if ((contextClassLoader != null) && (contextClassLoader != DEFAULT_CLASS_LOADER)) {
try {
return contextClassLoader.loadClass(name);
} catch (ClassNotFoundException e) {
Expand All @@ -198,7 +198,9 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
}

ClassLoader parentClassLoader = getParent();
if (parentClassLoader != null && parentClassLoader != contextClassLoader && parentClassLoader != classClassLoader) {
if ((parentClassLoader != null)
&& (parentClassLoader != classClassLoader)
&& (parentClassLoader != contextClassLoader)) {
return parentClassLoader.loadClass(name);
}
throw (error == null) ? new ClassNotFoundException(name) : error;
Expand All @@ -207,7 +209,7 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
@Override
public @Nullable URL getResource(String name) {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null && contextClassLoader != DEFAULT_CLASS_LOADER) {
if ((contextClassLoader != null) && (contextClassLoader != DEFAULT_CLASS_LOADER)) {
URL resource = contextClassLoader.getResource(name);
if (resource != null) {
return resource;
Expand All @@ -223,7 +225,9 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
}

ClassLoader parentClassLoader = getParent();
if (parentClassLoader != null && parentClassLoader != contextClassLoader && parentClassLoader != classClassLoader) {
if ((parentClassLoader != null)
&& (parentClassLoader != classClassLoader)
&& (parentClassLoader != contextClassLoader)) {
return parentClassLoader.getResource(name);
}

Expand All @@ -245,7 +249,9 @@ public Enumeration<URL> getResources(String name) throws IOException {
}

ClassLoader parentClassLoader = getParent();
if (parentClassLoader != null && parentClassLoader != contextClassLoader && parentClassLoader != classClassLoader) {
if ((parentClassLoader != null)
&& (parentClassLoader != classClassLoader)
&& (parentClassLoader != contextClassLoader)) {
resources.addAll(Collections.list(parentClassLoader.getResources(name)));
}

Expand Down

0 comments on commit c1ed088

Please sign in to comment.