= AspectJ 1.8.7

_© Copyright 2015 Contributors. All rights reserved._

The full list of resolved issues in 1.8.7 is available
https://bugs.eclipse.org/bugs/buglist.cgi?query_format=advanced;bug_status=RESOLVED;bug_status=VERIFIED;bug_status=CLOSED;product=AspectJ;target_milestone=1.8.7;[here]

_Release info: 1.8.7 available 9-Sep-2015_

== Notable changes

=== ajdoc

The ajdoc tool has been fixed! It is now working again if run on a 1.7
JDK.

=== Dynamic weaver attachment

The AspectJ loadtime weaving agent can now be dynamically attached to a
JVM after it has started (you don't need to use -javaagent). This offers
extra flexibility but obviously any classes loaded before attachment
will not be woven.

Here is a simple aspect:

[source, java]
....
public aspect Azpect {
  before(): execution(* *(..)) {
    System.out.println(thisJoinPointStaticPart);
  }
}
....

Compiled via:

[source, text]
....
ajc -1.8 Azpect.java -outxml
....

This produces a compiled class Azpect.class and a file
META-INF/aop-ajc.xml.

I then have this sample application (same directory):

[source, java]
....
import java.lang.management.ManagementFactory;
import org.aspectj.weaver.loadtime.Agent;
import com.sun.tools.attach.VirtualMachine;

public class Application {

  public static void main(String[] args) {
    if (!isAspectJAgentLoaded())
      System.err.println("WARNING: AspectJ weaving agent not loaded");
    new Sample().doSomething();
  }

  public static boolean isAspectJAgentLoaded() {
    try {
      Agent.getInstrumentation();
    } catch (NoClassDefFoundError e) {
      System.out.println(e);
      return false;
    } catch (UnsupportedOperationException e) {
      System.out.println(e);
      return dynamicallyLoadAspectJAgent();
    }
    return true;
  }

  public static boolean dynamicallyLoadAspectJAgent() {
    String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
    int p = nameOfRunningVM.indexOf('@');
    String pid = nameOfRunningVM.substring(0, p);
    try {
      VirtualMachine vm = VirtualMachine.attach(pid);
      String jarFilePath = System.getProperty("AGENT_PATH");
      vm.loadAgent(jarFilePath);
      vm.detach();
    } catch (Exception e) {
      System.out.println(e);
      return false;
    }
    return true;
  }
}
....

And this Sample class:

[source, java]
....
public class Sample {
    public void doSomething() {
        System.out.println("Do something");
        System.out.println("Square of 7 = " + square(7));
    }

    private int square(int i) {
        return i * i;
    }
}
....

Compile these with javac, *but you must have the aspectjweaver and the
JDK tools.jar on your classpath*.

Once compiled we can run it:

[source, text]
....
java -DAGENT_PATH=<path-to>/aspectjweaver.jar Application
....

What does it do? The main method calls the function that detects whether
the agent is attached, if it is not then it programmatically attaches it
using the VirtualMachine class. Then the main method accesses the Sample
class. At this point in program execution the Sample class is loaded and
because the agent has been attached it gets woven. Notice that the
Application class itself is not woven because it was loaded prior to
agent attachment.

Thanks to Alexander Kriegisch for the sample code and the patch to add
this behaviour to AspectJ.
