Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Hello world Agent with aspectjweaver

Hello,

What I want to do is to trigger the before advice on the agent when the methods run on the generator.jar

Could you help meĀ ,I would greatly appreciate it

regards

java -javaagent:JavaAgent.jar -jar Generator.jar


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.weaver.loadtime.Agent;

import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.util.jar.JarFile;

@Aspect
public class StartAgent {

public static void premain(String agentArgs, Instrumentation instrumentation) {
System.out.println("Java Agent Started");

String aspectjWeaverPath = "aspectjweaver.jar";
try {
instrumentation.appendToBootstrapClassLoaderSearch(new JarFile(aspectjWeaverPath));
} catch (IOException e) {
System.out.println(e);
}

Agent.premain(agentArgs, instrumentation);
}

@Before("execution(* *(..))")
public void beforeMethodExecution(JoinPoint joinPoint) {
System.out.println("Before method execution: " + joinPoint.getSignature().toShortString());
}
}
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Premain-Class>StartAgent</Premain-Class>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Implementation-Version>${project.version}</Implementation-Version>
</manifestEntries>
</transformer>
</transformers>

<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>
</execution>
</executions>
</plugin>


Back to the top