Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aspectj java.util.ArrayList or jdk not working

Tags:

java

aspectj

I am learning AOP, reading tutorials, and start using AspectJ. My goal is to "aspect" any jdk files, so started from simplest example java.util.ArrayList, also tried for any jdk filed but with no luck. Reading tutorials, watching YouTube, but cant find anything about jdk. How to @Aspect jdk? I am using IntelliJ for project setup.

@Aspect
public class MyAspect {
    @After("execution(* java.util.ArrayList.*(..))")
    public void afterArrayListAspect() {
        System.out.println("afterArrayListAspect");
    }
}

Main class:

public class Main {
    public static void main(String[] args) {
        ArrayList<String> dd = new ArrayList<>();
        dd.add("added");
        System.out.println(dd.get(0));
    }
}

pom.xml file:

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.21</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.21</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.15.0</version>
            <configuration>
                <complianceLevel>11</complianceLevel>
                <source>11</source>
                <target>11</target>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <encoding>UTF-8</encoding>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
like image 839
Adolis Pali Avatar asked Nov 28 '25 18:11

Adolis Pali


1 Answers

As AspectJ compiler compiles your own classes or optionally also weaves aspect code into third-party classes on the inpath (in AspectJ Maven exposed as weaveDependencies).

However, JDK classes are not on any regular class path, but on the JVM bootstrap class path. Therefore, compilers like javac (via Maven Compiler) or ajc (via AspectJ Maven) do not see those classes. You cannot just modify core JDK classes like classes loaded by the JDK. In theory, you can of course apply binary weaving on classes extracted from the JDK, creating a new, woven JDK, replace the original JDK with it and then run your programs with it, but that is not an option to most users.

Anyway, what you can do is not to use execution (which is woven into the target class of the method call) but call (which is woven into the calling class). That will solve your problem, because in your example you are in control of the calling class and can therefore expose it to the AspectJ compiler. However, call will of course not work, if the call comes from the JDK internally, because then also the caller is outside of the compiler's control for the same reasons explained above.

like image 175
kriegaex Avatar answered Dec 01 '25 07:12

kriegaex