I'm trying to apply different Advices on different classes (scope). I'm configuring it this way:
new AgentBuilder.Default()
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.type((name(equals(com.toto.titi.Class1)) or name(equals(com.toto.titi.Class2))))
.transform(new AgentBuilder.Transformer.ForAdvice()
.advice(ElementMatchers
.isAnnotatedWith(named(annotationClassName))
.or(name(equals(com.toto.titi.Class1))))
, com.tata.Advice1))
.transform(new AgentBuilder.Transformer.ForAdvice()
.advice(ElementMatchers
.isAnnotatedWith(named(annotationClassName))
.or(name(equals(com.toto.titi.Class2))))
, com.tata.Advice2));
The problem that I'm having is that both Advices (com.tata.Advice1 and com.tata.Advice2) are being applied to all classes (com.toto.titi.Class1 and com.toto.titi.Class2). Whereas what I am trying to do is apply only com.tata.Advice1 to com.toto.titi.Class1 and com.tata.Advice2 to com.toto.titi.Class2.
What am I missing? How can I do this differently?
You can chain any amount of matchers with each their transformation. If you apply:
agentBuilder
.type(matcher1).transform(transformer1)
.type(matcher2).transform(transformer2)
this will only apply the first advice on types matched by 1 and vice versa for 2. In your example, you apply:
agentBuilder
.type(matcher1.or(matcher2)).transform(transformer1).transform(transformer2)
You will apply both transformers if either matcher applies. You would need to change your matcher pattern from the second concept to the first.