如何使用字节好友来仪器/重新构成内部类别?

发布于 2025-02-03 05:50:57 字数 2340 浏览 5 评论 0 原文

目标

我想创建一个插件包装器来嵌入一个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() );

我发现以下是指类似的问题:

,但尚未找到精确的解决方案。

Goal

I would like to create a plug-in wrapper to embed a Java code that will let me enable/disable methods annotated with JUnit5 Before* and After* annotations at runtime as well as handle in similar manner JUnit5 extensions (the ExtendWith annotation).

I chose ByteBuddy as primary instrumentation library.

I created a github repo here: https://github.com/azewiusz/bytebuddy_examples where I describe the idea in more detail.

Question

The problem is that I cannot get instrumentation working for inner classes.
I keep getting two exceptions:

Caused by: java.lang.IllegalStateException: Failed to invoke proxy for
public abstract java.lang.reflect.AnnotatedElement
net.bytebuddy.description.method.MethodDescription$InDefinedShape$AbstractBase$Executable.getAnnotatedReceiverType(java.lang.Object)

preceded by

Caused by: java.lang.IncompatibleClassChangeError
tests.TestSetWithInnerClasses and
tests.TestSetWithInnerClasses$HelperTestFilteredOutExtendWithInnerClass
disagree on InnerClasses attribute

A test class that reproduces is here:
https://github.com/azewiusz/bytebuddy_examples/blob/master/src/test/java/tests/CoreInstrumentationTest.java

-> Look there for test2

It fails at this transformation:

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() );

I found following that seem to refer to similar problem:

But haven't found yet an exact solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2025-02-10 05:50:57

仪器内部阶级很棘手。 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 consider noNestMate() to avoid clashes there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文