簡體   English   中英

指定 @Morph 參數時無法運行 ByteBuddy 攔截器

[英]Not able to run ByteBuddy interceptor when @Morph argument is specified

我需要根據一些輸入創建一個自定義類。 我的自動取款機是這樣的:

    final Class service = ...;
    final Method method = ...;

    final DynamicType.Unloaded unloadedType = new ByteBuddy()
        .subclass(Object.class)
        .name(service.getClass().getSimpleName() + "DynamicResolver")
        .defineMethod(
            endpointName,
            resolveReturnType(method),
            Modifier.PUBLIC)
       .withParameters(parameters)
       .intercept(MethodDelegation
            .withDefaultConfiguration()
            .withBinders(Morph.Binder.install(Morphing.class))
            .to(interceptor).andThen(
                MethodCall.invoke(method).on(service).withArgument(arguments)
            ))
       .make()

我在這里所做的是創建一個 class ,它使用一種委托給提供的方法。 但是,創建方法和委托方法的參數有些不同。 創建的方法還有一個參數(在parameters中)。 創建的方法不采用該參數,因此arguments數組具有參數索引(少一個參數)。

到目前為止還可以。 現在,我需要在調用委托方法時添加額外的參數。 為了示例的簡單起見,假設我們必須再添加一個字符串來委托調用。

正如我從文檔中看到的,操作 arguments 的方法是使用@Morph 所以我做了:

public interface Morphing<T> {
    T invoke(Object[] args);
}

和我的攔截器:

public class Interceptor {
    @RuntimeType
    public Object invoke(
        @Morph final Morphing<Integer> m,
        @AllArguments final Object[] args
    ) {
        System.out.println("!!!");
        return m.invoke(args);
    }
}

不幸的是,這不起作用。 當我刪除@Morph參數時,攔截器被調用。

我在這里想念什么?

編輯: @Morph是否僅用於子類而不是在委派給另一個實例時?

EDIT2:示例

Byte Buddy 正在綁定Object class 的方法,以便不再觸發所需的攔截器。 您可以在 withDefaultConfiguration() 之后添加filter(isDeclaredBy(MyInterceptor.class)) withDefaultConfiguration()以避免這種情況。 這樣做,您將得到一個異常,即 Byte Buddy 無法綁定您的任何方法。

@Morph使 class 不合格的原因是沒有要調用的超級方法。 在您的示例中,您正在定義一個沒有原始實現的新方法。 您需要覆蓋現有方法才能使用注釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM