【问题标题】:method not found exception but method does exist and is implemented方法未找到异常,但方法确实存在并已实现
【发布时间】:2016-09-08 07:06:35
【问题描述】:

我有以下问题。

Caused by: java.lang.NoSuchMethodError :ResourcePreviewRepository.objectExists(Ljava/lang/String;)Z

在运行和调试期间,这是类。

EntityRepository$GenericRepository$ResourcePreviewRepository$1323269030$Proxy$_$$_WeldClientProxy.objectExists(Unknown Source)

这是 ResourcePreviewRepository 的定义

public interface ResourcePreviewRepository extends GenericRepository<String>, EntityRepository<String, ResourceMetadata>

现在:objectExists 方法确实存在于名为 AbstractJcrRepository 的 GenericRepository 的具体实现中。但是这个方法也是在 EntityRepository 接口中定义的(我想这就是问题所在)。

public interface GenericRepository<IdentifierType extends Serializable> {
   boolean objectExists(IdentifierType id) throws RepositoryException;
}


public class AbstractJcrRepository implements GenericRepository<String> {

   @Override
   public boolean objectExists(final String id) throws RepositoryException {
      ...
   }

}

请注意,当我向 GenericRepository 添加强制转换时,它确实有效,但显然我不想强制转换。

这是我应该得到的最终实现:

public class JcrResourcePreviewRepositoryImpl extends AbstractJcrEntityRepository<ResourceMetadata> implements ResourcePreviewRepository

但是虽然我确实有实现和可用的方法,但我得到了这个错误。我该如何解决这个问题?

感谢您的帮助。

使用解决方案更新 我不会发布这是一个答案,但我必须将缺少的方法添加到界面中。虽然Producer返回的impl类确实实现了这个方法,但是只能通过继承一个接口加上它的扩展。所以在继承链中声明这个方法两次就解决了。

【问题讨论】:

  • 更多代码会有所帮助。
  • 我看不出有任何理由假设接口中存在该方法。
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建minimal reproducible example
  • 把你的代码和方法实现也放在一起
  • 我添加了更多代码

标签: java cdi weld


【解决方案1】:

由于AbstractJcrRepository 没有实现ResourcePreviewRepository,所以发生异常时使用的不是这个实现。

【讨论】:

  • 这没有意义吧?因为 ResourcePreviewRepository 继承了扩展的方法。它也编译得很好。最终的实现应该包含方法 impl。
  • ResourcePreviewRepository 是一个接口,所以它不需要实现他的扩展。 ResourcePreviewRepository 的实现必须扩展 AbstractJcrRepository 才能得到它。
  • 确实是这样,我会添加更多代码,可能不清楚
  • 两个接口中的签名是否相同(带有 throws)? GenericRepository && EntityRepository
  • 可能是因为boolean objectExists(final String id) 不等于boolean objectExists(IdentifierType id)String 不扩展 IdentifierType。但我认为你应该在编译时得到一个错误。
猜你喜欢
  • 2023-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多