AspectJ non-Spring annotations not intercepting adviced methods - java

I wanted to use AOP-styled annotations for #Around advice in a non-Spring project. I was able to make up some code, but it's been giving me trouble - instead of seeing the console output as coded in the Advice I can only see the SampleClass method's printout.
Providing a minimal, reproductible example of my code. Hoping to get some hint on how to get it working. Thanks!
Main.java
package pl.bart;
public class Main {
public static void main(String[] args) {
System.out.println(new SampleClass().a());
}
}
SampleClass.java
package pl.bart;
public class SampleClass {
#Annotation
public String a() {
return "Hello from sample class";
}
}
Annotation.java
package pl.bart;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface Annotation {
}
Advice.java
package pl.bart;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
#Aspect
public class Advice {
#Pointcut("#annotation(Annotation)")
public void callAt() {
}
#Around("callAt()")
public Object advice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("Hello from advice");
return proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
}
}
META-INF/aop.xml
<aspectj>
<aspects>
<aspect name="pl.bart.Advice"/>
<weaver options="-verbose -showWeaveInfo">
<include within="pl.bart.*"/>
</weaver>
</aspects>
</aspectj>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.bart</groupId>
<artifactId>aspectj-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}"/org/aspectj/
aspectjweaver/1.8.9/
aspectjweaver-1.8.9.jar
</argLine>
<useSystemClassLoader>true</useSystemClassLoader>
<forkMode>always</forkMode>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.14.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

The are three mistakes in you project.
1.
#Pointcut("#annotation(Annotation)")
public void callAt() {
}
use fully qualified names of classes, i.e. pl.bart.Annotation instead of Annotation - it costs you nothing but saves a lot of time, in particular aspectj 1.5.4 "does not support" simple names.
2.
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
aspectj does some magic with bytecode, and it is too naive to expect the library released 12 years ago supports java 17, switch to the recent aspectj 1.9.9, also note you need to add --add-opens java.base/java.lang=ALL-UNNAMED to JVM arguments.
3.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.14.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
if you are going to use load time weaving and you don't use native aspectJ (.aj) syntax, you do not need aspectj-maven-plugin - it compiles .aj files and performs compile time weaving.

Related

Java cannot access Kotlin's Companion

I am trying to use kotlinx serialization in java code. The problem I encounter is that I cannot use Companion object in java code to access it's static serializer() method which is generated by kotlin serializaton plugin.
Here is the kotlin code that decalres a serializable class:
#kotlinx.serialization.Serializable
data class MyData(private val data: String)
My mavem pom.xml file is shows below
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>TestProject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<kotlin.version>1.7.0</kotlin.version>
<serialization.version>1.3.3</serialization.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-serialization-protobuf-jvm -->
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-json-jvm</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
<compilerPlugins>
<plugin>kotlinx-serialization</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-serialization</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
And java code I wrote is below(it does not compile because it cannot find Companion object):
public class Main {
public static void main(String[] args) {
MyData.Companion.serializer();
}
}
I can access Companion objects of other kotlin classes(for example, the Companio.serializer() of JsonElement) but cannot access Companion that is autogenerated by kotlinx serialization plugin.
Also, the target class that is autogenerated contains the Companion object, here is the code:
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
#kotlinx.serialization.Serializable public final data class MyData public constructor(data: kotlin.String) {
public companion object {
}
#kotlin.Deprecated public constructor(seen1: kotlin.Int, data: kotlin.String?, serializationConstructorMarker: kotlinx.serialization.internal.SerializationConstructorMarker?) { /* compiled code */ }
private final val data: kotlin.String /* compiled code */
private final operator fun component1(): kotlin.String { /* compiled code */ }
#kotlin.Deprecated public object `$serializer` : kotlinx.serialization.internal.GeneratedSerializer<MyData> {
}
}
UPD: I can access the auto-generated copy method that I tried.
UPD: Here is the screenshot from intellij
This seems to be only a problem with the Kotlin compiler plugin in IntelliJ IDEA. Even though IDEA thinks you have an error in your Java code, you can actually still compile it on the command line (or using Maven or Gradle) and it runs just fine.

AspectJ Aspects not getting triggered in Maven Project

I am trying to build a POC project using AspectJ without using Spring AOP. I am using an annotation based approach where I want to run the aspect #Around the method which has been annotated with an annotation. For some reason my aspects don't get triggered. Below is my code:
pom.xml
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<goals>
<!-- use this goal to weave all your main classes -->
<goal>compile</goal>
<!-- use this goal to weave all your test classes -->
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
resources/META-INF/aop.xml
<aspectj>
<aspects>
<aspect name="com.aditya.personal.aspects.MetricsAspect"/>
<weaver options="-verbose -showWeaveInfo">
<include within="com.carrot.personal.aspects.*"/>
</weaver>
</aspects>
</aspectj>
My Aspect:
#Aspect
public class DataTrackAspect {
#Around("#annotation(com.carrot.personal.aspects.DataTrackEnabled)")
public Object performAspect(ProceedingJoinPoint joinPoint, DataTrackEnabled dataTrackEnabled) throws Throwable {
Object result = joinPoint.proceed();
DataTrackHelper dataTrackHelper = new DataTrackHelper();
dataTrackHelper.recordInstanceCount(dataTrackEnabled.dataClass(), dataTrackEnabled.dataName(), dataTrackEnabled.instance());
return result;
}
}
The annotated method
#DataTrackEnabled(dataClass = "Hey", dataName = "There", instance = "How are you?")
public Map<String, String> fetchUser() {
Map<String, String> returnable = new HashMap<>();
returnable.put("firstName", "carrot");
returnable.put("lastName", "radish");
return returnable;
}
I can't seem to figure it out as to what am I missing. I have uploaded the sample code on GitHub here.
You are using AspectJ Maven Plugin, i.e. you are going to use compile-time weaving. Therefore, you do not need aop.xml because that one is used for load-time weaving. So you can delete it.
During compilation you should get a compile error:
Unbound pointcut parameter 'dataTrackEnabled'
That tells you that your advice method has a parameter dataTrackEnabled which does not occur anywhere in the pointcut as it should. So just change the pointcut from
#Around("#annotation(com.carrot.personal.aspects.DataTrackEnabled)")
to
#Around("#annotation(dataTrackEnabled)")
As you see, I am referring to the method parameter name from the pointcut. If you would not bind the annotation to a parameter, then you would need the fully qualified class name as you used it before.
Also, your Maven POM should cause this error:
diamond operator is not supported in -source 1.5
This is because using AspectJ Maven does not automatically deactivate or replace Maven Compiler Plugin and the latter complains that you did not set source and target versions for it. So you have two options:
Deactivate Maven Compiler, let AspectJ Maven compile your Java and aspect classes.
Set the compiler level to 1.8 (or simply 8) for Maven Compiler and use the same settings for AspectJ Maven.
I will choose option no. 2 in this case. Maven knows properties named maven.compiler.source and maven.compiler.target. Let us define and use them:
<!-- (...) -->
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- (...) -->
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<!-- (...) -->
Now there are a few more warnings:
Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
bad version number found in C:\Users\alexa\.m2\repository\org\aspectj\aspectjrt\1.8.9\aspectjrt-1.8.9.jar expected 1.8.2 found 1.8.9
The first two are just because you forgot to set <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>. While you are at it, you can set the encoding for generated reports, too: <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>.
The last one is aspect-related and due to the fact that AspectJ Maven 1.7 depends on AspectJ 1.8.2, not 1.8.9. But be careful! It depends on aspectjtools (for using the compiler), not on aspectjweaver (for load-time weaving). You can either upgrade the version or override the dependency inside the plugin. I am going to show you both at the same time, just in case.
Last but not least, the latest version of AspectJ 1.8 is 1.8.13. But you can just use the very latest version 1.9.6, it supports up to Java 14 (the next version for Java 15+16 is almost ready) and can still produce byte code for Java 8.
How about this POM?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.carrot</groupId>
<artifactId>SO_AJ_MavenProjectNotWorking_66734262</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<aspectj.version>1.9.6</aspectj.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<goals>
<!-- use this goal to weave all your main classes -->
<goal>compile</goal>
<!-- use this goal to weave all your test classes -->
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</project>
Now when you run the program, you should see something like:
Recording: dataClass = Hey, dataName = There, instance = How are you?
Recording: dataClass = Hey, dataName = There, instance = How are you?
You may ask why the advice is triggered twice. Well, just because #annotation(dataTrackEnabled) captures both the method call() and the method execution() pointcut. So let us limit the match to just executions.
The full solution looks like this (do not forget RUNTIME retention for your annotation):
package com.carrot.personal.aspects;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
#Retention(RetentionPolicy.RUNTIME)
public #interface DataTrackEnabled {
String dataClass();
String dataName();
String instance();
}
package com.carrot.personal.app;
public class DataTrackHelper {
public void recordInstanceCount(String dataClass, String dataName, String instance) {
System.out.println("Recording: dataClass = " + dataClass + ", dataName = " + dataName + ", instance = " + instance);
}
}
package com.carrot.personal.app;
import com.carrot.personal.aspects.DataTrackEnabled;
import java.util.HashMap;
import java.util.Map;
public class Application {
public static void main(String[] args) {
System.out.println(new Application().fetchUser());
}
#DataTrackEnabled(dataClass = "Hey", dataName = "There", instance = "How are you?")
public Map<String, String> fetchUser() {
Map<String, String> returnable = new HashMap<>();
returnable.put("firstName", "carrot");
returnable.put("lastName", "radish");
return returnable;
}
}
package com.carrot.personal.aspects;
import com.carrot.personal.app.DataTrackHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
#Aspect
public class DataTrackAspect {
#Around("#annotation(dataTrackEnabled) && execution(* *(..))")
public Object performAspect(ProceedingJoinPoint joinPoint, DataTrackEnabled dataTrackEnabled) throws Throwable {
System.out.println(joinPoint);
Object result = joinPoint.proceed();
DataTrackHelper dataTrackHelper = new DataTrackHelper();
dataTrackHelper.recordInstanceCount(dataTrackEnabled.dataClass(), dataTrackEnabled.dataName(), dataTrackEnabled.instance());
return result;
}
}
I added some more log output, so let us run the application again:
execution(Map com.carrot.personal.app.Application.fetchUser())
Recording: dataClass = Hey, dataName = There, instance = How are you?
{firstName=carrot, lastName=radish}

AspectJ 1.9.4 with OpenJDK 11 Without Spring Context not working as a dependent module

I am trying to create custom loadtime annotations with AspectJ, Open JDK11 without Spring Context. It works fine within a module and annotations are weaving at class load time and aspects are executing at runtime. No issues, But when aspectJ implemented module added as a dependency on another module. AspectJ and annotations are not processing. Am I missing any configuration?
module-a
#Documented
#Inherited
#Target(METHOD)
#Retention(RUNTIME)
public #interface Counter {
String name() default "";
}
#Aspect
public class CounterAspect {
#Around("execution(* *.*(..)) && #annotation(counter)")
public void myBeforeLogger(ProceedingJoinPoint joinPoint, Counter counter) {
System.out.println("Okay - we're in the before handler...");
System.out.println("The test annotation value is: " + counter.name().toString());
}
resources/META-INF/aop.xml
<aspectj>
<aspects>
<aspect name="CounterAspect"/>
</aspects>
<weaver options="-verbose">
<!-- weave anything -->
<include within="*" />
</weaver>
</aspectj>
pom.xml
<properties>
<aspectj.version>1.9.4</aspectj.version>
</properties>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
module-b
pom.xml
<dependency>
<groupId>org.mymodule</groupId>
<artifactId>module-a</artifactId>
<version>1.0.0</version>
</dependency>
public class MyCounter {
#Counter(name="call_count")
public void count() {}
}
public class MyCounterTest {
#Test
public void testCount() {
MyCounter counter = new MyCounter();
counter.count();
//NOTE: I expect CounterAspect from module-a should be intercepted. It is not working!!!! There is no aspectJ class loading verbose in console.
}
}
I suggest you create a parent POM with some predefined properties, dependency versions and plugin configurations. Your two sub-modules (one for application + test, one for aspect) can use these definitions, which leads to shorter sub-module POMs:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.scrum-master</groupId>
<artifactId>aspectj-ltw-test-multi-module</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.source-target.version>11</java.source-target.version>
<aspectj.version>1.9.4</aspectj.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.source-target.version}</source>
<target>${java.source-target.version}</target>
<!-- IMPORTANT -->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
<plugin>
<groupId>com.nickwongdev</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.12.1</version>
<configuration>
<!--<showWeaveInfo>true</showWeaveInfo>-->
<source>${java.source-target.version}</source>
<target>${java.source-target.version}</target>
<Xlint>ignore</Xlint>
<complianceLevel>${java.source-target.version}</complianceLevel>
<encoding>${project.build.sourceEncoding}</encoding>
<!--<verbose>true</verbose>-->
<!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
</configuration>
<executions>
<execution>
<!-- IMPORTANT -->
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>de.scrum-master</groupId>
<artifactId>aspect</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>aspect</module>
<module>application</module>
</modules>
</project>
Please note that I am not using the original AspectJ Maven plugin but a fork which supports Java 11. The original still lags behind. As you said you want to use Java 11, you have to do that. With Java 8 you can use the original plugin.
Aspect module:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.scrum-master</groupId>
<artifactId>aspectj-ltw-test-multi-module</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>de.scrum-master</groupId>
<artifactId>aspect</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.nickwongdev</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>
</project>
<aspectj>
<aspects>
<aspect name="de.scrum_master.aspect.CounterAspect"/>
</aspects>
<weaver options="-verbose">
<!-- weave anything -->
<include within="*"/>
</weaver>
</aspectj>
package de.scrum_master.aspect;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
#Documented
#Inherited
#Target(METHOD)
#Retention(RUNTIME)
public #interface Counter {
String name() default "";
}
package de.scrum_master.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
#Aspect
public class CounterAspect {
#Around("execution(* *.*(..)) && #annotation(counter)")
public void myBeforeLogger(ProceedingJoinPoint joinPoint, Counter counter) {
System.out.println(joinPoint + " -> " + counter.name());
}
}
Application module:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.scrum-master</groupId>
<artifactId>aspectj-ltw-test-multi-module</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>de.scrum-master</groupId>
<artifactId>application</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>de.scrum-master</groupId>
<artifactId>aspect</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
package de.scrum_master.app;
import de.scrum_master.aspect.Counter;
public class MyCounter {
#Counter(name = "call_count")
public void count() {}
}
package de.scrum_master.app;
import org.junit.Test;
public class MyCounterTest {
#Test
public void testCount() {
new MyCounter().count();
}
}
Now if you run mvn clean verify or just mvn clean test you will get the following console log for the application module test:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
[AppClassLoader#2aae9190] info AspectJ Weaver Version 1.9.4 built on Friday May 10, 2019 at 08:43:10 PDT
[AppClassLoader#2aae9190] info register classloader jdk.internal.loader.ClassLoaders$AppClassLoader#2aae9190
[AppClassLoader#2aae9190] info using configuration (...)/SO_AJ_LTWTestMultiModule_56397605/aspect/target/classes/META-INF/aop.xml
[AppClassLoader#2aae9190] info register aspect de.scrum_master.aspect.CounterAspect
[IsolatedClassLoader#74e52ef6] info AspectJ Weaver Version 1.9.4 built on Friday May 10, 2019 at 08:43:10 PDT
[IsolatedClassLoader#74e52ef6] info register classloader org.apache.maven.surefire.booter.IsolatedClassLoader#74e52ef6
[IsolatedClassLoader#74e52ef6] info using configuration (...)/SO_AJ_LTWTestMultiModule_56397605/aspect/target/classes/META-INF/aop.xml
[IsolatedClassLoader#74e52ef6] info register aspect de.scrum_master.aspect.CounterAspect
Running de.scrum_master.app.MyCounterTest
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access using Lookup on org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor (file:/(...)/.m2/repository/org/aspectj/aspectjweaver/1.9.4/aspectjweaver-1.9.4.jar) to class java.lang.ClassLoader
WARNING: Please consider reporting this to the maintainers of org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[AppClassLoader#2aae9190] info processing reweavable type de.scrum_master.aspect.CounterAspect: de\scrum_master\aspect\CounterAspect.aj
[AppClassLoader#2aae9190] info successfully verified type de.scrum_master.aspect.CounterAspect exists. Originates from de\scrum_master\aspect\CounterAspect.aj
execution(void de.scrum_master.app.MyCounter.count()) -> call_count
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.885 sec
Please especially note
execution(void de.scrum_master.app.MyCounter.count()) -> call_count
This is what you want, the load-time-woven aspect kicks in for your test.
Update: I created a GitHub repository for you. You can just clone it instead of copying all my code snippets from this answer.

Creating a sample AspectJ-project within IntelliJ IDEA

I'm setting up a project within IntelliJ IDEA based on Maven and AspectJ. Unfortunately, everything tried fails (i.e. the aspect seems to be ignored).
I created the smallest possible testproject with a maven-file. It looks like this:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>com.nickwongdev</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.12.1</version>
<configuration>
<complianceLevel>11</complianceLevel>
<source>11</source>
<target>11</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>de.benchmarktest.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
I had to use aspectj-maven-plugin from com.nickwongdev, to be able to compile with Java 11.
The idea was taken from an example with a simple Account-class:
package de.benchmarktest;
public class Account {
int balance = 20;
public boolean withdraw(int amount) {
balance -= amount;
return true;
}
}
The AccountAspect.aj looks like this:
package de.benchmarktest;
public aspect AccountAspect {
pointcut callWithDraw(int amount, Account acc) :
call(boolean de.benchmarktest.Account.withdraw(int)) && args(amount) && target(acc);
boolean around(int amount, Account acc) :
callWithDraw(amount, acc) {
if (acc.balance < amount) return false;
return proceed(amount, acc);
}
}
If I now construct a sample Main-method like so:
package de.benchmarktest;
public class Main {
public static void main(String[] args) {
Account acc = new Account();
System.out.println(acc.withdraw(10));
System.out.println(acc.withdraw(20));
}
}
I'd guess the output should be true, followed by false. Infact, it is two times true. Therefore the Aspect seems not to be triggered.
If running the compilation manually from the console via mvn package, I get [WARNING] No sources found skipping aspectJ compile. How do I teach the system to run the Aspects on compiling the sourcecode? Thanks in advance for your kind help!

Drop wizard AspectJ metrics does not report metrics via JConsole MBeans

I have tried to follow the instructions to get drop wizard AspectJ metrics working, see https://github.com/astefanutti/metrics-aspectj however when I follow these simple instructions and view the metrics through JConsole mbeans there are no metrics reported for my annotated service, just metrics for the resource as is the default drop wizard behaviour.
I am using an old version of drop wizard so that there are no jar dependancy clashes with com.codahale.metrics artefacts
When the application is built hitting the following url produces a random string http://localhost:8080/string
Here is my simple example code:
pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-project</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>test project</name>
<description>try and get aspect j metrics working in dropwizard following instructions on https://github.com/astefanutti/metrics-aspectj</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>io.astefanutti.metrics.aspectj</groupId>
<artifactId>metrics-aspectj</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-annotation</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
<!--
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>io.astefanutti.metrics.aspectj</groupId>
<artifactId>metrics-aspectj</artifactId>
</aspectLibrary>
</aspectLibraries>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Premain-Class>org.aspectj.weaver.loadtime.Agent</Premain-Class>
<Main-Class>com.example.MyApplication</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
MyApplication.java
package com.example;
import com.example.service.MyService;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
public class MyApplication extends Application<MyConfiguration> {
public static void main(String[] args) throws Exception {
MyApplication app = new MyApplication();
app.run(args);
}
#Override
public void initialize(Bootstrap<MyConfiguration> arg0) {
}
#Override
public void run(MyConfiguration configuration, Environment environment) throws Exception {
environment.jersey().register(new MyResource(new MyService()));
}
}
MyConfiguration.java
package com.example;
import io.dropwizard.Configuration;
public class MyConfiguration extends Configuration {
}
MyResource.java
package com.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.codahale.metrics.annotation.Timed;
import com.example.service.MyService;
#Path("/string")
#Produces(MediaType.APPLICATION_JSON)
public class MyResource {
private final MyService service;
public MyResource(MyService service) {
this.service = service;
}
#GET
#Timed
public String getString() {
return "string is [" + this.service.constructString() + "]";
}
}
MyService.java
package com.example.service;
import java.util.Random;
import com.codahale.metrics.annotation.Timed;
import io.astefanutti.metrics.aspectj.Metrics;
#Metrics(registry = "myRegistry")
public class MyService {
private final Random random = new Random();
#Timed(name = "myTimedMethod")
public String constructString() {
byte[] bytes = new byte[20];
this.random.nextBytes(bytes);
return new String(bytes);
}
}
example.yml
logging:
level: INFO
I build the shaded jar on the command line with standard maven command:
mvn clean install
And run the jar with the following command:
java -jar -javaagent:<path-to-maven-repo>/org/aspectj/aspectjweaver/1.8.7/aspectjweaver-1.8.7.jar target/test-project-0.0.1-SNAPSHOT.jar server example.yml
I am not quite sure what I am missing but a day spent searching didn't yield any useful insights.
So it turns out that what you have to do is register your MetricRegistry with a JmxReporter for the metrics to come out on JMX.
It seems that in the latest version of drop wizard you should be able to create a MetricRegistry and add this to the bootstrap metrics in the initialize method of your application
#Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
MetricRegistry myRegistry = SharedMetricRegistries.getOrCreate("myRegistry");
MetricRegistry bootstrapMetricRegistry = bootstrap.getMetricRegistry();
bootstrapMetricRegistry.register("myRegistry", myRegistry);
}
and then when this gets automatically put in a JMX reporter in the registerMetrics method of the bootstrap, however this doesn't seem to work.
My current solution is as follows, in the run method of the application grab the registry from the shared registry and build another JmxReporter. Metrics happily now appear when connecting through JMX.
#Override
public void run(MyConfiguration configuration, Environment environment) throws Exception {
...
JmxReporter.forRegistry(SharedMetricRegistries.getOrCreate("myRegistry")).build().start();
...
}

Categories

Resources