Reputation: 3
There is a method testFoo
in my code which looks like this:
newClassBuilder = newClassBuilder.method(ElementMatchers.nameStartsWith("test"))
.intercept(MethodDelegation.to(new InstMethodsInter(new MethodInterceptor1())));
newClassBuilder = newClassBuilder.method(ElementMatchers.named("testFoo"))
.intercept(MethodDelegation.to(new InstMethodsInter(new MethodInterceptor2())));
newClassBuilder = newClassBuilder.method(ElementMatchers.nameEndsWith("Foo"))
.intercept(MethodDelegation.to(new InstMethodsInter(new MethodInterceptor3())));
only MethodInterceptor3 works.
I have lots of interceptors.
What do I need to do?
Upvotes: 0
Views: 39
Reputation: 44032
You have to add a most-specific matcher yourself and specify the interceptors yourself there:
newClassBuilder.method(ElementMatchers.nameStartsWith("test").and(ElementMatchers.named("testFoo"))
.intercept(MethodDelegation.to(new InstMethodsInter(new MethodInterceptor1()).andThen(MethodDelegation.to(InstMethodsInter(new MethodInterceptor2()))));
This is necessary as the later matcher override any previous matcher that also matches a method. The most specific matcher needs to be applied last and interceptors are not stacked.
However, it seems like you should refactor your interception logic here to make this unnecessary.
Upvotes: 0