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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package Torello.Java;

import static Torello.Java.C.*;

import java.util.Arrays;

/**
 * <B><CODE>'Exception Cause Chain'</CODE></B> helps convert exception messages whose
 * <CODE><B>Throwable.cause()</B></CODE> method returns a non-null <CODE>cause</CODE>, thereby
 * unrolling <I>(and printing)</I> this chain of exceptions into a readable <CODE>String</CODE>.
 * 
 * <EMBED CLASS=external-html DATA-FILE-ID=EXCC>
 */
@Torello.JavaDoc.StaticFunctional
public class EXCC
{
    private EXCC() { }

    /**
     * Convenience Method.
     * <BR />Invokes: {@link #toString(Throwable)}
     * <BR />And-Then: {@link StrIndent#indentTabs(String, int)}
     */
    public static String toString(Throwable t, int indentation)
    { return StrIndent.indentTabs(toString(t), indentation); }

    /**
     * Prints the {@code Exception}-message and {@code Exception}-stack trace to an output
     * {@code String}, and invokes, recursively, this method with any cause-{@code Throwable's}
     * that are present in this {@code Throwable}.
     *
     * @param t Any Java {@code Throwable}.  Java {@code Throwable's} with one or {@code 'cause'}
     * {@code Throwable's} will utilize more fully the printing-features of this class,
     * {@code 'EXCC'} - <I>though any {@code Throwable} will be properly printed</I>.
     * 
     * @return This method doesn't actually print anything to the screen or terminal, it just
     * returns a {@code String} that you may print yourself - <I>or write to a
     * {@code class StringBuilder}, or any variation of text-output you wish.</I>
     */
    public static String toString(Throwable t)
    {
        StackTraceElement[] steArr      = t.getStackTrace();
        StringBuilder       sb          = new StringBuilder();
        Throwable           cause       = t.getCause();
        String              m           = t.getMessage();
        String              lm          = t.getLocalizedMessage();
        boolean             hasMessage  = (m != null) && (m.length() > 0);
        boolean             hasLMess    = (lm != null) && (lm.length() > 0);

        if (hasMessage) m   = StrIndent.indent(m, 1, false, true);
        if (hasLMess)   lm  = StrIndent.indent(lm, 1, false, true);

        sb.append(BRED + "THROWN: " + t.getClass().getCanonicalName() + RESET + "\n");

        if (hasMessage)
        {
            sb.append(BCYAN + "Throwable.getMessage():\n" + RESET + m + "\n");

            if (hasLMess && (! m.equals(lm)))
                sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");
        }

        else if (hasLMess)
            sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");

        else
            sb.append(BYELLOW + "No Exception Messages Provided.\n" + RESET);

        sb.append(BCYAN + "StackTrace:\n" + RESET);

        int temp, maxLN = 0;

        for (StackTraceElement ste : steArr)

            if ((temp = ste.getLineNumber()) > maxLN)
                maxLN = temp;

        int base10 = 2 /* 2: colon + space */ + ((int) Math.ceil(Math.log10(maxLN)));

        for (int k=0; k < steArr.length; k++)

            sb.append(
                '\t' + StringParse.rightSpacePad(steArr[k].getLineNumber() + ":", base10) +
                steArr[k].getClassName() + "." +
                steArr[k].getMethodName() + "()\n"
            );

        return (cause == null)
            ? sb.toString()
            : sb.toString() + StrIndent.indentTabs(toString(cause), 1);
    }

    /**
     * Convenience Method.
     * <BR />Invokes: {@link #toStringMaxTraces(Throwable, int)}
     * <BR />And-Then: {@link StrIndent#indentTabs(String, int)}
     */
    public static String toStringMaxTraces(Throwable t, int maxNumInvocations, int indentation)
    { return StrIndent.indent(toStringMaxTraces(t, maxNumInvocations), indentation); }

    /**
     * Prints the {@code Exception}-message and {@code Exception}-stack trace to an output
     * {@code String}, and invokes, recursively, this method with any cause-{@code Throwable's}
     * that are present in this {@code Throwable}.
     *
     * @param t Any Java {@code Throwable}.  Java {@code Throwable's} with one or {@code 'cause'}
     * {@code Throwable's} will utilize more fully the printing-features of this class,
     * {@code 'EXCC'} - <I>though any {@code Throwable} will be properly printed</I>.
     * 
     * @param maxNumInvocations Each of the {@code Throwable's} printed shall have, at most,
     * {@code 'maxNumInvocations'} of their Stack-Traces printed into the output {@code String}.
     * 
     * @return This method doesn't actually print anything to the screen or terminal, it just
     * returns a {@code String} that you may print yourself - <I>or write to a
     * {@code class StringBuilder}, or any variation of text-output you wish.</I>
     */
    public static String toStringMaxTraces(Throwable t, int maxNumInvocations)
    {
        StackTraceElement[] steArr      = t.getStackTrace();
        StringBuilder       sb          = new StringBuilder();
        Throwable           cause       = t.getCause();
        String              m           = t.getMessage();
        String              lm          = t.getLocalizedMessage();
        boolean             hasMessage  = (m != null) && (m.length() > 0);
        boolean             hasLMess    = (lm != null) && (lm.length() > 0);

        if (hasMessage) m   = StrIndent.indent(m, 1, false, true);
        if (hasLMess)   lm  = StrIndent.indent(lm, 1, false, true);

        sb.append(BRED + "THROWN: " + t.getClass().getCanonicalName() + RESET + "\n");

        if (hasMessage)
        {
            sb.append(BCYAN + "Throwable.getMessage():\n" + RESET + m + "\n");

            if (hasLMess && (! m.equals(lm)))
                sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");
        }

        else if (hasLMess)
            sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");

        else
            sb.append(BYELLOW + "No Exception Messages Provided.\n" + RESET);

        sb.append(BCYAN + "StackTrace:\n" + RESET);

        int temp, maxLN = 0;

        int stTruncated = 0;

        if (steArr.length > maxNumInvocations)
        {
            stTruncated = steArr.length - maxNumInvocations;
            steArr = Arrays.copyOf(steArr, maxNumInvocations);
        }

        for (StackTraceElement ste : steArr)

            if ((temp = ste.getLineNumber()) > maxLN)
                maxLN = temp;

        int base10 = 2 /* 2: colon + space */ + ((int) Math.ceil(Math.log10(maxLN)));

        for (int k=0; k < steArr.length; k++)

            sb.append(
                '\t' + StringParse.rightSpacePad(steArr[k].getLineNumber() + ":", base10) +
                steArr[k].getClassName() + "." +
                steArr[k].getMethodName() + "()\n"
            );

        if (stTruncated > 0) sb.append("\t... and " + stTruncated + " more invocations.\n");

        return (cause == null)
            ? sb.toString()
            : sb.toString() + StrIndent.indentTabs(toString(cause), 1);
    }

    /**
     * Convenience Method.
     * <BR />Invokes: {@link #toStringNoST(Throwable)}
     * <BR />And-Then: {@link StrIndent#indentTabs(String, int)}
     */
    public static String toStringNoST(Throwable t, int indentation)
    { return StrIndent.indentTabs(toStringNoST(t), indentation); }

    /**
     * Prints the {@code Exception}-message to an output {@code String}.  Invokes, recursively,
     * this method with any cause-{@code Throwable's} that are present in this {@code Throwable}.
     *
     * <BR /><BR />This method differs from {@link #toString(Throwable)}, in that it <I>does not
     * print the {@code StackTrace's} to the output {@code String}.</I>
     * 
     * @param t Any Java {@code Throwable}.  Java {@code Throwable's} with one or {@code 'cause'}
     * {@code Throwable's} will utilize more fully the printing-features of this class,
     * {@code 'EXCC'} - <I>though any {@code Throwable} will be properly printed</I>.
     * 
     * @return This method doesn't actually print anything to the screen or terminal, it just
     * returns a {@code String} that you may print yourself - <I>or write to a
     * {@code class StorageBuffer,} or any variation of logging you wish.</I>
     */
    public static String toStringNoST(Throwable t)
    {
        StringBuilder       sb          = new StringBuilder();
        Throwable           cause       = t.getCause();
        String              m           = t.getMessage();
        String              lm          = t.getLocalizedMessage();
        boolean             hasMessage  = (m != null) && (m.length() > 0);
        boolean             hasLMess    = (lm != null) && (lm.length() > 0);

        if (hasMessage) m   = StrIndent.indent(m, 1, false, true);
        if (hasLMess)   lm  = StrIndent.indent(lm, 1, false, true);

        sb.append(BRED + "THROWN: " + t.getClass().getCanonicalName() + RESET + "\n");

        if (hasMessage)
        {
            sb.append(BCYAN + "Throwable.getMessage():\n" + RESET + m + "\n");

            if (hasLMess && (! m.equals(lm)))
                sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");
        }

        else if (hasLMess)
            sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");

        else
            sb.append(BYELLOW + "No Exception Messages Provided.\n" + RESET);

        return (cause == null)
            ? sb.toString()
            : sb.toString() + StrIndent.indentTabs(toStringNoST(cause), 1);
    }

}