0

I am using AspectJ for logging when tests execution only, so I use load time weaving. I pack Interceptor to a jar file to use with another Maven project. But with below config, aspectjweaver will weave methods of external libraries. I want it only weave my source code (include test) without specific config like <include within="hello.*"/>, for generic using like dependency.

Sorry, my English is quite bad. Thanks so much !!!

In aop.xml of this jar file, it likes

<aspectj>
<aspects>
    <aspect name="log.Interceptor"/>
    <weaver options="-verbose -showWeaveInfo">
        <include within="*"/>
    </weaver>
</aspects>
</aspectj>

// Interceptor

pointcut traceMethods() : (execution(* *(..)) && !cflow(within(Interceptor)) && !within(*Test) && !within(Test*) && !within(*Tests) && !within(*TestCase));
before(): traceMethods(){
    Method method = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod();
    logDebug(method, LogPattern.METHOD_START);
}
after(): traceMethods(){
    Method method = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod();
    logDebug(method, LogPattern.METHOD_FINISH);
}` 

1 Answers1

0

Well, you cannot eat the cake and keep it at the same time. So either your solution is generic with pointcuts targeting the whole world or it is specific via includes and/or excludes.

  • What you could try is to use an additional META-INF/aop.xml locally when building or deploying projects using you generic library and specify in-/excludes there. As far as I remember AspectJ will find all those files on the classpath and merge the settings found there.

  • Alternatively, you can define an abstract pointcut and tell your users to make it concrete in an aop.xml file in their project.

Both suggested solutions should work, but they require your library's users to have a basic understanding of AspectJ or at least pointcut syntax. Either you do that and document your library accordingly with sample pointcuts or whole sample AOP config files or you have to be more specific in your own pointcuts and/or in-/excludes. There is no magic solution, the AspectJ weaver cannot guess what you want to weave and what not.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • I thought there was the syntax stand for source code but maybe no. So I will use post-compile time or compile time weaving to weave only source code. Thanks for your help. – Nghĩa Nguyễn Feb 23 '20 at 12:21
  • Actually I don't understand what you just said. Anyway, if you feel like I was able to answer your question (even though you might not like the content of the answer) then please accept the answer in order to close the question. Thank you very much. – kriegaex Feb 23 '20 at 13:55
  • Sorry, this is the first time I use Stackoverflow with my friend, google translate. I accepted the answer. – Nghĩa Nguyễn Feb 23 '20 at 16:23