3

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!

Xyene
  • 2,304
  • 20
  • 36
  • why can't you weave the service before your plugin loads? (i'm assuming you control the whole jvm because you indicate that you can control the service jar). – jtahlborn Sep 02 '12 at 18:56
  • @jtahlborn The service itself operates like this. Loader reads a yml file specifying which service to load. What I am doing is I am creating a copy of the service, changing the classes I need, and then modifying the yml file to point to my new service. On startup of my modded service, I delete the vanilla service. My plugin is loaded by the service, AFTER the service loads. – Xyene Sep 02 '12 at 20:32
  • Maybe I just do not understand your comment, but I see no reason why LTW should not work for you. It was invented for what you want to do: weave classes when they are loaded - thus the name LTW. – kriegaex Sep 02 '12 at 21:04
  • http://stackoverflow.com/questions/11749409/replace-content-of-some-methods-at-runtime seems related: a plugin trying to modify the application for which it was loaded. No AspectJ there (yet), but perhaps these two questions can profit from one another. – MvG Sep 02 '12 at 21:27
  • @kriegaex So load-time weaving works if I do not have access to the files before they are loaded? I am reading AspectJ: In Action and all its examples with load time weaving have the program be executed with -javaagent... – Xyene Sep 02 '12 at 21:34
  • Could you, just to avoid any misunderstandings and give me something to work with, please specify a fully working, minimal piece of sample code showing me how your software works and how you load classes? Maybe if it is too much code, can you provide a download link? I can probably look into it tomorrow. We cannot discuss here for much longer, this is not a discussion forum. – kriegaex Sep 02 '12 at 22:49
  • @kriegaex Its a MineCraft server mod api, called Bukkit. bukkit.org is where its at. It allows for plugins, which are started AFTER Bukkit itself loads. I need to use AspectJ to make some changes in the Bukkit code after its been loaded. Some very specific things that wouldn't get accepted in a pull request. The loader is a separate, unrelated piece of software. – Xyene Sep 03 '12 at 00:27
  • @Nox: **minimal** piece of sample code, maybe just three or four classes playing the roles of server, loader, bukkit and plugin. Something stand-alone. Otherwise I can just give general hints, shots in the dark. Try making it easy for other users to help you by asking smart and clear questions. Your sample advice does not explain your class-loading situation, only that you want to advise the execution of `aClass.aMethod` to inspect its parameters. I do not even know which of the four mentioned application parts `aClass` belongs to. I do not like to guess, I really want to help. – kriegaex Sep 03 '12 at 09:22
  • @kriegaex I rewrote the question to make it hopefully more understanding. Thanks for taking the time to help me! – Xyene Sep 03 '12 at 13:00
  • I still do not completely get it. Is `aClass` part of the main app hosting the plugins? And what do you want to happen if `aMethod`is called? Trigger an action in the plugin? Set one of its properties? If so, does the `Plugin` interface have any method you can call so as to notify the plugin of the `aMethod` call? Edit: Whatever it is, I think you can use AspectJ for it, but I can only write a comprehensive answer if I know what you need. – kriegaex Sep 03 '12 at 13:44
  • aClass is part of the main app. When aMethod is called, I wish to be notified, to be able to print something like "aMethod was called! Its argument was %s". It does, but I'd rather refrain from using it, as it is terribly slow. – Xyene Sep 03 '12 at 13:58
  • @kriegaex Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16199/discussion-between-nox-and-kriegaex) – Xyene Sep 03 '12 at 14:06

1 Answers1

2

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:

    public class Util {

    /**
     * Gets the current JVM PID
     * @return
     * Returns the PID
     * @throws Exception
     */

    public static String getPidFromRuntimeMBean() {
    String jvm = ManagementFactory.getRuntimeMXBean().getName();
    String pid = jvm.substring(0, jvm.indexOf('@'));
    return pid;
    }

    /**
     * Attaches given agent classes to JVM
     * 
     * @param agentClasses
     * A Class<?>[] of classes to be included in agent
     * @param JVMPid
     * The PID of the JVM to attach to
     */

    public static void attachAgentToJVM(Class<?>[] agentClasses, String JVMPid) {

    try {


    File jarFile = File.createTempFile("agent", ".jar");
    jarFile.deleteOnExit();

    Manifest manifest = new Manifest();
    Attributes mainAttributes = manifest.getMainAttributes();
    mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    mainAttributes.put(new Attributes.Name("Agent-Class"),
        Agent.class.getName());
    mainAttributes.put(new Attributes.Name("Can-Retransform-Classes"),
        "true");
    mainAttributes.put(new Attributes.Name("Can-Redefine-Classes"), "true");

    JarOutputStream jos = new JarOutputStream(new FileOutputStream(
        jarFile), manifest);


    for(Class<?> clazz: agentClasses) {         
        JarEntry agent = new JarEntry(clazz.getName().replace('.',
            '/')
            + ".class");
        jos.putNextEntry(agent);

    jos.write(getBytesFromIS(clazz.getClassLoader()
        .getResourceAsStream(
            clazz.getName().replace('.', '/') + ".class")));
    jos.closeEntry();
    }

    jos.close();
    VirtualMachine vm = VirtualMachine.attach(JVMPid);
    vm.loadAgent(jarFile.getAbsolutePath());
    vm.detach();
    } catch (Exception e) {
        e.printStackTrace();
    }

    }

    /**
     * Gets bytes from InputStream
     * 
     * @param stream
     * The InputStream
     * @return 
     * Returns a byte[] representation of given stream
     */

    public static byte[] getBytesFromIS(InputStream stream) {

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    try {
        int nRead;
        byte[] data = new byte[16384];

        while ((nRead = stream.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
        }

        buffer.flush();
    } catch (Exception e) {
        System.err.println("Failed to convert IS to byte[]!");
        e.printStackTrace();
    }

    return buffer.toByteArray();

    }

    /**
     * Gets bytes from class
     * 
     * @param clazz    
     * The class
     * @return
     * Returns a byte[] representation of given class
     */

    public static byte[] getBytesFromClass(Class<?> clazz) {            
    return getBytesFromIS(clazz.getClassLoader().getResourceAsStream( clazz.getName().replace('.', '/') + ".class"));   
    }

}

I included JavaDoc comments for clarity.

An example of using it would be:

Util.attachAgentToJVM(new Class<?>[] { Agent.class, Util.class,
        Profile.class, ProfileClassAdapter.class,
        ProfileMethodAdapter.class }, Util.getPidFromRuntimeMBean());   

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.

Xyene
  • 2,304
  • 20
  • 36