Improve reliability of java version check in ReflectionClassLoader (#2093)

This commit is contained in:
Luck 2020-03-24 10:35:13 +00:00
parent 30d7768299
commit 2adea2d3dc
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -52,7 +52,7 @@ public class ReflectionClassLoader implements PluginClassLoader {
} }
this.addUrlMethod = Suppliers.memoize(() -> { this.addUrlMethod = Suppliers.memoize(() -> {
if (getJavaMajorVersion() >= 9) { if (isJava9OrNewer()) {
bootstrap.getPluginLogger().info("It is safe to ignore any warning printed following this message " + bootstrap.getPluginLogger().info("It is safe to ignore any warning printed following this message " +
"starting with 'WARNING: An illegal reflective access operation has occurred, Illegal reflective " + "starting with 'WARNING: An illegal reflective access operation has occurred, Illegal reflective " +
"access by " + getClass().getName() + "'."); "access by " + getClass().getName() + "'.");
@ -78,16 +78,13 @@ public class ReflectionClassLoader implements PluginClassLoader {
} }
} }
private static int getJavaMajorVersion() { private static boolean isJava9OrNewer() {
String version = System.getProperty("java.version"); try {
if (version.startsWith("1.")) { // method was added in the Java 9 release
version = version.substring(2, 3); Runtime.class.getMethod("version");
} else { return true;
int dot = version.indexOf("."); } catch (NoSuchMethodException e) {
if (dot != -1) { return false;
version = version.substring(0, dot);
}
} }
return Integer.parseInt(version);
} }
} }