Skip to main content

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

First of all, thank you very much for your answer.

Since the coding experiments I have made are AI-guided, I cannot say that they will be consistent.

Very simply, to write a simple agent that calculates the total running time of the application I am making as an agent, from the after-before start and end time of the methods running on the JVM.

I can do it with Aspectj using AOP technique.

I want to do it for legacy applications running on JDK8.

regards

Alexander Kriegisch via aspectj-users <aspectj-users@xxxxxxxxxxx>, 17 Oca 2024 Çar, 12:25 tarihinde şunu yazdı:

This is not trivial. So please, provide a full reproducer project on GitHub including the target code to be woven, so I do not have to fill in the gaps here.

What I noticed at first glance is, that class StartAgent to be and do multiple things at once:

  1. It is a java agent.
  2. It tries to inject another java agent aspectjweaver into the bootstrap classloader, which begs the question why you are not doing that from the JVM command line, if you are already using a -javaagent parameter anyway. Then the springboard agent would not even be necessary. The next question would be why you think you need it on the bootstrap classloader at all.
  3. It is an aspect, i.e. it is accessing classes from aspectjweaver from the system classloader already, before it even has a chance to inject the jar into the bootstrap classloader.

Your springboard agent should not directly import or reference any classes from aspectjweaver or aspectjrt before the weaver JAR has been added to the bootstrap loader. If later for any reason the springboard agent needs to kick off some action involving classes referencing weaver classes, you ought to load and start them via reflection, e.g. Class.forName etc.

It looks as if you are making a simple matter overly complicated. The justification for what you are trying to do could only be a very special use case, which you have failed to describe. So I have no way to be sure, whether there is not a simpler approach. Lacking additional evidence, I would assume there is.

BTW, are you on JDK 8 or on 9+? I am asking for a specific reason too early to discuss now.

--
Alexander Kriegisch
https://scrum-master.de
 

kypdk via aspectj-users schrieb am 17.01.2024 03:20 (GMT +07:00):

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>
 
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/aspectj-users

Back to the top