Search code examples
javaintellij-ideaaop

Java native AOP | Imports in Aspect required


While playing around with native AspectJ in IntelliJ I noticed something strange: An advice is not executed if I don't have the import(s) of the class(es) that I use in the corresponding pointcut. For example, assume the following pointcut:

public pointcut changeObject():
        call(void MyObject.change());

The execution of the advice now depends on the fact whether I have an import statement of MyObject or not. If it's not present, it compiles without errors but I get the message: "Warning:(8, 0) ajc: no match for this type name: MyObject [Xlint:invalidAbsoluteTypeName]"

In this tutorial they do not need imports (IntelliJ also marks the import as unnecessary). So I'm wondering what the problem is.

My setup: IntelliJ Ultimate 2017.1 with enabled AspectJ plugin. I installed AspectJ 1.8.10 and referenced the Ajc compiler in the compiler settings of the IDE. In the projects structure I referenced aspectjrt.jar in "Modules | Dependencies".

Do I miss anything or could it be related to the fact that Intellij has not the best AspectJ support?


Solution

  • After having tested this situation in eclipse with the AOP plugins I found an answer: It is necessary to import classes that are used in pointcuts and advices.

    At the moment, IntelliJ really has no great (native) AOP support as 1) it does not suggest to import classes that are used in pointcuts but rather 2) signals they are unused (and actually removes them when optimizing imports). Furhtermore, eclipse shows a "no match for this type name" warning which IntelliJ does not. Finally, is also superior in the way that it highlights adviced code sections.