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.

MainFailure.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 1998-2002 PARC Inc. All rights reserved.
  3. *
  4. * Use and copying of this software and preparation of derivative works based
  5. * upon this software are permitted. Any distribution of this software or
  6. * derivative works must comply with all applicable United States export
  7. * control laws.
  8. *
  9. * This software is made available AS IS, and PARC Inc. makes no
  10. * warranty about the software, its performance or its conformity to any
  11. * specification.
  12. */
  13. import java.util.*;
  14. import java.io.*;
  15. import org.aspectj.lang.*;
  16. /** @author Wes Isberg */
  17. public aspect MainFailure {
  18. public static void main (String[] args) { TargetClass.main(args); }
  19. pointcut main(String[] args) :
  20. args(args) && execution(public static void main(String[]));
  21. // article page 42 - recording failures from main
  22. // START-SAMPLE testing-inoculated-failureCapture Log failures
  23. /** log after failure, but do not affect exception */
  24. after(String[] args) throwing (Throwable t) : main(args) {
  25. logFailureCase(args, t, thisJoinPoint);
  26. }
  27. // END-SAMPLE testing-inoculated-failureCapture
  28. // alternate to swallow exception
  29. // /** log after failure and swallow exception */
  30. // Object around() : main(String[]) {
  31. // try {
  32. // return proceed();
  33. // } catch (Error e) { // ignore
  34. // logFailureCase(args, t, thisJoinPoint);
  35. // // can log here instead
  36. // }
  37. // return null;
  38. // }
  39. public static void logFailureCase(String[] args, Throwable t, Object jp) {
  40. System.err.println("failure case: args " + Arrays.asList(args));
  41. }
  42. }
  43. class TargetClass {
  44. static Thread thread;
  45. /** will throw error if exactly one argument */
  46. public static void main (String[] args) {
  47. // make sure to do at least one failure
  48. if (thread == null) {
  49. Runnable r = new Runnable() {
  50. public void run() {
  51. main(new String[] {"throwError" });
  52. }
  53. };
  54. thread = new Thread(r);
  55. thread.start();
  56. }
  57. if (1 == args.length) {
  58. throw new Error("hello");
  59. }
  60. try { thread.join(); }
  61. catch (InterruptedException ie) { }
  62. }
  63. }