Skip to content

Commit

Permalink
Add notice to inform users to ignore 'illegal reflective access' warn…
Browse files Browse the repository at this point in the history
…ing messages (#952)
  • Loading branch information
lucko committed Mar 19, 2020
1 parent ac9fe74 commit 3973aa1
Showing 1 changed file with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@

package me.lucko.luckperms.common.dependencies.classloader;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

import me.lucko.luckperms.common.plugin.bootstrap.LuckPermsBootstrap;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
Expand All @@ -33,34 +38,56 @@
import java.nio.file.Path;

public class ReflectionClassLoader implements PluginClassLoader {
private static final Method ADD_URL_METHOD;

static {
try {
ADD_URL_METHOD = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
ADD_URL_METHOD.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new ExceptionInInitializerError(e);
}
}

private final URLClassLoader classLoader;

public ReflectionClassLoader(Object plugin) throws IllegalStateException {
ClassLoader classLoader = plugin.getClass().getClassLoader();
@SuppressWarnings("Guava") // we can't use java.util.Function because old Guava versions are used at runtime
private final Supplier<Method> addUrlMethod;

public ReflectionClassLoader(LuckPermsBootstrap bootstrap) throws IllegalStateException {
ClassLoader classLoader = bootstrap.getClass().getClassLoader();
if (classLoader instanceof URLClassLoader) {
this.classLoader = (URLClassLoader) classLoader;
} else {
throw new IllegalStateException("ClassLoader is not instance of URLClassLoader");
}

this.addUrlMethod = Suppliers.memoize(() -> {
if (getJavaMajorVersion() >= 9) {
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 " +
"access by " + getClass().getName() + "'.");
bootstrap.getPluginLogger().info("This is intended, and will not have any impact the operation of LuckPerms.");

This comment has been minimized.

Copy link
@Andre601

Andre601 Mar 19, 2020

Contributor

Shouldn't this be more This is intended, and will not have any impact on the operation of LuckPerms?
The missing on makes it sound wrong.

This comment has been minimized.

Copy link
@lucko

lucko Mar 19, 2020

Author Member

???

This comment has been minimized.

Copy link
@lucko

lucko Mar 19, 2020

Author Member

Oh I see, right yeah.

}

try {
Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
addUrlMethod.setAccessible(true);
return addUrlMethod;
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
});
}

@Override
public void addJarToClasspath(Path file) {
try {
ADD_URL_METHOD.invoke(this.classLoader, file.toUri().toURL());
this.addUrlMethod.get().invoke(this.classLoader, file.toUri().toURL());
} catch (IllegalAccessException | InvocationTargetException | MalformedURLException e) {
throw new RuntimeException(e);
}
}

private static int getJavaMajorVersion() {
String version = System.getProperty("java.version");
if (version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf(".");
if (dot != -1) {
version = version.substring(0, dot);
}
}
return Integer.parseInt(version);
}
}

0 comments on commit 3973aa1

Please sign in to comment.