我试图通过覆盖 loadClass(String, Boolean) 方法来更改类名。
我正在创建一个 BukkitPlugin。Bukkitsource 可以在这里找到
类加载器本身工作正常,我对其进行了测试,所有类都工作正常,在我开始更改类名后开始出现错误。
这是方法:
import java.io.File;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import org.bukkit.plugin.java.JavaPlugin;
public class PluginClassLoader extends URLClassLoader {
private final HashMap<String, String> replace;
public PluginClassLoader(JavaPlugin p, HashMap<String, String> replace) throws Exception {
super(new URL[0], p.getClass().getClassLoader());
this.replace = replace;
File f = null;
Field file = JavaPlugin.class.getDeclaredField("file");
file.setAccessible(true);
f = (File) file.get(p);
addURL(f.toURI().toURL());
}
private final HashMap<String, Class<?>> classes = new HashMap<>();
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
String s = replace.get(name);
if(s != null)
name = s;
Class<?> c;
try {
c = findClass(name);
} catch (Exception e) {
c = super.loadClass(name);
}
return c;
}
@Override
protected Class<?> loadClass(String name, boolean b)
throws ClassNotFoundException {
String s = replace.get(name);
if(s != null)
name = s;
Class<?> c;
try {
c = findClass(name);
} catch (ClassNotFoundException e) {
c = super.loadClass(name, b);
}
return c;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
if ((name.startsWith("org.bukkit."))
|| (name.startsWith("net.minecraft."))) {
throw new ClassNotFoundException(name);
}
Class<?> result = classes.get(name);
if (result == null) {
result = super.findClass(name);
classes.put(name, result);
}
return result;
}
}
当我使用类加载器时,每次我使用一个被替换的类时都会得到一个 NoClassDefFoundError 。我最好的猜测是我必须重写另一种方法,但我不知道它可能是什么。
错误日志:
java.lang.NoClassDefFoundError: org/bukkit/craftbukkit/v1_5_R3/CraftServer
at me.leo.itemeffects.IEPlugin.onEnable(IEPlugin.java:25)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.java:282)
at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.java:264)
at net.minecraft.server.v1_6_R2.MinecraftServer.l(MinecraftServer.java:313)
at net.minecraft.server.v1_6_R2.MinecraftServer.f(MinecraftServer.java:290)
at net.minecraft.server.v1_6_R2.MinecraftServer.a(MinecraftServer.java:250)
at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.java:151)
at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:391)
at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
IEPlugin.java:25 是这样的:
CraftServer server = (CraftServer) Bukkit.getServer();
jvm 正在搜索错误的 classDefinition,它会查找带有包 'v1_5_R3' 的那个,但我将 loadClass() 中的类名替换为 'v1_6_R2'
我将类名 'org.bukkit.craftbukkit.v1_5_R3.CraftServer 替换为 'org.bukkit.craftbukkit.v1_6_R2.CraftServer'
带有 v1_5_R3 的包中的类不存在,这就是为什么我在 loadClass(String, Boolean) 方法中将其更改为 v1_6_R2
感谢您的阅读和您希望即将到来的解决方案。