2015-10-15 62 views
2

我有一堆運行在普通JDK上的WebServices,我需要攔截所有公共方法才能做某件事。有些方法使用@WebParam註釋。使用ByteBuddy子類化WebService會從重寫方法中刪除@WebParam註釋,並且該服務不再按預期工作。ByteBuddy和註釋

下面是一個示例簽名

public Something fetch(@WebParam(name="Query") QueryCriteria query) 

這裏就是我如何使用ByteBuddy

new ByteBuddy() 
    .subclass(implementationClass) 
    .method(isPublic().and(isDeclaredBy(implementationClass))) 
    .intercept( 
      MethodDelegation.to(new WebServiceInterceptor()) 
      .andThen(SuperMethodCall.INSTANCE)) 
    .annotateType(implementationClass.getAnnotations()) 
    .make() 

我知道有一種方法來標註的參數,但它需要對方法參數的專門知識(因爲只有一些參數被註釋)。我想要做的就是讓ByteBuddy以與超類相同的方式註釋我的類,包括所有重寫方法的所有參數。

subclass(implementationClass) 
.annotateType(LIKE_SUPERCLASS) 
.method() 
.intercept(...) 
.annotateMethod(LIKE_SUPER_INCLUDING_PARAMS) 

任何想法?

BR,Paci

回答

2

自己找到解決方案。

new ByteBuddy() 
    .withAttribute(new TypeAttributeAppender.ForInstrumentedType(AnnotationAppender.ValueFilter.AppendDefaults.INSTANCE)) 
    .withDefaultMethodAttributeAppender(new MethodAttributeAppender.ForInstrumentedMethod(AnnotationAppender.ValueFilter.AppendDefaults.INSTANCE)) 

將做的伎倆。

Br,Paci