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.

StaticCalls.java 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import org.aspectj.testing.Tester;
  2. public class StaticCalls {
  3. public static void main(String args[]) { test(); }
  4. Object server = null;
  5. void run() {
  6. Tester.checkEqual(StaticCalls.lookup("TimeService0"),
  7. "TimeService0",
  8. "untouched");
  9. Tester.checkEqual(StaticCalls.lookup("InterceptThis"),
  10. "FromAround",
  11. "touched");
  12. Tester.checkEqual(this.lookup("InterceptThis"),
  13. "FromAround",
  14. "this and touched");
  15. Tester.checkEqual(lookup("InterceptThis"),
  16. "FromAround",
  17. "lexical and touched");
  18. }
  19. public static void test() {
  20. new StaticCalls().run();
  21. Class c = Class.forName("java.lang.Foo");
  22. Tester.check(c == null, "intercepted exception and returned null");
  23. }
  24. static String lookup(String s){
  25. return s;
  26. }
  27. }
  28. aspect Aspect {
  29. Object around(String s):
  30. within(StaticCalls) && call(String StaticCalls.lookup(String)) && args(s)
  31. {
  32. if (s.equals("InterceptThis")) return "FromAround";
  33. else return proceed(s);
  34. }
  35. pointcut classForName(): call(Class Class.forName(String));
  36. declare soft: ClassNotFoundException: classForName();
  37. Class around(): classForName() {
  38. try {
  39. return proceed();
  40. } catch (ClassNotFoundException e) {
  41. return null;
  42. }
  43. }
  44. }