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.

Pointcuts.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* *******************************************************************
  2. * Copyright (c) 2003 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Wes Isberg initial implementation
  11. * ******************************************************************/
  12. // START-SAMPLE library-pointcutIdioms Standard pointcut idioms
  13. package langlib;
  14. import java.io.*;
  15. /**
  16. * Library of pointcut idioms to use in combination with
  17. * other pointcuts.
  18. *
  19. * @author Wes Isberg
  20. */
  21. public class Pointcuts {
  22. // ------- not staticly-determinable
  23. public pointcut adviceCflow() : cflow(adviceexecution());
  24. public pointcut notInAdviceCflow() : !adviceCflow();
  25. public pointcut cflowMainExecution() :
  26. cflow(mainExecution());
  27. // ------- staticly-determinable
  28. public pointcut mainExecution() :
  29. execution(public static void main(String[]));
  30. /** staticly-determinable to never match any join point */
  31. public pointcut never() : if(false)
  32. && execution(ThreadDeath *(ThreadDeath, ThreadDeath));
  33. public pointcut anyMethodExecution() :
  34. execution(* *(..));
  35. public pointcut anyPublicMethodExecution() :
  36. execution(public * *(..));
  37. public pointcut anyNonPrivateMethodExecution() :
  38. execution(!private * *(..));
  39. public pointcut anyConstructorExecution() :
  40. execution(new(..));
  41. public pointcut anyPublicConstructorExecution() :
  42. execution(public new(..));
  43. public pointcut anyNonPrivateConstructorExecution() :
  44. execution(!private new(..));
  45. public pointcut anyPublicFieldGet() :
  46. get(public * *);
  47. public pointcut anyNonPrivateFieldGet() :
  48. get(!private * *);
  49. public pointcut anyPublicFieldSet() :
  50. set(public * *);
  51. public pointcut anyNonPrivateFieldSet() :
  52. set(!private * *); // also !transient?
  53. public pointcut withinSetter() :
  54. withincode(* set*(..));
  55. public pointcut withinGetter() :
  56. withincode(Object+ get*(..));
  57. public pointcut anyNonPublicFieldSetOutsideConstructorOrSetter() :
  58. set(!public * *) && !withincode(new(..))
  59. && !withinSetter();
  60. public pointcut anyRunnableImplementation() :
  61. staticinitialization(Runnable+);
  62. public pointcut anyGetSystemErrOut() :
  63. get(PrintStream System.err) || get(PrintStream System.out);
  64. public pointcut anySetSystemErrOut() :
  65. call(void System.setOut(..)) || call(void System.setErr(..));
  66. public pointcut withinAnyJavaCode() :
  67. within(java..*) || within(javax..*);
  68. public pointcut notWithinJavaCode() :
  69. !withinAnyJavaCode();
  70. public pointcut toStringExecution() :
  71. execution(String toString()) && !within(String);
  72. /** call or execution of any Thread constructor, including subclasses */
  73. public pointcut anyThreadConstruction() :
  74. call(Thread+.new(..)) || execution(Thread+.new(..));
  75. /**
  76. * Any calls to java.io classes
  77. * (but not methods declared only on their subclasses).
  78. */
  79. public pointcut anyJavaIOCalls() :
  80. call(* java.io..*.*(..)) || call(java.io..*.new(..));
  81. /**
  82. * Any calls to java.awt or javax.swing classes
  83. * (but not methods declared only on their subclasses).
  84. */
  85. public pointcut anyJavaAWTOrSwingCalls() :
  86. call(* java.awt..*.*(..)) || call(java.awt..*.new(..))
  87. || call(* javax.swing..*.*(..)) || call(javax.swing..*.new(..));
  88. public pointcut cloneImplementationsInNonCloneable() :
  89. execution(Object !Cloneable+.clone());
  90. public pointcut runImplementationsInNonRunnable() :
  91. execution(void !Runnable+.run());
  92. /** any calls to java.lang.reflect or Class.get* (except getName()) */
  93. public pointcut anySystemReflectiveCalls() :
  94. call(* java.lang.reflect..*.*(..))
  95. || (!call(* Class.getName())
  96. && call(* Class.get*(..)));
  97. /** standard class-loading calls by Class and ClassLoader */
  98. public pointcut anySystemClassLoadingCalls() :
  99. call(Class Class.forName(..))
  100. || call(Class ClassLoader.loadClass(..));
  101. public pointcut anySystemProcessSpawningCalls() :
  102. call(Process Runtime.exec(..))
  103. || call(Class ClassLoader.loadClass(..));
  104. public pointcut mostThrowableReadCalls() :
  105. call(* Throwable+.get*(..))
  106. || call(* Throwable+.print*(..))
  107. || call(String Throwable+.toString(..));
  108. public pointcut exceptionWrappingCalls() :
  109. (args(Throwable+,..) || args(.., Throwable+))
  110. && (set(Throwable+ Throwable+.*)
  111. || (call(* Throwable+.*(..))
  112. || call(Throwable+.new(..))));
  113. private Pointcuts() {}
  114. }
  115. aspect A {
  116. private static aspect PointcutsOnly {
  117. /** require this library to only contain pointcuts */
  118. declare error : within(Pointcuts) &&
  119. (Pointcuts.anyMethodExecution()
  120. || Pointcuts.anyNonPrivateConstructorExecution()
  121. || set(* *)) : "only pointcuts permitted in Pointcuts";
  122. // does not pick out field definitions -- too costly
  123. // set(* Pointcuts.*) || get(* Pointcuts.*)
  124. }
  125. }
  126. // END-SAMPLE library-pointcutIdioms
  127. class PointcutQuestions {
  128. public pointcut anyCodeThrowingException() : // XXX broken?
  129. execution(* *(..) throws Exception+)
  130. || execution(new(..) throws Exception+);
  131. }