如何使用字节好友来仪器/重新构成内部类别?
目标
我想创建一个插件包装器来嵌入一个Java代码,该代码可以让我在*之前和*在* and*之前用Junit5注释的方法,并以类似的方式处理Junit5扩展名(ExtentWith andwith notation)。
我选择Bytebuddy作为主要仪表库。
我在此处创建了一个GitHub回购: https://github.com/azewewius.com/azewewiusz/bytebuddy_examples 我在哪里更详细的想法。
问题
问题是我无法让仪器在内部阶级工作。 我一直得到两个例外:
引起的:java.lang.illegalstateException:未能调用代理 公共摘要java.lang.reflect.annotatedElement net.bytebuddy.description.method.methoddescription $ indefinedshape $ axpactutabase $ expautable.getAnnotatedReceivertype(java.lang.object)
先于
由:java.lang.incompatibleclasschangeerror引起 tests.testsetsetwithinnerclasses和 tests.testsetwithinnerclasses $ helperTestFilterEdOutExtendWithInnerClass 在InnerClasses属性上不同意
- >在那里寻找Test2,
它在此转换时失败了:
final Class strippedOffExtendWithAnnotation = filterOutJUnit5ExtendWithAnnotation(
testClass, classLoader );
Class beforeAll = stagedTypeTransform( strippedOffExtendWithAnnotation,
new ByteBuddy().rebase( strippedOffExtendWithAnnotation,
ClassFileLocator.ForClassLoader.of( classLoader ) )
.name( testClass.getName() + "BeforeAll" )
.method( ElementMatchers
.isAnnotatedWith( BeforeAll.class ) )
.intercept( MethodDelegation.to( InterceptorForBeforeAllAnnotation.class ) ).make() );
我发现以下是指类似的问题:
- 如何访问接收器类型的参数上的类型注释
- https://github.com/raphw/byte-buddy/issues/83
- https://bugs.java.com/bugdatabase/view_bug.do?bug_id = 7003595 (看起来 部分相关)
,但尚未找到精确的解决方案。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仪器内部阶级很棘手。 Java类包含所谓的内部类属性描述编译Java类无法表示的Java类属性。例如,编译的Java类不能是 private ,但是在使用反射时,您仍然希望看到内部类的修饰符。
您可以考虑通过在DSL中使用
topleveltype()
将新类成为顶级类。您还应考虑nonestmate()
以避免在那里发生冲突。Instrumenting inner classes is tricky. Java classes contain so-called inner class attributes that describe Java class properties that compiled Java classes cannot represent. For example, a compiled Java class cannot be private, but you would still want to see this modifier for the inner class when using reflection.
You can consider to make your new class a top-level class by using
topLevelType()
in the DSL. You should also considernoNestMate()
to avoid clashes there.