Address offline review -> comments for maintainers, simplify exc and JAR_PATH

This commit is contained in:
Justin Lu 2025-06-12 11:00:41 -07:00
parent a9cad71666
commit 47f62aa273

View File

@ -41,7 +41,7 @@ import com.sun.net.httpserver.*;
import sun.net.www.ParseUtil; import sun.net.www.ParseUtil;
public class ClassnameCharTest { public class ClassnameCharTest {
private static final Path JAR_PATH = Path.of(".").resolve("testclasses.jar"); private static final Path JAR_PATH = Path.of("testclasses.jar");
static HttpServer server; static HttpServer server;
public static void realMain(String[] args) throws Exception { public static void realMain(String[] args) throws Exception {
@ -104,7 +104,7 @@ public class ClassnameCharTest {
} }
@Override @Override
public Class<?> findClass(String name) throws ClassNotFoundException { public Class<?> findClass(String name) {
int index = name.indexOf(';'); int index = name.indexOf(';');
String cookie = ""; String cookie = "";
if(index != -1) { if(index != -1) {
@ -115,13 +115,15 @@ public class ClassnameCharTest {
// check loaded JAR files // check loaded JAR files
try { try {
return super.findClass(name); return super.findClass(name);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException _) {}
}
// Otherwise, try loading the class from the code base URL // Otherwise, try loading the class from the code base URL
// final String path = name.replace('.', '/').concat(".class").concat(cookie); // final String path = name.replace('.', '/').concat(".class").concat(cookie);
String encodedName = ParseUtil.encodePath(name.replace('.', '/'), false); String encodedName = ParseUtil.encodePath(name.replace('.', '/'), false);
final String path = (new StringBuffer(encodedName)).append(".class").append(cookie).toString(); final String path = (new StringBuffer(encodedName)).append(".class").append(cookie).toString();
Exception exc = null;
// try block used for checked exceptions as well as ClassFormatError
// from defineClass call
try { try {
URL finalURL = new URL(base, path); URL finalURL = new URL(base, path);
// Make sure the codebase won't be modified // Make sure the codebase won't be modified
@ -131,11 +133,13 @@ public class ClassnameCharTest {
byte[] b = getBytes(finalURL); byte[] b = getBytes(finalURL);
return defineClass(name, b, 0, b.length, codesource); return defineClass(name, b, 0, b.length, codesource);
} }
} catch (Exception e) { // protocol/host/port mismatch, fail with RuntimeExc
throw new ClassNotFoundException(name, e); } catch (Exception underlyingE) {
exc = underlyingE; // Most likely CFE from defineClass
} }
throw new ClassNotFoundException( // Fail if there was either a protocol/host/port mismatch
"\"%s\" not found - Protocol/Host/Port mismatch".formatted(name)); // or an exception was thrown (which is propagated)
throw new RuntimeException(name, exc);
} }
/* /*