簡體   English   中英

ByteBuddy中的接口類型非法

[英]Illegal interface type in ByteBuddy

我正在從ByteBuddy 0.6.15升級到1.2.3,並且遇到了“非法接口類型”異常。 我已經將其簡化為以下幾段代碼:

@Test
public void bytebuddy() {
    new ByteBuddy()
            .subclass(Object.class)
            .implement(MyInterface.class)
            .make();
}

interface MyInterface {
    void doSomething();
}

在0.6.15中,這曾經可以工作,但是在1.2.3中,我遇到了一個例外:請參見下文。

我不了解該接口有哪些違法規定,或者需要進行哪些更改才能使其正常工作。 起初我以為可能是因為我沒有使用doSomething方法執行任何操作,但是當接口完全為空時,會發生相同的錯誤。

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)

在1. *版中,Byte Buddy添加了顯式驗證,這使診斷錯誤變得更加容易。 在您的情況下,您從另一個包中的類擴展了包專用接口。 這意味着該接口對於實現類在運行時不可見,這最終將導致IllegalAccessError

如果將接口設置為public ,那么您的示例應該可以工作。 或者,您可以將生成的類放入接口的包中。 當直接子類化接口(即new ByteBuddy().subclass(MyInteface.class)時,會自動發生這種情況。 字節伙伴發現提供的類是一個接口,並實現了它。 但是,在這種情況下,隱式名稱將是與接口位於同一包中的名稱。

最后,類型驗證會帶來一些運行時開銷。 您可以通過設置new ByteBuddy().with(TypeValidation.DISABLED)來禁用驗證,這是您理想的生產方式,而不是單元測試。

最后,關於單元測試,沒有任何非法之處,措辭有誤,我將異常消息更改為“ invisible”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM