- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
让我首先总结一下我到底想做什么。基本上,我使用 JavaCompiler 包在运行时编译一个扩展我的父类(super class)“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 的地方。我创建了一个新的“Game”类,它接受 2 个 Player 参数,在这个 Game 类中,我从自定义类对象(转换为 Players)调用 calcMove() 方法。但是,我收到 AbstractMethodError 异常,因为它尝试从 Player 调用抽象 calcMove 方法,而不是从子类调用实现的版本。
所以,我想知道,是否有某种原因它试图从父类调用抽象版本而不是我刚刚编译的类的版本? (据我所知,java认为我创建的对象是子类的一种类型,而不仅仅是一个Player类,因为它是抽象的,所以我无论如何都无法实例化)现在我正在使用java Reflection来强制它从对象调用 calcMove 函数
Method calcmove = players[0].getClass().getMethod("calcMove");
calcmove.invoke(players[0]);
出于安全原因,我想避免在代码中使用反射。那么为什么这会起作用,但是这个:
players[0].calcMove();
给我一个 AbstractClassError?
最佳答案
我已经找到了一个有效的解决方案。如果我声明 Player 中的抽象方法 calcMove 受到保护,那么它会正确使用子类中的 calcMove,而不使用反射来强制它。我假设这是因为它无法调用 Player 中的 protected 方法,因此它调用子类中的公共(public)/包访问方法(因为重写的方法可以比我发现的它们重写的方法具有更高级别的可见性)我但我并不完全确定为什么会这样。
关于java - 从运行时编译的类调用抽象类的重写方法时出现 AbstractMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58698651/
我有一个使用 Spring 3.0.5、JPA2 和 Hibernate 3.6.7 的应用程序。 Maven 处理我的依赖管理。这是一个 pom 摘录: 3.0.5.RELEASE
我对配备 jboss 开发工作室的 Eclipse IDE (Juno RE 2) 有问题。我有几个使用 JPA 的 Maven 项目。在这些项目中,Eclipse 报错“ 此项目具有 JPA 方面,
我正在尝试使用 java 代码向 postgres 插入一个数组,但我总是收到此错误: SEVERE [http-nio-8080-exec-2]org.apache.catalina.core.St
让我首先总结一下我到底想做什么。基本上,我使用 JavaCompiler 包在运行时编译一个扩展我的父类(super class)“Player”的类。我唯一知道的是,它将在子类中扩展 Player
我的电脑中的 Web 服务程序出现错误,但相同的代码在其他电脑上运行正常。有什么解决办法吗? SEVERE: Servlet.service() for servlet [ServletAdaptor
当我在 ant 中运行 junit 测试时出现以下错误。测试类需要调用web service API。 有人可以帮忙解决吗? java.lang.AbstractMethodError: javax.
我刚刚尝试关注 this为我的 Web 服务实现基于 token 的身份验证的教程,刚刚完成了“保护 REST 端点”步骤,但现在在调用端点时我总是收到 AbstractMethodError。 我已
我正在 tomcat 8.5 中构建一个简单的 JSF 2.3 应用程序。我让 websocket 正常工作,然后添加了 Pretty Faces。 如果我单独使用它们,它们都可以工作。我的意思是,如
考虑以下代码: object Impls { trait ConcreteImpl { type Foo = Int def foo = 1 } } trait Base {
我正在部署一个在我自己的机器上运行的 war 并收到错误: java.lang.AbstractMethodError: com.mchange.v2.c3p0.impl.NewProxyPrepar
将 hibernate(在 JBoss 7.2.0 中)从 4.2.14 更新到 5.3.7 后,出现 AbstractMethodError 异常。这可能是一些依赖性问题,但我不知道应该在哪里寻找。
在尝试启动 SpringApplication 时,使用 Maven 添加 Jadira 依赖项会导致 AbstractMethodError。堆栈跟踪中没有任何帮助;它没有引用我自己的任何类(cla
我正在尝试制作一个 Elasticsearch 传输客户端,但出现以下错误 Caused by: java.lang.AbstractMethodError: org.elasticsearch.tr
我刚刚将应用程序的 GCM 更改为 FCM,并向其中添加了 OneSignal。完全遵循指南,甚至创建了一个新的监听器服务来处理推送通知。 然而,即使没有监听器,每当我收到推送时,应用程序都会崩溃并显
我在 android studio bumblebee 2021.1.1 上工作,并在创建 compose 项目时出现此错误 java.lang.AbstractMethodError: abstra
我是网络服务新手。我尝试将 jax.ws.. Web 服务与 jersey 和 spring 一起使用。当我尝试在 Windows 中使用 Chrome Post Client 或 CURL 发送请求
我正在项目中将 newrelic 版本从 2.x.x 更新到 5.x.x。我按照 newrelic 文档中提到的步骤进行操作(我更新了 pom 文件中的依赖项版本,更新了代理 jar 文件和配置文件)
我最近开始使用 Jackon 和 SmileFactory API's 来序列化 JSON 文档,但不知何故,每当我尝试使用 SmileFactory properties = new HashMap
我正在尝试从 Apache Spark 示例 mlib 推荐器 http://spark.apache.org/docs/1.2.1/mllib-collaborative-filtering.htm
我在我的项目中使用atomikos Essentials 来管理交易。部署项目后出现以下异常。我陷入其中无法解决。谁能帮我解决这个问题。 Exception in thread "Atomikos:0
我是一名优秀的程序员,十分优秀!