0

让我首先总结一下我到底想要做什么。基本上,我使用 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?

4

1 回答 1

0

我设法找到了一个可行的解决方案。如果我将 Player 中的抽象方法 calcMove 声明为受保护,那么它会正确使用子类中的 calcMove 而不使用反射来强制它。我假设这是因为它无法调用 Player 中的受保护方法,因此它调用子类中的公共/包访问方法(因为被覆盖的方法可以比我发现的它们覆盖的方法具有更高的可见性)我'不完全确定为什么这有效。

于 2019-11-08T14:48:17.190 回答