- 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
String
giving 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
Locale
giving the locale to use (null implies the default locale. - a
String
containing format directives or the messsage itself - a variable number of
Object
s 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 Appendable
TheAppendable
to which error messages will be written.protected static boolean
A field indicating if a separator is needed. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Add a separator.protected static void
Implemtation method for an instance of ErrorMsgOps.static void
Add separator if needed.protected static void
Implemtation method for an instance of ErrorMsgOps.static void
static void
Display a message.static void
Display a message with a file name and line number.static void
Display a message with a title.static void
Display an error message, based on an exception, with a line from some input.static void
Display a Throwable with a prefix.static void
Display a Throwable.protected static void
displayAux
(String msg) Implemtation method for an instance of ErrorMsgOps.protected static void
displayAux
(String fn, int lineno, String msg) Implemtation method for an instance of ErrorMsgOps.protected static void
displayAux
(String title, String msg) Implemtation method for an instance of ErrorMsgOps.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.protected static void
displayAux
(String prefix, Throwable t) Implemtation method for an instance of ErrorMsgOps.protected static void
Implemtation method for an instance of ErrorMsgOps.static void
Display a console if there is one, if it is an instance ofSimpleConsole
, and if there is new text to display.static void
displayFormat
(String title, String format, Object... args) Display formatted text with a title using the default locale.static void
displayFormat
(String title, Locale locale, String format, Object... args) Display formatted text with a title for a specific locale.protected static void
displayFormatAux
(String title, String format, Object... args) Implemtation method for an instance of ErrorMsgOps.protected static void
displayFormatAux
(String title, Locale locale, String format, Object... args) Implemtation method for an instance of ErrorMsgOps.static void
Display formatted text using the default locale.static void
Display formatted text for a specific locale.protected static void
Implemtation method for an instance of ErrorMsgOps.protected static void
Implemtation 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 String
Get an unannotated message for aThrowable
.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.static void
setAppendable
(Appendable out) Set the Appendable recording error messages.protected static void
Implemtation method for an instance of ErrorMsgOps.protected static void
setOps
(ErrorMsgOps newops) Set the message operations object.static void
setStackTrace
(boolean value) Set stack-trace recording.protected static void
setStackTraceAux
(boolean value) Implemtation method for an instance of ErrorMsgOps.static boolean
Check if stack traces are enabled.protected static boolean
Implemtation 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
TheAppendable
to 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 bySwingErrorMessage
to 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 aSimpleConsole
to 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. SeeFormatter
for 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. SeeFormatter
for 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. SeeFormatter
for 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. SeeFormatter
for 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 ofThrowable
to a function that provides the message for thatThrowable
without 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.Exception
as it will annotate its message with a description of the location where an error occurred.ObjectParser.Exception
provides 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
-
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
-