1 | package genius.core.exceptions;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.Hashtable;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Warning objects handle warning messages. These objects also count how many
|
---|
9 | * times a particular type of message has been issued already. You can ask for a
|
---|
10 | * stack dump as well.
|
---|
11 | */
|
---|
12 |
|
---|
13 | public class Warning {
|
---|
14 |
|
---|
15 | public final static int DEFAULT_SUPPRESSION_NUMBER = 5;
|
---|
16 |
|
---|
17 | protected class MyWarningException extends Exception {
|
---|
18 |
|
---|
19 | private static final long serialVersionUID = 2047098752954743217L;
|
---|
20 | }
|
---|
21 |
|
---|
22 | /*
|
---|
23 | * Hashtable key = warning message, corresponding value = #repetitions.
|
---|
24 | */
|
---|
25 | static Hashtable<String, Integer> pPreviousMessages = new Hashtable<String, Integer>();
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Default warning: Print warning message at most 5 times. Stack trace is
|
---|
29 | * not printed.
|
---|
30 | */
|
---|
31 | public Warning(String warning) {
|
---|
32 | makeWarning(warning, new MyWarningException(), false, DEFAULT_SUPPRESSION_NUMBER);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * The location of the error will be reported as the code location where
|
---|
37 | * this warning is placed. Note that this is not useful if you are
|
---|
38 | * converting an exception into a warning. In that case, you better use
|
---|
39 | * Warning(warning, Exception).
|
---|
40 | *
|
---|
41 | * @param warning
|
---|
42 | * is the message to be shown
|
---|
43 | * @param showstack
|
---|
44 | * is true if you want to show a stack dump as well. Then, stack
|
---|
45 | * dump will be made for location where WARNING occurs
|
---|
46 | * @param suppressat
|
---|
47 | * is the maximum number of this warning you want to appear
|
---|
48 | */
|
---|
49 | public Warning(String warning, boolean showstack, int suppressat) {
|
---|
50 | makeWarning(warning, new MyWarningException(), showstack, suppressat);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public Warning(String pWarning, Exception err) {
|
---|
54 | makeWarning(pWarning, err, false, DEFAULT_SUPPRESSION_NUMBER);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Note that this is not useful if you are converting an exception into a
|
---|
59 | * warning. In that case, you better use Warning(warning, Exception)
|
---|
60 | *
|
---|
61 | * @param pWarning
|
---|
62 | * is the message to be shown
|
---|
63 | * @param err
|
---|
64 | * is the exception that caused the rise of this warning. this
|
---|
65 | * will be used to inform the user about where the problem
|
---|
66 | * occured.
|
---|
67 | * @param pShowStack
|
---|
68 | * is true if you want to show a stack dump as well. If set,
|
---|
69 | * stack dump will be made for location where WARNING occurs.
|
---|
70 | * @param pSuppressAt
|
---|
71 | * is the maximum number of this warning you want to appear
|
---|
72 | */
|
---|
73 | public Warning(String pWarning, Exception err, boolean pShowStack, int pSuppressAt) {
|
---|
74 | makeWarning(pWarning, err, pShowStack, pSuppressAt);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Add warning to static hashtable used to keep track of all warnings issued
|
---|
79 | * so far. Only show warning if message has not appeared more than
|
---|
80 | * 'fSuppressAt' times.
|
---|
81 | *
|
---|
82 | * @param e
|
---|
83 | * is exception that caused the problem. Use null to avoid stack
|
---|
84 | * dump.
|
---|
85 | */
|
---|
86 | public void makeWarning(String pWarning, Exception e, boolean pDumpStack, int pSuppressAt) {
|
---|
87 |
|
---|
88 | Object lWarnings = pPreviousMessages.get(pWarning);
|
---|
89 |
|
---|
90 | if (lWarnings == null) {
|
---|
91 | pPreviousMessages.put(pWarning, 0);
|
---|
92 | lWarnings = 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | int lNrOfWarnings = (Integer) (pPreviousMessages.get(pWarning)) + 1;
|
---|
96 | // Update nr of warning occurrences in hashtable
|
---|
97 | pPreviousMessages.put(pWarning, lNrOfWarnings);
|
---|
98 |
|
---|
99 | if ((Integer) lWarnings > pSuppressAt)
|
---|
100 | return;
|
---|
101 |
|
---|
102 | System.out.print("WARNING: " + pWarning + ", " + e);
|
---|
103 |
|
---|
104 | ArrayList<StackTraceElement> elts = new ArrayList<StackTraceElement>(Arrays.asList(e.getStackTrace()));
|
---|
105 | ArrayList<StackTraceElement> tmp = new ArrayList<StackTraceElement>(elts);
|
---|
106 | if (e instanceof MyWarningException) {
|
---|
107 |
|
---|
108 | tmp.remove(0); // remove the warning itself from the trace.
|
---|
109 | }
|
---|
110 | while ((!tmp.isEmpty()) && (tmp.get(0).toString().indexOf(':') == -1))
|
---|
111 | tmp.remove(0);
|
---|
112 | if (tmp.isEmpty())
|
---|
113 | tmp = elts;
|
---|
114 |
|
---|
115 | if (pDumpStack) {
|
---|
116 | System.out.println();
|
---|
117 | for (StackTraceElement elt : tmp)
|
---|
118 | System.out.println(elt);
|
---|
119 | } else {
|
---|
120 | if (!(tmp.isEmpty()))
|
---|
121 | System.out.print(" at " + tmp.get(0) + "\n");
|
---|
122 | else
|
---|
123 | System.out.print(" at empty stack point?\n");
|
---|
124 | }
|
---|
125 |
|
---|
126 | if ((Integer) lWarnings == pSuppressAt) {
|
---|
127 | System.out.print("New occurrences of this warning will not be shown anymore.\n");
|
---|
128 | return;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | }
|
---|