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

Resolve #208: Only use deprecated JDK 8 method under JDK 8 #1687

Merged
merged 1 commit into from
Sep 28, 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
27 changes: 23 additions & 4 deletions loader/jrunscript/java/inonit/script/engine/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ public String toString() {
return rv;
}

private java.lang.reflect.Method polyfillGetDefinedPackage() {
try {
return ClassLoader.class.getMethod("getDefinedPackage", String.class);
} catch (NoSuchMethodException e) {
try {
return ClassLoader.class.getDeclaredMethod("getPackage", String.class);
} catch (NoSuchMethodException ee) {
throw new RuntimeException(ee);
}
}
}

protected Class<?> findClass(String name) throws ClassNotFoundException {
//LOGGER.log(Level.FINE, "findClass(" + name + ")");
String path = name.replace('.', '/') + ".class";
Expand All @@ -189,11 +201,18 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
//LOGGER.log(Level.FINE, "findClass(" + name + ") using source " + source);
Code.Loader.Resource in = source.getFile(path);
if (in != null) {
if (getPackage(packageName) == null) {
definePackage(packageName,null,null,null,null,null,null,null);
java.lang.reflect.Method getDefinedPackage = polyfillGetDefinedPackage();
try {
if (getDefinedPackage.invoke(this, packageName) == null) {
definePackage(packageName,null,null,null,null,null,null,null);
}
byte[] b = streams.readBytes(in.getInputStream());
return defineClass(name, b, 0, b.length);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (java.lang.reflect.InvocationTargetException e) {
throw new RuntimeException(e.getCause());
}
byte[] b = streams.readBytes(in.getInputStream());
return defineClass(name, b, 0, b.length);
}
} catch (IOException e) {
// do nothing
Expand Down
Loading