1
0

No need to nest in else.

This commit is contained in:
Gary Gregory
2021-03-05 14:43:24 -05:00
parent 34ea3d6a0d
commit 1642a1bd98
6 changed files with 96 additions and 119 deletions

View File

@@ -312,26 +312,18 @@ public class PathableClassLoader extends URLClassLoader {
if (parentFirst) {
return super.loadClass(name, resolve);
} else {
// Implement child-first.
//
// It appears that the findClass method doesn't check whether the
// class has already been loaded. This seems odd to me, but without
// first checking via findLoadedClass we can get java.lang.LinkageError
// with message "duplicate class definition" which isn't good.
try {
Class clazz = findLoadedClass(name);
if (clazz == null) {
clazz = super.findClass(name);
}
if (resolve) {
resolveClass(clazz);
}
return clazz;
} catch(final ClassNotFoundException e) {
return super.loadClass(name, resolve);
}
try {
Class clazz = findLoadedClass(name);
if (clazz == null) {
clazz = super.findClass(name);
}
if (resolve) {
resolveClass(clazz);
}
return clazz;
} catch(final ClassNotFoundException e) {
return super.loadClass(name, resolve);
}
}
@@ -344,13 +336,12 @@ public class PathableClassLoader extends URLClassLoader {
public URL getResource(final String name) {
if (parentFirst) {
return super.getResource(name);
} else {
final URL local = super.findResource(name);
if (local != null) {
return local;
}
return super.getResource(name);
}
final URL local = super.findResource(name);
if (local != null) {
return local;
}
return super.getResource(name);
}
/**
@@ -364,30 +355,29 @@ public class PathableClassLoader extends URLClassLoader {
public Enumeration getResourcesInOrder(final String name) throws IOException {
if (parentFirst) {
return super.getResources(name);
} else {
final Enumeration localUrls = super.findResources(name);
final ClassLoader parent = getParent();
if (parent == null) {
// Alas, there is no method to get matching resources
// from a null (BOOT) parent classloader. Calling
// ClassLoader.getSystemClassLoader isn't right. Maybe
// calling Class.class.getResources(name) would do?
//
// However for the purposes of unit tests, we can
// simply assume that no relevant resources are
// loadable from the parent; unit tests will never be
// putting any of their resources in a "boot" classloader
// path!
return localUrls;
}
final Enumeration parentUrls = parent.getResources(name);
final ArrayList localItems = toList(localUrls);
final ArrayList parentItems = toList(parentUrls);
localItems.addAll(parentItems);
return Collections.enumeration(localItems);
}
final Enumeration localUrls = super.findResources(name);
final ClassLoader parent = getParent();
if (parent == null) {
// Alas, there is no method to get matching resources
// from a null (BOOT) parent classloader. Calling
// ClassLoader.getSystemClassLoader isn't right. Maybe
// calling Class.class.getResources(name) would do?
//
// However for the purposes of unit tests, we can
// simply assume that no relevant resources are
// loadable from the parent; unit tests will never be
// putting any of their resources in a "boot" classloader
// path!
return localUrls;
}
final Enumeration parentUrls = parent.getResources(name);
final ArrayList localItems = toList(localUrls);
final ArrayList parentItems = toList(parentUrls);
localItems.addAll(parentItems);
return Collections.enumeration(localItems);
}
/**
@@ -418,18 +408,17 @@ public class PathableClassLoader extends URLClassLoader {
public InputStream getResourceAsStream(final String name) {
if (parentFirst) {
return super.getResourceAsStream(name);
} else {
final URL local = super.findResource(name);
if (local != null) {
try {
return local.openStream();
} catch(final IOException e) {
// TODO: check if this is right or whether we should
// fall back to trying parent. The javadoc doesn't say...
return null;
}
}
return super.getResourceAsStream(name);
}
final URL local = super.findResource(name);
if (local != null) {
try {
return local.openStream();
} catch(final IOException e) {
// TODO: check if this is right or whether we should
// fall back to trying parent. The javadoc doesn't say...
return null;
}
}
return super.getResourceAsStream(name);
}
}

View File

@@ -251,11 +251,10 @@ public class WeakHashtableTestCase extends TestCase {
if(weakHashtable.get(new Long(1)) == null) {
break;
} else {
// create garbage:
final byte[] b = new byte[bytz];
bytz = bytz * 2;
}
// create garbage:
final byte[] b = new byte[bytz];
bytz = bytz * 2;
}
// some JVMs seem to take a little time to put references on

View File

@@ -116,7 +116,8 @@ public class MockSecurityManager extends SecurityManager {
// the call stack.
System.out.println("Access controller found: returning");
return;
} else if (cname.startsWith("java.")
}
if (cname.startsWith("java.")
|| cname.startsWith("javax.")
|| cname.startsWith("junit.")
|| cname.startsWith("org.apache.tools.ant.")
@@ -133,13 +134,12 @@ public class MockSecurityManager extends SecurityManager {
System.out.println("Untrusted code [testcase] found");
throw new SecurityException("Untrusted code [testcase] found");
} else if (cname.startsWith("org.apache.commons.logging.")) {
if (permissions.implies(p)) {
// Code here is trusted if the caller is trusted
System.out.println("Permission in allowed set for JCL class");
} else {
if (!permissions.implies(p)) {
System.out.println("Permission refused:" + p.getClass() + ":" + p);
throw new SecurityException("Permission refused:" + p.getClass() + ":" + p);
}
// Code here is trusted if the caller is trusted
System.out.println("Permission in allowed set for JCL class");
} else {
// we found some code that is not trusted to perform this operation.
System.out.println("Unexpected code: permission refused:" + p.getClass() + ":" + p);