001: package edu.umd.cs.findbugs;
002:
003: import java.util.ArrayList;
004:
005: /**
006: * Object recording a recoverable error that occurred during analysis.
007: *
008: * @author David Hovemeyer
009: */
010: public class AnalysisError {
011: private String message;
012: private String exceptionMessage;
013: private String[] stackTrace;
014: private String nestedExceptionMessage;
015: private String[] nestedStackTrace;
016: private final Throwable exception;
017:
018: /**
019: * Constructor.
020: *
021: * @param message message describing the error
022: */
023: public AnalysisError(String message) {
024: this (message, null);
025: }
026:
027: /**
028: * Constructor.
029: *
030: * @param message message describing the error
031: * @param exception exception which is the cause of the error
032: */
033: public AnalysisError(String message, Throwable exception) {
034: this .message = message;
035: this .exception = exception;
036: if (exception != null) {
037: exceptionMessage = exception.toString();
038: stackTrace = getStackTraceAsStringArray(exception);
039: Throwable initCause = exception.getCause();
040: if (initCause != null) {
041: nestedExceptionMessage = initCause.toString();
042: nestedStackTrace = getStackTraceAsStringArray(initCause);
043: }
044:
045: }
046: }
047:
048: /**
049: * @param exception
050: * @return
051: */
052: private String[] getStackTraceAsStringArray(Throwable exception) {
053: StackTraceElement[] exceptionStackTrace = exception
054: .getStackTrace();
055: ArrayList<String> arr = new ArrayList<String>();
056: for (StackTraceElement aExceptionStackTrace : exceptionStackTrace) {
057: arr.add(aExceptionStackTrace.toString());
058: }
059: String[] tmp = arr.toArray(new String[arr.size()]);
060: return tmp;
061: }
062:
063: /**
064: * Set the message describing the error.
065: *
066: * @param message message describing the error
067: */
068: public void setMessage(String message) {
069: this .message = message;
070: }
071:
072: /**
073: * Get the message describing the error.
074: */
075: public String getMessage() {
076: return message;
077: }
078:
079: /**
080: * Set the exception message. This is the value returned by
081: * calling toString() on the original exception object.
082: *
083: * @param exceptionMessage the exception message
084: */
085: public void setExceptionMessage(String exceptionMessage) {
086: this .exceptionMessage = exceptionMessage;
087: }
088:
089: /**
090: * Get the exception message. This is the value returned by
091: * calling toString() on the original exception object.
092: */
093: public String getExceptionMessage() {
094: return exceptionMessage;
095: }
096:
097: /**
098: * Get the exception message. This is the value returned by
099: * calling toString() on the original exception object.
100: */
101: public String getNestedExceptionMessage() {
102: return nestedExceptionMessage;
103: }
104:
105: /**
106: * Set the stack trace elements.
107: * These are the strings returned by calling toString()
108: * on each StackTraceElement in the original exception.
109: *
110: * @param stackTraceList the stack trace elements
111: */
112: public void setStackTrace(String[] stackTraceList) {
113: stackTrace = stackTraceList;
114: }
115:
116: /**
117: * Get the stack trace elements.
118: * These are the strings returned by calling toString()
119: * on each StackTraceElement in the original exception.
120: */
121: public String[] getStackTrace() {
122: return stackTrace;
123: }
124:
125: /**
126: * Get the stack trace elements.
127: * These are the strings returned by calling toString()
128: * on each StackTraceElement in the original exception.
129: */
130: public String[] getNestedStackTrace() {
131: return nestedStackTrace;
132: }
133:
134: /**
135: * @return original exception object, or null if no exception was thrown
136: */
137: public Throwable getException() {
138: return exception;
139: }
140: }
|