0

カスタム コンストラクターで ByteBuddy を使用してクラスを動的に作成しようとしています。Byte Buddy を使用した Intercepting デフォルト コンストラクターを読み、それに基づいて次のコード ベースを作成しました。

Class<?> dynamicType = new ByteBuddy().subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS)
                .name("foo").defineConstructor(Modifier.PUBLIC).withParameters(int.class)
                .intercept(
                        to(new Object() {
                            public void construct() throws Exception {
                                System.out.println("before constructor");
                            }
                        })
                        .andThen(MethodCall.invoke(Object.class.getConstructor()))
                        .andThen(to(new Object() {
                            public void construct() throws Exception {
                                System.out.println("after constructor");
                            }})
                        ))
                .make()
                .load(Main.class.getClassLoader(), INJECTION)
                .getLoaded();

        dynamicType.getConstructor(int.class).newInstance(3);

私の質問は、スーパーコンストラクターを呼び出す前後に追加したカスタムコードで、「foo」コンストラクターの整数引数にどのようにアクセスできるかです。

4

1 に答える 1

1

もちろん、注釈 @Argument(0) を使用してパラメーターを定義するだけです。

パッケージのプライベートな可視性がトリッキーな結果をもたらす可能性があるため、匿名クラスを使用しないことをお勧めします。

于 2020-11-05T19:06:39.167 に答える