java - 如何在 Java 中实现包装装饰器?

标签 java decorator bytecode byte-buddy

问题是创建现有对象的动态增强版本。

我无法修改对象的 Class。相反,我必须:

  • 子类化
  • 将现有对象包装在新的 Class
  • 将所有原始方法调用委托(delegate)给包装对象
  • 实现由另一个接口(interface)定义的所有方法

添加到现有对象的接口(interface)是:

public interface EnhancedNode {

  Node getNode();
  void setNode(Node node);

  Set getRules();
  void setRules(Set rules);

  Map getGroups();
  void setGroups(Map groups);

}

Byte Buddy我设法子类化并实现了我的接口(interface)。问题是对包装对象的委托(delegate)。我发现执行此操作的唯一方法是使用太慢的反射(我的应用程序负载很重,性能很关键)。

到目前为止我的代码是:

Class<? extends Node> proxyType = new ByteBuddy()
     .subclass(node.getClass(), ConstructorStrategy.Default.IMITATE_SUPER_TYPE_PUBLIC)
     .method(anyOf(finalNode.getClass().getMethods())).intercept(MethodDelegation.to(NodeInterceptor.class))
     .defineField("node", Node.class, Visibility.PRIVATE)
     .implement(EnhancedNode.class).intercept(FieldAccessor.ofBeanProperty())
     .defineField("groups", Map.class, Visibility.PRIVATE)
     .implement(EnhancedNode.class).intercept(FieldAccessor.ofBeanProperty())
     .defineField("rules", Set.class, Visibility.PRIVATE)
     .implement(EnhancedNode.class).intercept(FieldAccessor.ofBeanProperty())
     .make()
     .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
     .getLoaded();
enhancedClass = (Class<N>) proxyType;
EnhancedNode enhancedNode = (EnhancedNode) enhancedClass.newInstance();
enhancedNode.setNode(node);

其中 Node 是要子类化/包装的对象。 NodeInterceptor 将调用的方法转发给 getNode 属性。

这里是NodeInterceptor的代码:

public class NodeInterceptor {

  @RuntimeType
  public static Object intercept(@Origin Method method,
                               @This EnhancedNode proxy,
                               @AllArguments Object[] arguments)
        throws Exception {
      Node node = proxy.getNode();
      Object res;
      if (node != null) {
          res = method.invoke(method.getDeclaringClass().cast(node), arguments);
      } else {
          res = null;
      }
      return res;
  }
}

一切正常,但拦截方法太慢了,我打算直接使用 ASM 来添加 Node 的每个方法的实现,但我希望有一种使用 Byte Buddy 的更简单的方法。

最佳答案

您可能想使用 Pipe 而不是反射 API:

public class NodeInterceptor {

  @RuntimeType
  public static Object intercept(@Pipe Function<Node, Object> pipe,
                                 @FieldValue("node") Node proxy) throws Exception {
      return proxy != null
        ? pipe.apply(proxy);
        : null;
  }
}

要使用管道,首先需要安装它。如果您有可用的 Java 8,则可以使用 java.util.Function。否则,只需定义一些类型:

interface Function<T, S> { S apply(T t); }

你自己。类型的名称和方法是无关紧要的。安装类型:

MethodDelegation.to(NodeInterceptor.class)
                .appendParameterBinder(Pipe.Binder.install(Function.class));

但是您确定反射部分是应用程序性能问题的关键点吗?您是否正确地缓存了生成的类并且您的缓存是否有效地工作?反射 API 比它的声誉更快,特别是因为使用 Byte Buddy 往往意味着单态调用站点。

最后,一些一般性反馈。你在打电话

.implement(EnhancedNode.class).intercept(FieldAccessor.ofBeanProperty())

多次。这没有效果。此外,method.getDeclaringClass().cast(node) 不是必需的。反射 API 为您进行转换。

关于java - 如何在 Java 中实现包装装饰器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34595221/

相关文章:

java - POST 到 Jersey REST 服务时出现错误 415 不支持的媒体类型

python - 奇怪的AttributeError仅在赋值时

python - 如何创建一个可以使用或不使用参数的装饰器?

java - Java 中的动态链接 : Verifier vs Resolution

java - AccordionPanel 移动时间表

java - 将可变对象转换为不可变对象(immutable对象)

c# - 如何在C#中自动生成装饰器模式

java - 字节码特性在 Java 语言中不可用

python字节码兼容性

java - JPA:如何使用派生 IdClass 实现复合主键?