让我首先总结一下我到底想要做什么。基本上,我使用 JavaCompiler 包在运行时编译一个扩展我的超类“Player”的类。我唯一知道的是子类是它将扩展 Player 并覆盖抽象方法 calcMove()。为了在编译后在运行时加载类,我创建了一个 URIclassloader 对象来在类文件创建后加载它。问题是当我尝试从实例化对象运行 calcMove 方法时(通过使用 java.lang.reflect)
这基本上是我正在做的事情:
//to hold the compiler output
ByteArrayOutputStream compilerOut = new ByteArrayOutputStream();
//compile
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, compilerOut, compilerOut, playerFile.getAbsolutePath());
if (compilationResult == 0) {
System.out.println("File " + playerFile.getName() + " compiled successfully");
} else {
//code 99 as compile error
System.err.println(compilerOut.toString());
System.exit(99);
}
文件编译完成后,我使用此代码制作 uriclassloader 来加载类(上传文件夹包含源文件和类文件) className 由文件名确定。然后我使用 java Reflection 将类实例化为对象并将其转换为 Player:
URL classUrl = new File("upload").toURI().toURL();
ClassLoader classLoader = URLClassLoader.newInstance(new URL[]{classUrl}, ClassLoader.getSystemClassLoader());
classLoader.loadClass(className).asSubclass(Player.class);
Class<?> studentCustomClass = Class.forName(className, true, classLoader);
Constructor<?> constructor = studentCustomClass.getConstructor();
Player studentPlayer = (Player) constructor.newInstance()
实例化显然有效,直到我到达它调用 calcMove 的位置。我创建了一个新的“游戏”类,它接受 2 个玩家参数,在这个游戏类中,我从自定义类对象(转换为玩家)中调用 calcMove() 方法。但是我得到了一个 AbstractMethodError 异常,因为它试图从 Player 调用抽象 calcMove 方法,而不是从子类中调用实现的版本。
所以,我想知道,为什么它试图从父类调用抽象版本而不是我刚刚编译的类的版本,是否有某种原因?(据我所知,java 认为我创建的对象是子类的一种类型,而不仅仅是一个 Player 类,因为它是抽象的,所以我无论如何都无法实例化它)现在我正在使用 java Reflection 来强制它从对象调用 calcMove 函数
Method calcmove = players[0].getClass().getMethod("calcMove");
calcmove.invoke(players[0]);
出于安全原因,我想避免在代码中此时使用反射。那么为什么这行得通,但是这个:
players[0].calcMove();
给我一个 AbstractClassError?