I am developing a plugin for a service. For it to function, it needs some data that the service does not provide.
A plugin has a strict loading/unloading specification. A bare plugin looks like this:
public class Plugin extends JavaPlugin
{
@Override
public void onEnable() {} //Plugin enters here. Comparable to main(String[] args)
@Override
public void onDisable() {} //Plugin exits here, when service shuts down.
}
There is a package called org.service.aClass. Inside it there is aMethod. aMethod looks like this:
public boolean aMethod(boolean bool) {
return bool;
}
An oversimplified scenario, but it works. My plugin needs to know that value of bool whenever aMethod is called. This is absolutely critical o my program; I have no other way of obtaining that value.
I would advise the aMethod, but since my plugin is loaded after the service this would not work. Load time weaving, from what I understand, won’t be suitable here either, due to being loaded AFTER.
Despite it doesn’t work, here is the aspect I was using, in case it may be of any use:
public aspect LogAspect {
pointcut publicMethodExecuted(): execution(* org.service.aClass.aMethod(..));
after(): publicMethodExecuted(){
cat(String.format("Entered method: %s.",
thisJoinPoint.getSignature()));
List<Object> arguments = Arrays.asList(thisJoinPoint.getArgs());
List<Object> argTypes = new ArrayList<Object>();
for (Object o: arguments) {
argTypes.add(o.getClass().toString());
}
cat(String.format("With argument types: %s and values: %s.",
argTypes, arguments));
cat(String.format("Exited method: %s.", thisJoinPoint.getSignature()));
}
public void cat(Object dog) {
System.out.println("[TEST] " + dog);
}
}
I have the AspectJ: In Action book open beside me right now, and on all the load-time weaving examples it mentions that the program must be started with a -javaagent flag. Since my proram is a plugin, there is no way this can happen.
I also looked into ASM. I found a wonderful tutorial on building a profiler (basically what I wish to do) here.
Problem with that is that it once again uses the -javaagent flag when it starts, as well as the public static premain, so it is unsuitable, as I only have onEnable and onDisable.
Then I found out about Java’s Attach API. Which from the looks of it would allow me to attach my agent, the profiler, after a class has been loaded. It seems perfect, but after half an hour searching I could not find a good example of this that I could understand.
Could anyone help? This is a two-in-one question: can AspectJ be used for this? And if so, how? Also, in the case that it cannot, could someone point me in the right direction for using the Attach API with an ASM profiler?
Thanks in advance!
I did it!
I created a runtime attacher for the profiler outlined here. Its basically that, with the premain renamed to ‘agentmain’.
I made a Util class that has the attacher along with other useful functions. The attacher works by creating a jar with the agent, with a manifest stating that it can profile. The Util class looks like this:
I included JavaDoc comments for clarity.
An example of using it would be:
Remember that the attacher wants Agent.class to be the main agent. You can change this easily. The rest of the Class[] are classes to be included in the temporary agent.jar
If your IDE complains about “UnsatisfiedLinkError”s, it is because the attach.(dll|so) needed for this only comes with the JDK. Just copy paste it into your %JAVA_PATH%/jre/lib. Also, add a reference to your JDK’s tools.jar, because it contains all the com.sun imports.
EDIT: I have a working github example is anyone might think this is useful. Its here.