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-187.html 3.8KB

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