summaryrefslogtreecommitdiffstats
path: root/docs/sandbox/inoculated/src/MainFailure.java
blob: 6d5a62a35f4651008930c107149f5bfe4c48f1ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Copyright (c) 1998-2002 PARC Inc.  All rights reserved.
 *
 * Use and copying of this software and preparation of derivative works based
 * upon this software are permitted.  Any distribution of this software or
 * derivative works must comply with all applicable United States export
 * control laws.
 *
 * This software is made available AS IS, and PARC Inc. makes no
 * warranty about the software, its performance or its conformity to any
 * specification.
 */

import java.util.*;
import java.io.*;
import org.aspectj.lang.*;

/** @author Wes Isberg */

public aspect MainFailure { 

    public static void main (String[] args) { TargetClass.main(args); }

    pointcut main(String[] args) :
        args(args) && execution(public static void main(String[]));

    // article page 42 - recording failures from main
    // START-SAMPLE testing-inoculated-failureCapture Log failures
    /** log after failure, but do not affect exception */
    after(String[] args) throwing (Throwable t) : main(args) {
        logFailureCase(args, t, thisJoinPoint);
    }
    // END-SAMPLE testing-inoculated-failureCapture

    // alternate to swallow exception
//      /** log after failure and swallow exception */
//      Object around() : main(String[]) {
//          try {
//              return proceed();
//          } catch (Error e) { // ignore
//              logFailureCase(args, t, thisJoinPoint);
//              // can log here instead
//          }
//          return null;
//      }

    public static void logFailureCase(String[] args, Throwable t, Object jp) {
        System.err.println("failure case: args " + Arrays.asList(args));
    }
}

class TargetClass {
    static Thread thread;
    /** will throw error if exactly one argument */
    public static void main (String[] args) {
        // make sure to do at least one failure
        if (thread == null) {
            Runnable r = new Runnable() {
                    public void run() {
                        main(new String[] {"throwError" });
                    }
                };
            thread = new Thread(r);
            thread.start();
        }
        if (1 == args.length) {
            throw new Error("hello");
        }
        try { thread.join(); }
        catch (InterruptedException ie) { }
    } 
}