简体   繁体   English

在运行时更改类的超类

[英]Change the superclass of a class at runtime

Can i use Byte Buddy to enhance a class at runtime by inheriting from an abstract class? 我可以通过从抽象类继承来使用字节伙伴来在运行时增强类吗?

@AggregateRoot
public class Organization {
}

I should be able to intercept all instances of Organization and enhance it by inheriting from an abstract class like below. 我应该能够拦截Organization所有实例,并通过从下面的抽象类继承而对其进行增强。

public abstract class BaseAggregateRoot {
  public void notify() {
    //Notify domain events
  }
}

So what you want to do is to dynamically change the super class of a given class at runtime? 因此,您想要做的是在运行时动态更改给定类的超类? Currently, this is not possible in Byte Buddy. 当前,这在Byte Buddy中是不可能的。 Instead, you can add interfaces with default methods defined, when you are using Java 8. Would that be an option? 相反,当您使用Java 8时,可以添加具有定义的默认方法的接口。这是一种选择吗?

What you can also do is to subclass the BaseAggregateRoot class at runtime, name the class Organization and add the annotation on top of it. 您还可以做的是在运行时对BaseAggregateRoot类进行子类化,将类命名为Organization并在其顶部添加注释。

I will however consider this as a future feature for Byte Buddy. 但是,我会将其视为Byte Buddy的未来功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM