是否有一个运行时代理创建库可支持保留代理类的注释? [英] Is there a runtime proxy creation library that supports to retain annotations of the proxied class?

查看:98
本文介绍了是否有一个运行时代理创建库可支持保留代理类的注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用例如 cglib javassist代理,此代理通过创建代理目标的子类。但是,这意味着该代理上的注释将丢失。当一个类由两个库处理时,这是有问题的:

When creating a proxy with for example cglib or javassist proxies, this proxy is implemented by creating a subclass of the proxy target. However, this means that the annotations on this proxy are lost. This is problematic when a class is processed by two libraries where:


  1. 第一个库需要创建给定类的代理才能起作用

  2. 第二个库通过从对象中读取注释来处理对象。

对于第二个库,同时使用第一个库时,注释消失了。问题是:是否存在一个带有高级API的运行时代码生成库,该库可以轻松保留代理类的注释?

For the second library, the annotations have disappeared when simultaneously using the first library. The question is: Does there exist a runtime code generation library with a high-level API that allows for an easy retention of the annotations of the proxied class?

推荐答案

字节伙伴是一个用于在运行时生成Java类的库。它的功能不仅限于创建代理类,而且创建代理类是一个显而易见的用例。

Byte Buddy is a library for the runtime generation of Java classes. Its capabilities are not limited to the creation of proxy classes but the creation of proxy classes is an obvious use case.

假设,我们正在处理以下代码:

Assuming, we are dealing with the following code:

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation { }

@MyAnnotation
class Foo {
  @MyAnnotation
  public void bar() { }
}

然后,我们可以在运行时创建一个覆盖 bar 方法的子类。 bar 方法的重写实现只是简单地调用其超级实现:

Then we can create a subclass at runtime that overrides the bar method. The overridden implementation of the bar method is implemented to simply call its super implementation:

Class<?> runtimeType = new ByteBuddy()
  .withAttribute(TypeAttributeAppender.ForSuperType.INSTANCE)
  .withDefaultMethodAttributeAppender(MethodAttributeAppender.ForInstrumentedMethod.INSTANCE)
  .subclass(Foo.class)
  .method(named("bar")).intercept(SuperMethodCall.INSTANCE)
  .make()
  .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
  .getLoaded();

使用上述运行时类,我们现在可以验证结果类型:

With the above runtime class, we can now verify the resulting type:

assertNotEquals(Foo.class, runtimeType);
assertThat(runtimeType.isAnnotationPresent(MyAnnotation.class)), is(true));
assertThat(runtimeType.getDeclaredMethod("bar").isAnnotationPresent(MyAnnotation.class)), is(true));

类型和方法均由 MyAnnotation 尽管有子类。通过调用 getDeclaredMethod ,我们进一步验证了该子类实际上定义了一个新方法。

Both the type and the method are annotated by MyAnnotation despite the subclass. By calling getDeclaredMethod, we furthermore verify that the subclass actually defines a new method.

披露:我是Byte Buddy的作者,我想为这个问题提供一个答案,该问题通常在SO上稍作介绍更具体的上下文。此外,我想借此机会为Byte Buddy创建一个SO标签。

Disclosure: I am the author of Byte Buddy and I wanted to provide an answer to this question that is often asked on SO in a slightly more specific context. Furthermore, I wanted to take the opportunity to create a SO tag for Byte Buddy.

这篇关于是否有一个运行时代理创建库可支持保留代理类的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆