You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README-1.8.7.adoc 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. = AspectJ 1.8.7
  2. _© Copyright 2015 Contributors. All rights reserved._
  3. The full list of resolved issues in 1.8.7 is available
  4. 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]
  5. _Release info: 1.8.7 available 9-Sep-2015_
  6. == Notable changes
  7. === ajdoc
  8. The ajdoc tool has been fixed! It is now working again if run on a 1.7
  9. JDK.
  10. === Dynamic weaver attachment
  11. The AspectJ loadtime weaving agent can now be dynamically attached to a
  12. JVM after it has started (you don't need to use -javaagent). This offers
  13. extra flexibility but obviously any classes loaded before attachment
  14. will not be woven.
  15. Here is a simple aspect:
  16. [source, java]
  17. ....
  18. public aspect Azpect {
  19. before(): execution(* *(..)) {
  20. System.out.println(thisJoinPointStaticPart);
  21. }
  22. }
  23. ....
  24. Compiled via:
  25. [source, text]
  26. ....
  27. ajc -1.8 Azpect.java -outxml
  28. ....
  29. This produces a compiled class Azpect.class and a file
  30. META-INF/aop-ajc.xml.
  31. I then have this sample application (same directory):
  32. [source, java]
  33. ....
  34. import java.lang.management.ManagementFactory;
  35. import org.aspectj.weaver.loadtime.Agent;
  36. import com.sun.tools.attach.VirtualMachine;
  37. public class Application {
  38. public static void main(String[] args) {
  39. if (!isAspectJAgentLoaded())
  40. System.err.println("WARNING: AspectJ weaving agent not loaded");
  41. new Sample().doSomething();
  42. }
  43. public static boolean isAspectJAgentLoaded() {
  44. try {
  45. Agent.getInstrumentation();
  46. } catch (NoClassDefFoundError e) {
  47. System.out.println(e);
  48. return false;
  49. } catch (UnsupportedOperationException e) {
  50. System.out.println(e);
  51. return dynamicallyLoadAspectJAgent();
  52. }
  53. return true;
  54. }
  55. public static boolean dynamicallyLoadAspectJAgent() {
  56. String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
  57. int p = nameOfRunningVM.indexOf('@');
  58. String pid = nameOfRunningVM.substring(0, p);
  59. try {
  60. VirtualMachine vm = VirtualMachine.attach(pid);
  61. String jarFilePath = System.getProperty("AGENT_PATH");
  62. vm.loadAgent(jarFilePath);
  63. vm.detach();
  64. } catch (Exception e) {
  65. System.out.println(e);
  66. return false;
  67. }
  68. return true;
  69. }
  70. }
  71. ....
  72. And this Sample class:
  73. [source, java]
  74. ....
  75. public class Sample {
  76. public void doSomething() {
  77. System.out.println("Do something");
  78. System.out.println("Square of 7 = " + square(7));
  79. }
  80. private int square(int i) {
  81. return i * i;
  82. }
  83. }
  84. ....
  85. Compile these with javac, *but you must have the aspectjweaver and the
  86. JDK tools.jar on your classpath*.
  87. Once compiled we can run it:
  88. [source, text]
  89. ....
  90. java -DAGENT_PATH=<path-to>/aspectjweaver.jar Application
  91. ....
  92. What does it do? The main method calls the function that detects whether
  93. the agent is attached, if it is not then it programmatically attaches it
  94. using the VirtualMachine class. Then the main method accesses the Sample
  95. class. At this point in program execution the Sample class is loaded and
  96. because the agent has been attached it gets woven. Notice that the
  97. Application class itself is not woven because it was loaded prior to
  98. agent attachment.
  99. Thanks to Alexander Kriegisch for the sample code and the patch to add
  100. this behaviour to AspectJ.