首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ByteBuddy中的非法接口类型

ByteBuddy中的非法接口类型
EN

Stack Overflow用户
提问于 2016-03-04 10:03:09
回答 1查看 492关注 0票数 2

我正在从ByteBuddy 0.6.15升级到1.2.3,我得到了“非法接口类型”异常。我设法把它归结为以下几个简短的代码:

代码语言:javascript
复制
@Test
public void bytebuddy() {
    new ByteBuddy()
            .subclass(Object.class)
            .implement(MyInterface.class)
            .make();
}

interface MyInterface {
    void doSomething();
}

在0.6.15中,这曾经有效,但在1.2.3中,我得到了一个例外:参见下面。

我不明白什么是不合法的界面,也不明白我需要修改什么才能让它正常工作。起初,我认为这可能是因为我没有对doSomething方法做任何事情,但是当接口完全为空时也会发生同样的错误。

代码语言:javascript
复制
java.lang.IllegalStateException: Illegal interface type interface nl.jqno.equalsverifier.internal.InstantiatorTest$MyInteface for class net.bytebuddy.renamed.java.lang.Object$ByteBuddy$qViwRZJu

  at net.bytebuddy.dynamic.scaffold.InstrumentedType$Default.validated(InstrumentedType.java:694)
  at net.bytebuddy.dynamic.scaffold.MethodRegistry$Default.prepare(MethodRegistry.java:530)
  at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:153)
  at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$Delegator.make(DynamicType.java:2508)
  at nl.jqno.equalsverifier.internal.InstantiatorTest.bytebuddy(InstantiatorTest.java:43)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:497)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
  at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:497)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-05 10:15:02

在版本1.*中,Buddy添加了显式验证,从而使诊断错误变得更容易。在您的例子中,您从存在于另一个包中的类扩展包-私有接口。这意味着接口在运行时对实现类不可见,最终会导致IllegalAccessError

如果您将接口设置为public,则您的示例应该可以工作。或者,您可以将生成的类放入接口的包中。当您直接对接口(即new ByteBuddy().subclass(MyInteface.class) )进行子类时,就会自动发生这种情况。字节Buddy发现提供的类是一个接口,并实现了它。但是,在这种情况下,隐式名称将与接口位于同一个包中。

最后,类型验证引入了一些运行时开销。您可以通过设置new ByteBuddy().with(TypeValidation.DISABLED)来禁用验证,理想情况下,您在生产中应该做什么,而不是在单元测试中。

最后,没有任何违法的单元测试,措辞是错误的,我改变了异常信息,说看不见。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35793093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档