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.2.adoc 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. = AspectJ 1.8.2
  2. _© Copyright 2014 Contributors. All rights reserved._
  3. The full list of resolved issues in 1.8.2 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.2;[here]
  5. _Release info: 1.8.2 available 14-Aug-2014_
  6. == Notable changes
  7. Although only a few bugs have been fixed here, they are quite important
  8. ones:
  9. === Update to more recent Eclipse Compiler
  10. AspectJ is now based on a more up to date Eclipse compiler level (git
  11. hash 2b07958) so includes all the latest fixes
  12. === Correct handling of RuntimeInvisibleTypeAnnotations (type annotations without runtime visibility)
  13. For anyone weaving code containing these kind of type annotations, this
  14. is an important fix. Although AspectJ does not currently support
  15. pointcuts matching on these kinds of annotation it was crashing when
  16. they were encountered. That is now fixed.
  17. === Annotation processing
  18. A very long standing issue, the AspectJ compiler now supports annotation
  19. processors thanks to some work by Sergey Stupin.
  20. Here is a short example, a very basic annotation and application:
  21. ==== Marker.java
  22. [source, java]
  23. ....
  24. import java.lang.annotation.Retention;
  25. import java.lang.annotation.RetentionPolicy;
  26. @Retention(RetentionPolicy.RUNTIME)
  27. public @interface Marker { }
  28. ....
  29. ==== Code.java
  30. [source, java]
  31. ....
  32. public class Code {
  33. public static void main(String []argv) {
  34. new Code().moo();
  35. new Code().boo();
  36. new Code().too();
  37. new Code().woo();
  38. }
  39. public void moo() {}
  40. @Marker
  41. public void boo() {}
  42. @Marker
  43. public void too() {}
  44. public void woo() {}
  45. }
  46. ....
  47. And now a basic annotation processor. This processor will find methods
  48. in the source marked with the annotation Marker and for each one
  49. generate an aspect tailored to advising that method (this *is* a
  50. contrived demo!)
  51. ==== DemoProcessor.java
  52. [source, java]
  53. ....
  54. import java.io.*;
  55. import javax.tools.*;
  56. import java.util.*;
  57. import javax.annotation.processing.*;
  58. import javax.lang.model.*;
  59. import javax.lang.model.element.*;
  60. @SupportedAnnotationTypes(value= {"*"})
  61. @SupportedSourceVersion(SourceVersion.RELEASE_6)
  62. public class DemoProcessor extends AbstractProcessor {
  63. private Filer filer;
  64. @Override
  65. public void init(ProcessingEnvironment env) {
  66. filer = env.getFiler();
  67. }
  68. @Override
  69. public boolean process(Set elements, RoundEnvironment env) {
  70. // Discover anything marked with @Marker
  71. for (Element element: env.getElementsAnnotatedWith(Marker.class)) {
  72. if (element.getKind() == ElementKind.METHOD) {
  73. // For any methods we find, create an aspect:
  74. String methodName = element.getSimpleName().toString();
  75. String aspectText =
  76. "public aspect Advise_"+methodName+" {\n"+
  77. " before(): execution(* "+methodName+"(..)) {\n"+
  78. " System.out.println(\""+methodName+" running\");\n"+
  79. " }\n"+
  80. "}\n";
  81. try {
  82. JavaFileObject file = filer.createSourceFile("Advise_"+methodName, element);
  83. file.openWriter().append(aspectText).close();
  84. System.out.println("Generated aspect to advise "+element.getSimpleName());
  85. } catch (IOException ioe) {
  86. // already creates message can appear if processor runs more than once
  87. if (!ioe.getMessage().contains("already created")) {
  88. ioe.printStackTrace();
  89. }
  90. }
  91. }
  92. }
  93. return true;
  94. }
  95. }
  96. ....
  97. With those sources, we compile the processor:
  98. [source, text]
  99. ....
  100. ajc -1.6 DemoProcessor.java Marker.java
  101. ....
  102. Now compile the code with the processor specified:
  103. [source, text]
  104. ....
  105. ajc -1.6 -processor DemoProcessor -showWeaveInfo Code.java Marker.java
  106. ....
  107. [source, text]
  108. ....
  109. Generated aspect to advise too
  110. Generated aspect to advise boo
  111. Join point 'method-execution(void Code.boo())' in Type 'Code' (Code.java:14) advised by before advice from 'Advise_boo' (Advise_boo.java:2)
  112. Join point 'method-execution(void Code.too())' in Type 'Code' (Code.java:17) advised by before advice from 'Advise_too' (Advise_too.java:2)
  113. ....
  114. Notice the processor generates the aspects and then they are woven into
  115. the code being compiled immediately.
  116. Finally we can run it:
  117. [source, text]
  118. ....
  119. java Code
  120. boo running
  121. too running
  122. ....
  123. *Note:* There is still work to be done to get annotation processors
  124. behaving under AJDT.