Aspectj not working with eclipse Luna

I am trying to use aspectJ with eclipse 4.4.0 (Luna). Y cannot find any ajdt plugin suitable for this version of eclipse.

In any case, I think aspectj should work, so I put javascript in the lib directory of my webApp and create this Aspect class:

@Aspect public class LoggerAspect { @Before("(execution(* *.*(..))") public void intercept(JoinPoint jp) { System.out.print("before"); } @After("(execution(* *.*(..))") public void afterReturning(JoinPoint jp) { System.out.print("after"); } @AfterThrowing(pointcut = "execution(* *.*(..))") public void logException(JoinPoint jp, Exception t) { System.out.print("throwing"); } } 

But nothing is registered. What am I missing?

+6
source share
2 answers

Then try using Kepler if you want to use AJDT. ;-)

By the way, did you compile your aspect and Java classes with ajc (AspectJ compiler)? Or do you want to compile them using javac? In this case, you need to weave aspects at runtime (LTW, weaving at boot time), and you need a weaving agent ( -javaagent:path/to/aspectjweaver.jar ) plus the corresponding configuration in aop.xml . After you tell me what you want to do, I can update this answer with a concrete example.


Update: Ok, I checked your aspect. It contains several syntax errors and other problems, for example.

  • In @Before("(execution(* *.*(..))") there the number brackets do not match. Instead, it should be @Before("execution(* *.*(..))") .
  • Annotations after throwing skip the exception binding. It should be @AfterThrowing(pointcut = "execution(* *.*(..))", throwing = "t") .

Now you can fully test the standalone example:

Driver Application:

 class Application { public static void main(String[] args) { new Application().foo(); } public void foo() { try { sayHello("world"); } catch (Exception e) { System.out.println("Caught exception: " + e); } } public void sayHello(String recipient) { System.out.println("Hello " + recipient + "!"); throw new RuntimeException("Oops!"); } } 

Format:

 import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class LoggerAspect { @Before("execution(* *.*(..))") public void intercept(JoinPoint jp) { System.out.println("before " + jp); } @After("execution(* *.*(..))") public void afterReturning(JoinPoint jp) { System.out.println("after " + jp); } @AfterThrowing(pointcut = "execution(* *.*(..))", throwing = "t") public void logException(JoinPoint jp, Exception t) { System.out.println("throwing " + jp); } } 

How to compile and run using ajc:

Assuming your source files are in a directory called src and you want the class files to end up in bin , this is how you compile:

 ajc -sourceroots src -1.7 -d bin -cp aspectjrt.jar 

Now run the application:

 java -cp bin;aspectjrt.jar Application before execution(void Application.main(String[])) before execution(void Application.foo()) before execution(void Application.sayHello(String)) Hello world! after execution(void Application.sayHello(String)) throwing execution(void Application.sayHello(String)) Caught exception: java.lang.RuntimeException: Oops! after execution(void Application.foo()) after execution(void Application.main(String[])) 

How to compile with javac and work with LTW:

You need to compile debug characters ( -g ):

 javac -g -cp aspectjrt.jar -d bin src\*.java 

Before starting the program, you will need the LTW configuration in the bin\META-INF\aop.xml (or aop-ajc.xml ):

 <?xml version="1.0" encoding="UTF-8"?> <aspectj> <aspects> <aspect name="LoggerAspect"/> </aspects> <weaver> <include within="*"/> </weaver> </aspectj> 

Now you can start the application using weaving:

 java -javaagent:aspectjweaver.jar -cp bin Application 

The result is the same, but if you want to see more, you can add other weaver parameters, for example

 <weaver options="-verbose -showWeaveInfo"> 

This should output the following result:

 [ AppClassLoader@58644d46 ] weaveinfo Join point 'method-execution(void Application.main(java.lang.String[]))' in Type 'Application' (Application.java:3) advised by before advice from 'LoggerAspect' (LoggerAspect.java) [ AppClassLoader@58644d46 ] weaveinfo Join point 'method-execution(void Application.main(java.lang.String[]))' in Type 'Application' (Application.java:3) advised by after advice from 'LoggerAspect' (LoggerAspect.java) [ AppClassLoader@58644d46 ] weaveinfo Join point 'method-execution(void Application.main(java.lang.String[]))' in Type 'Application' (Application.java:3) advised by afterThrowing advice from 'LoggerAspect' (LoggerAspect.java) [ AppClassLoader@58644d46 ] weaveinfo Join point 'method-execution(void Application.foo())' in Type 'Application' (Application.java:8) advised by before advice from 'LoggerAspect' (LoggerAspect.java) [ AppClassLoader@58644d46 ] weaveinfo Join point 'method-execution(void Application.foo())' in Type 'Application' (Application.java:8) advised by after advice from 'LoggerAspect' (LoggerAspect.java) [ AppClassLoader@58644d46 ] weaveinfo Join point 'method-execution(void Application.foo())' in Type 'Application' (Application.java:8) advised by afterThrowing advice from 'LoggerAspect' (LoggerAspect.java) [ AppClassLoader@58644d46 ] weaveinfo Join point 'method-execution(void Application.sayHello(java.lang.String))' in Type 'Application' (Application.java:16) advised by before advice from 'LoggerAspect'(LoggerAspect.java) [ AppClassLoader@58644d46 ] weaveinfo Join point 'method-execution(void Application.sayHello(java.lang.String))' in Type 'Application' (Application.java:16) advised by after advice from 'LoggerAspect' (LoggerAspect.java) [ AppClassLoader@58644d46 ] weaveinfo Join point 'method-execution(void Application.sayHello(java.lang.String))' in Type 'Application' (Application.java:16) advised by afterThrowing advice from 'LoggerAspect' (LoggerAspect.java) before execution(void Application.main(String[])) before execution(void Application.foo()) before execution(void Application.sayHello(String)) Hello world! after execution(void Application.sayHello(String)) throwing execution(void Application.sayHello(String)) Caught exception: java.lang.RuntimeException: Oops! after execution(void Application.foo()) after execution(void Application.main(String[])) 

If you need even more, add the -debug option to the weaver options.

+4
source

If you configured the AspectJ Maven plugin in your pom.xml, then for this to work in Luna, you first need to add the following update site (menu "Help", "Install New Software"):

http://download.eclipse.org/tools/ajdt/44/dev/update/

From this site, install AspectJ Development Tools.

After installation, you can also install Maven Integration for AJDT through Preferences, Maven, Discovery, Open Directory, and then select AspectJ m2e Configurator.

(Attempting to complete the last step before installing the tools will result in no requirement: AspectJ Development Tools Core 2.1.3. The same error occurs when trying to install AspectJ things from the default Luna update sites.)

+20
source

Source: https://habr.com/ru/post/972791/


All Articles