- Direct Known Subclasses:
SwingErrorMessage
SwingErrorMessage.
Both this class and SwingErrorMessage provide
static methods, but are designed to be used interchangably for methods
common to both. They were originally a single class, but due to
the introduction of Java modules, it was desirable to split these into
two classes in order to allow some applications to be run with fewer
JAR files.
If nothing is configured, the messages by default are printed on
System.err. Where the messages are sent can be modified by calling
setAppendable(Appendable).
For methods that cause a message to be displayed the order of arguments are:
- a
Stringgiving a title for a dialog box. A dialog box will be provided only if the methodSwingErrorMessage.setComponent(Component)has been called, including with a null argument. - a
Localegiving the locale to use (null implies the default locale. - a
Stringcontaining format directives or the messsage itself - a variable number of
Objects providing arguments specified by a format string.
JOptionPane. The
rationale for a different order is to make any format string used
appear directly before its arguments and to keep the order of
arguments consistent among methods defined by this class.
The method setStackTrace(boolean) can be used
to programatically turn the display of stack traces on and off for
those "display" methods that take a Throwable as an argument,
Error messages will sometimes be directed to a console. The documentation
for SimpleConsole explains how do do this.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static AppendableTheAppendableto which error messages will be written.protected static booleanA field indicating if a separator is needed. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidAdd a separator.protected static voidImplemtation method for an instance of ErrorMsgOps.static voidAdd separator if needed.protected static voidImplemtation method for an instance of ErrorMsgOps.static voidstatic voidDisplay a message.static voidDisplay a message with a file name and line number.static voidDisplay a message with a title.static voidDisplay an error message, based on an exception, with a line from some input.static voidDisplay a Throwable with a prefix.static voidDisplay a Throwable.protected static voiddisplayAux(String msg) Implemtation method for an instance of ErrorMsgOps.protected static voiddisplayAux(String fn, int lineno, String msg) Implemtation method for an instance of ErrorMsgOps.protected static voiddisplayAux(String title, String msg) Implemtation method for an instance of ErrorMsgOps.protected static voiddisplayAux(String fn, String input, int index, Exception e, boolean verbose, boolean showLocation) Implementation to display an error message, based on an exception, with a line from some input.protected static voiddisplayAux(String prefix, Throwable t) Implemtation method for an instance of ErrorMsgOps.protected static voidImplemtation method for an instance of ErrorMsgOps.static voidDisplay a console if there is one, if it is an instance ofSimpleConsole, and if there is new text to display.static voiddisplayFormat(String title, String format, Object... args) Display formatted text with a title using the default locale.static voiddisplayFormat(String title, Locale locale, String format, Object... args) Display formatted text with a title for a specific locale.protected static voiddisplayFormatAux(String title, String format, Object... args) Implemtation method for an instance of ErrorMsgOps.protected static voiddisplayFormatAux(String title, Locale locale, String format, Object... args) Implemtation method for an instance of ErrorMsgOps.static voidDisplay formatted text using the default locale.static voidDisplay formatted text for a specific locale.protected static voidImplemtation method for an instance of ErrorMsgOps.protected static voidImplemtation method for an instance of ErrorMsgOps.static int[]getLineAndColumn(String s, int index) Get the line, column number, and end of line for a specified offset into a string.static StringGet an unannotated message for aThrowable.static StringgetMultilineString(String prefix, String fn, String input, int index, Exception e, boolean verbose, boolean useLocation) Generate a multi-line string for an error message, based on an exception, with a line from some input.static StringgetMultilineString(String prefix, String fn, String input, int index, ArrayList<String> fnList, ArrayList<String> inputList, ArrayList<Integer> indexList, Exception e, boolean verbose, boolean useLocation) Generate a multi-line string for an error message, based on an exception, with a line from some input.static voidsetAppendable(Appendable out) Set the Appendable recording error messages.protected static voidImplemtation method for an instance of ErrorMsgOps.protected static voidsetOps(ErrorMsgOps newops) Set the message operations object.static voidsetStackTrace(boolean value) Set stack-trace recording.protected static voidsetStackTraceAux(boolean value) Implemtation method for an instance of ErrorMsgOps.static booleanCheck if stack traces are enabled.protected static booleanImplemtation method for an instance of ErrorMsgOps.
-
Field Details
-
needSeparator
protected static boolean needSeparatorA field indicating if a separator is needed. This should be treated as a read-only value. -
err
TheAppendableto which error messages will be written. The default isSystem.err.
-
-
Constructor Details
-
ErrorMessage
public ErrorMessage()
-
-
Method Details
-
setOps
Set the message operations object. This class is used bySwingErrorMessageto override the default behavior of this class.- Parameters:
newops- a replacement for the object that handles error messages.
-
addSeparatorIfNeeded
public static void addSeparatorIfNeeded()Add separator if needed. -
addSeparatorIfNeededAux
protected static void addSeparatorIfNeededAux()Implemtation method for an instance of ErrorMsgOps. -
addSeparator
public static void addSeparator()Add a separator. -
addSeparatorAux
protected static void addSeparatorAux()Implemtation method for an instance of ErrorMsgOps. -
setStackTrace
public static void setStackTrace(boolean value) Set stack-trace recording. Determines if a stack trace is shown when a Throwable is displayed.- Parameters:
value- true if stack traces should be recorded; false otherwise- See Also:
-
setStackTraceAux
protected static void setStackTraceAux(boolean value) Implemtation method for an instance of ErrorMsgOps.- Parameters:
value- an argument
-
stackTraceEnabled
public static boolean stackTraceEnabled()Check if stack traces are enabled.- Returns:
- true if stack traces are enabled; false otherwise
- See Also:
-
stackTraceEnabledAux
protected static boolean stackTraceEnabledAux()Implemtation method for an instance of ErrorMsgOps.- Returns:
- status
-
setAppendable
Set the Appendable recording error messages. The default is System.err. The methoddisplayConsoleIfNeeded()will display a console if it is passed to this method and if it is an instance ofSimpleConsole. To use the Java console for output (the TTY associated with a process on Unix/Linux), try setAppendable(System.console().writer()); however, normally one would just use System.err.- Parameters:
out- the Appendable for error messages; null for the default.
-
setAppendableAux
Implemtation method for an instance of ErrorMsgOps.- Parameters:
out- an argument
-
display
Display a message with a file name and line number.- Parameters:
fn- the file namelineno- the line numbermsg- the message to display
-
displayAux
Implemtation method for an instance of ErrorMsgOps.- Parameters:
fn- an argumentlineno- an argumentmsg- an argumnt
-
displayConsoleIfNeeded
public static void displayConsoleIfNeeded()Display a console if there is one, if it is an instance ofSimpleConsole, and if there is new text to display. For aSimpleConsoleto be available, it must have * been passed tosetAppendable(Appendable). -
display
Display a Throwable with a prefix. at a minimum, the Throwable's message will be displayed. Optionally, a stack trace may be displayed as well.- Parameters:
prefix- a prefix to show before a stack trace, typically consisting of spacest- the Throwable- See Also:
-
displayAux
Implemtation method for an instance of ErrorMsgOps.- Parameters:
prefix- an argumentt- an argument
-
display
Display a Throwable. at a minimum, the Throwable's message will be displayed. Optionally, a stack trace may be displayed as well.- Parameters:
t- the Throwable- See Also:
-
displayAux
Implemtation method for an instance of ErrorMsgOps.- Parameters:
t- an argument
-
display
Display a message.- Parameters:
msg- the message to display
-
displayAux
Implemtation method for an instance of ErrorMsgOps.- Parameters:
msg- an argument
-
format
Display formatted text using the default locale. SeeFormatterfor a description of the format string.- Parameters:
format- a format stringargs- the arguments to format
-
formatAux
Implemtation method for an instance of ErrorMsgOps.- Parameters:
format- an argumentargs- arguments
-
format
Display formatted text for a specific locale. SeeFormatterfor a description of the format string.- Parameters:
locale- the Locale, null for no localizationformat- a format stringargs- the arguments to format
-
formatAux
Implemtation method for an instance of ErrorMsgOps.- Parameters:
locale- an argumentformat- an argumentargs- arguments
-
displayFormat
Display formatted text with a title using the default locale. SeeFormatterfor a description of the format string.The title argument is ignored. This method is provided for compatibility with
SwingErrorMessage.- Parameters:
title- the title; null for the default titleformat- a format stringargs- the arguments to format
-
displayFormatAux
Implemtation method for an instance of ErrorMsgOps.- Parameters:
title- an argumentformat- an argumentargs- arguments
-
displayFormat
Display formatted text with a title for a specific locale. SeeFormatterfor a description of the format string.- Parameters:
title- the title; null for the default titlelocale- the Locale, null for no localizationformat- a format stringargs- the arguments to format
-
displayFormatAux
Implemtation method for an instance of ErrorMsgOps.- Parameters:
title- an argumentlocale- an argumentformat- an argumentargs- arguments
-
display
Display a message with a title. The title argument is ignored. This method is provided for compatibility withSwingErrorMessage.- Parameters:
msg- the message to displaytitle- the title; null for the default title- See Also:
-
displayAux
Implemtation method for an instance of ErrorMsgOps.- Parameters:
title- an argumentmsg- an argument
-
getLineAndColumn
Get the line, column number, and end of line for a specified offset into a string.- Parameters:
s- the stringindex- the offset from the start of the string (the offset for the 1st character is 0)- Returns:
- an array whose first element is the line number, whose second element is the column number, and whose third element is the offset for the end of the line, excluding any trailing white space
-
addToMessageMap
public static void addToMessageMap(Class<? extends Throwable> clasz, Function<Throwable, String> mapping) Add a mapping from a subclass ofThrowableto a function that provides the message for thatThrowablewithout any modifications. This method should be called at most once for each class: subsequent calls for a given class will be ignored.Note: a mapping is provided for the class
ObjectParser.Exceptionas it will annotate its message with a description of the location where an error occurred.ObjectParser.Exceptionprovides a methodObjectParser.Exception.getPlainMessage()that recovers the unannotated message. -
getMessageForThrowable
Get an unannotated message for aThrowable. In most casses, this simply calls the throwable'sgetMessage()method. That behavior can be modified by callingaddToMessageMap(Class,Function).This method is used by
getMultilineString(String,String,String,int,Exception,boolean,boolean), which in turn is used byObjectParser.Exception.getMessage()to prevent a strange loop (i.e., infinite recursion).- Parameters:
t- theThrowable.a- Returns:
- the message
-
getMultilineString
public static String getMultilineString(String prefix, String fn, String input, int index, Exception e, boolean verbose, boolean useLocation) Generate a multi-line string for an error message, based on an exception, with a line from some input. The default format in English shows the filename in quotes followed by the line number, the class for the exception, and the corresponding message. The next line contains a portion of the input, with a subsequent line including a caret indicating the point where the error occurred, followed by some information about the column number.- Parameters:
prefix- a string that starts each line when verbose is true and each line other than the first line when verbose is falsefn- a file name or other name for the inputinput- the input being processedindex- a location in the input where an error occurrede- the exception that generated an errorverbose- true if a long format should be useduseLocation- true if the location should appended- Returns:
- a string spaning multiple lines
-
getMultilineString
public static String getMultilineString(String prefix, String fn, String input, int index, ArrayList<String> fnList, ArrayList<String> inputList, ArrayList<Integer> indexList, Exception e, boolean verbose, boolean useLocation) Generate a multi-line string for an error message, based on an exception, with a line from some input. The default format in English shows the filename in quotes followed by the line number, the class for the exception, and the corresponding message. The next line contains a portion of the input, with a subsequent line including a caret indicating the point where the error occurred, followed by some information about the column number.The ArrayList arguments are used to provide additional lines showing source-code locations to trace method and function calls.
- Parameters:
prefix- a string that starts each line when verbose is true and each line other than the first line when verbose is falsefn- a file name or other name for the inputinput- the input being processedindex- a location in the input where an error occurredfnList- a list of names for additional source-code locationsinputList- a list of inputs for additional souce-code inputsindexList- a list of indices for additional input locations.e- the exception that generated an errorverbose- true if a long format should be useduseLocation- true if the location should appended- Returns:
- a string spaning multiple lines
-
display
public static void display(String fn, String input, int index, Exception e, boolean verbose, boolean showLocation) Display an error message, based on an exception, with a line from some input. The default format in English shows the filename in quotes followed by the line number, the class for the exception, and the corresponding message. The next line contains a portion of the input, with a subsequent line including a caret indicating the point where the error occurred, followed by some information about the column number.- Parameters:
fn- a file name or other name for the inputinput- the input being processedindex- a location in the input where an error occurrede- the exception that generated an errorverbose- true if a long format should be usedshowLocation- true if line number and column numbers should be shown; false otherwise
-
displayAux
protected static void displayAux(String fn, String input, int index, Exception e, boolean verbose, boolean showLocation) Implementation to display an error message, based on an exception, with a line from some input. The default format in English shows the filename in quotes followed by the line number, the class for the exception, and the corresponding message. The next line contains a portion of the input, with a subsequent line including a caret indicating the point where the error occurred, followed by some information about the column number.- Parameters:
fn- a file name or other name for the inputinput- the input being processedindex- a location in the input where an error occurrede- the exception that generated an errorverbose- true if a long format should be usedshowLocation- true if line number and column numbers should be shown; false otherwise
-