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.

TheWholeShow.aj 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import java.lang.reflect.*;
  2. public class TheWholeShow {
  3. private int f;
  4. public void foo() {}
  5. private void bar() {}
  6. public static void main(String[] args) {
  7. Field[] twsFields = TheWholeShow.class.getDeclaredFields();
  8. for (Field f : twsFields) {
  9. if (!f.getName().equals("f") && !f.getName().equals("x") && !f.getName().startsWith("y") && !f.getName().equals("z")) {
  10. if (!f.isSynthetic()) {
  11. System.err.println("Found non-synthetic field: " + f.getName());
  12. throw new IllegalStateException("Found non-synthetic field: " + f.getName());
  13. }
  14. if (!Modifier.isStatic(f.getModifiers()) && !Modifier.isTransient(f.getModifiers())) {
  15. System.err.println("Found non-transient field: " + f.getName());
  16. throw new IllegalStateException("Found non-transient field: " + f.getName());
  17. }
  18. }
  19. }
  20. Method[] twsMethods = TheWholeShow.class.getDeclaredMethods();
  21. for (Method m: twsMethods) {
  22. if (! (m.getName().equals("foo") || m.getName().equals("bar") || m.getName().equals("<init>") ||
  23. m.getName().equals("main") || m.getName().equals("checkOnlyHasAdviceMembers") || m.getName().equals("getX")) ) {
  24. if (!m.isSynthetic()) {
  25. System.err.println("Found non-synthetic method: " + m.getName());
  26. throw new IllegalStateException("Found non-synthetic method: " + m.getName());
  27. }
  28. }
  29. }
  30. checkOnlyHasAdviceMembers(MakeITDs.class);
  31. checkOnlyHasAdviceMembers(Declares.class);
  32. checkOnlyHasAdviceMembers(Advises.class);
  33. checkOnlyHasAdviceMembers(PerObject.class);
  34. checkOnlyHasAdviceMembers(PTW.class);
  35. checkOnlyHasAdviceMembers(Priv.class);
  36. }
  37. private static void checkOnlyHasAdviceMembers(Class c) {
  38. Method[] ms = c.getDeclaredMethods();
  39. Field[] fs = c.getDeclaredFields();
  40. for (Field f : fs) {
  41. if (!f.isSynthetic()) {
  42. System.err.println("Found non-synthetic field: " + f.getName() + " in " + c.getName());
  43. throw new IllegalStateException("Found non-synthetic field: " + f.getName());
  44. }
  45. }
  46. for (Method m : ms) {
  47. if (!m.isSynthetic()) {
  48. String name = m.getName();
  49. if (name.equals("aspectOf") || name.equals("hasAspect") || name.equals("getWithinTypeName")) continue;
  50. if ( ! (name.startsWith("ajc$before") || name.startsWith("ajc$after") || name.startsWith("ajc$around") ||
  51. name.startsWith("ajc$interMethod$"))) {
  52. System.err.println("Found non-synthetic method: " + m.getName() + " in " + c.getName());
  53. throw new IllegalStateException("Found non-synthetic method: " + m.getName());
  54. } else if (name.startsWith("ajc$around") && name.endsWith("proceed")) {
  55. System.err.println("Found non-synthetic method: " + m.getName() + " in " + c.getName());
  56. throw new IllegalStateException("Found non-synthetic method: " + m.getName());
  57. }
  58. }
  59. }
  60. }
  61. }
  62. aspect MakeITDs {
  63. public int TheWholeShow.x = 5;
  64. private int TheWholeShow.y = 6;
  65. int TheWholeShow.z = 7;
  66. public int TheWholeShow.getX() { return x; }
  67. private int TheWholeShow.getY() { return y; }
  68. int TheWholeShow.getZ() { return z; }
  69. }
  70. aspect Declares {
  71. interface Foo {}
  72. declare parents : TheWholeShow implements Foo;
  73. declare warning : execution(* TheWholeShow.notThere(..)) : "foo";
  74. declare soft : Exception : execution(* TheWholeShow.foo(..));
  75. }
  76. aspect Advises {
  77. pointcut pc() : execution(* TheWholeShow.*(..));
  78. before() : pc() {}
  79. Object around(Object tws) : pc() && this(tws) {
  80. return proceed(new TheWholeShow());
  81. }
  82. after() : pc() {}
  83. after() returning : pc() {}
  84. after() throwing : pc() {}
  85. }
  86. aspect PerObject perthis(execution(* TheWholeShow.*(..))) {
  87. }
  88. aspect PTW pertypewithin(TheWholeShow) {}
  89. aspect Cflow {
  90. before() : set(* x) && cflow(execution(* TheWholeShow.*(..))) {}
  91. }
  92. privileged aspect Priv {
  93. before(TheWholeShow tws) : execution(* TheWholeShow.foo()) && this(tws) {
  94. tws.bar();
  95. tws.f = 12;
  96. }
  97. }