javaeclipsemavenaspectjload-time-weaving

How is Load-Time Weaving to a .jar file with AspectJ done?


Some project background: I am working with an application that runs on Java. My goal is to collect real-time data to send it to an API running on a local server. The application is packaged into a .jar file and I have no access to source code. (Aside from the question, I have attempted to decompile the class to add additional functionality; however, that did not end well, $access000...)

Having just learned about AOP, I ran to test my options. I spend over a week figuring out Maven Setup and AspectJ with it.

I set up a "helloworld" project in which I have been able to successfully weave aspects (from a .java file) into another .java file. My next step is to create an executable .jar file with the main class and weave the aspect .java class into it - then I would have achieved my desired outcome.

Is it possible to take a precompiled .jar file, put it into the project and write aspects for it?

I am not sure if I am misunderstanding something, I believe that should be something feasable.

From AspectJ Development Environment Guide:

In AspectJ tools, the aspectpath is where to find binary aspects. Like the classpath, it can include archives ( .jar and .zip files) and directories containing .class files in a package layout (since binary aspects are in .class files).

Some Reference information:

Project Structure

├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── hellomaven
    │               └── quickstart
    │                   ├── App.java
    │                   └── AppAspect.java
    └── test
        ├── java
        └── resources

POM.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.hellomaven</groupId>
  <artifactId>quickstart</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>helloMaven</name>
  <description>test</description>

  <properties>
    <java.version>1.8</java.version>
    <junit.version>4.5</junit.version>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <build>
    <pluginManagement>
      <plugins>

        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>aspectj-maven-plugin</artifactId>
          <version>1.9</version>
          <configuration>
            <showWeaveInfo>true</showWeaveInfo>
            <source>${java.version}</source>
            <target>${java.version}</target>
            <Xlint>ignore</Xlint>
            <complianceLevel>${java.version}</complianceLevel>
            <encoding>UTF-8</encoding>
            <verbose>true</verbose>
            <weaveDependencies>
              <weaveDependency>
                <groupId>com.hellomaven.quickstart.App</groupId>
                <artifactId>hello</artifactId>
              </weaveDependency>
            </weaveDependencies>
          </configuration>
          <executions>
            <execution>
              <phase>process-sources</phase>
              <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
              </goals>
            </execution>
          </executions>
          <dependencies>
            <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjrt</artifactId>
              <version>${aspectj.version}</version>
            </dependency>
            <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjtools</artifactId>
              <version>${aspectj.version}</version>
            </dependency>
          </dependencies>
        </plugin>

        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.3</version>
          <configuration>
            <mainClass>com.hellomaven.quickstart</mainClass>
          </configuration>
        </plugin>

      </plugins>
    </pluginManagement>
  </build>
</project>

App.java

package com.hellomaven.quickstart;

public class App {
    public static void main(String[] args) {
        hello("World");
    }
    
    public static void hello(String world) {
        System.out.println("Hello " + world);
    }
}

AppAspect.java

package com.hellomaven.quickstart;

import org.aspectj.lang.annotation.*;

@Aspect
public class AppAspect {
    
    @Before("execution(public static void hello(..))")
    public void testAspectBefore() {
        System.out.println("Before ok.");
    }
}

Solution

  • Wherever and in whichever way you are running your code, you simply need to add -javaagent:/path/to/aspectjweaver.jar to the Java command line. Furthermore, for LTW you need an aop.xml in the right place. For Maven that means:

    Outside of Maven, again you need to add -javaagent to the command line.

    Sorry for the generic answer, but it is just a reflection of your generic question. If you have a more precise follow-up question, you can comment on this answer.

    Some resources: