简体   繁体   English

如何使用 ByteBuddy 创建默认构造函数?

[英]How to create a default constructor with ByteBuddy?

I use ByteBuddy and I have this code:我使用 ByteBuddy 并且有以下代码:

public class A extends B {
    public A(String a) {
        super(a);
    }

    public String getValue() {
        return "HARDCODED VALUE";
    }
}

public abstract class B {
    private final String message;

    protected B(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

My current generation code is:我当前的生成代码是:

Constructor<T> declaredConstructor;

try {
    declaredConstructor = A.class.getDeclaredConstructor(String.class);
} catch (NoSuchMethodException e) {
    //fail with exception..
}

new ByteBuddy()
    .subclass(A.class, Default.IMITATE_SUPER_CLASS)
    .name(A.class.getCanonicalName() + "$Generated")
    .defineConstructor(Visibility.PUBLIC)                               
    .intercept(MethodCall.invoke(declaredConstructor).with("message"))                                                                         
    .make()        
    .load(tClass.getClassLoader(),ClassLoadingStrategy.Default.WRAPPER)
    .getLoaded()
    .newInstance();

I want to get instance of class A , and also I want to make some actions in constructor after invoke super() , like this:我想获得AA实例,并且我想在调用super()之后在构造函数中进行一些操作,如下所示:

public A(){
   super("message");

   // do something special..
}

I tried implement with MethodDelegation.to(DefaultConstructorInterceptor.class) , but I didn't succeed.我尝试使用MethodDelegation.to(DefaultConstructorInterceptor.class) ,但没有成功。

The JVM requires you to hard-code the super method call into a method what is not possible using delegation (also see the javadoc), this is why you cannot use the MethodDelegation to invoke the constructor. JVM 要求您将超级方法调用硬编码为使用委托无法实现的方法(另请参阅 javadoc),这就是您不能使用MethodDelegation来调用构造函数的原因。 What you can do is to chain the method call you already have and the delegation by using composition by the andThen step as in:您可以做的是通过使用andThen步骤的组合来链接您已经拥有的方法调用和委托,如下所示:

MethodCall.invoke(declaredConstructor).with("message")
  .andThen(MethodDelegation.to(DefaultConstructorInterceptor.class));

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

相关问题 如何使用ByteBuddy创建没有公共构造函数的类的动态代理 - How to create dynamic proxy of class with no public constructor using ByteBuddy 在ByteBuddy中从头开始创建一个类构造函数 - Create a class constructor from scratch in ByteBuddy 在使用 ByteBuddy 创建的构造函数中创建自定义代码 - Create custom code in a constructor created with ByteBuddy ByteBuddy如何创建一个class的文件? - ByteBuddy how to create a class file? ByteBuddy 如何在 Android 中创建另一个 ByteBuddy 创建的类的子类? - ByteBuddy how to create a subclass of another ByteBuddy created Class in Android? 如何在没有默认构造函数的情况下创建类 - How to create class with no default constructor 当我在ByteBuddy生成的类中创建自定义构造函数时出现异常 - Exception arised when I create custom constructor in ByteBuddy generated class 如何使用 ByteBuddy 创建未绑定泛型类型的代理? - How to create a proxy of an unbound generic type with ByteBuddy? 如何使用 ByteBuddy 代理处理构造函数抛出的异常? - How to take the exception thrown by a constructor using a ByteBuddy agent? 如何使用ByteBuddy使用1+ args构造函数对抽象类进行子类化 - How to subclass an abstract class with 1+ args constructor using ByteBuddy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM