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.

Real.java 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package a.b.c;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.Around;
  4. import org.aspectj.lang.annotation.Aspect;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. class Foo {
  7. public void m() {
  8. throw new RuntimeException("Hello World");
  9. }
  10. }
  11. @Aspect
  12. public class Real {
  13. public static void main(String []argv) {
  14. try {
  15. new Foo().m();
  16. } catch (Throwable t) {
  17. System.out.println(t.getMessage());
  18. }
  19. }
  20. @Pointcut("call(Throwable+.new(String, ..)) && this(caller) && args(exceptionMessage) && if()")
  21. public static boolean exceptionInitializer(Object caller, String exceptionMessage) {
  22. return isNdcEmpty();
  23. }
  24. @Around("exceptionInitializer(caller, exceptionMessage)")
  25. public Object annotateException(ProceedingJoinPoint jp, Object caller, String exceptionMessage) {
  26. System.out.println("advice running");
  27. return jp.proceed(new Object[]{caller, "newmessage"});
  28. }
  29. private static boolean isNdcEmpty() {
  30. return NDC.getDepth() == 0;
  31. }
  32. }
  33. class NDC {
  34. public static int getDepth() { return 0; }
  35. }