public class ToString extends Object
Methods of the form append(String, type) imply the name specified by the string (typically a field name) is shown with value specified by type.
Example usage:
foo and bar are fields of this object.
Another example:public String toString() { return new ToString(this). append("foo","some-value"). append("bar",123). render(); }
Note: Don't forget the render() call at the end.public String toString() { return new ToString(this).reflect(). render(); }
The class only needs to be instantiated once so here's a more efficient approach:
{ //instance initializer ToString tostr = new ToString(this); } public String toString() { return tostr.reflect(). render(); }
Modifier and Type | Class and Description |
---|---|
static class |
ToString.Style
Drives the formatting behavior.
|
Constructor and Description |
---|
ToString(Object obj)
Creates a ToString for the specified object, using the
default
ToString.Style . |
ToString(Object obj,
ToString.Style.VisibleLevel level)
Creates a ToString for the specified object with the
specified visibility level.
|
ToString(Object obj,
ToString.Style style)
Creates a ToString for the specified object using the
specified style
|
Modifier and Type | Method and Description |
---|---|
ToString |
append(Object str)
Appends an arbitrary string to the result.
|
ToString |
append(String fieldName,
boolean val) |
ToString |
append(String fieldName,
boolean[] val) |
ToString |
append(String fieldName,
byte val) |
ToString |
append(String fieldName,
byte[] val) |
ToString |
append(String fieldName,
char val) |
ToString |
append(String fieldName,
char[] val) |
ToString |
append(String fieldName,
double val) |
ToString |
append(String fieldName,
double[] val) |
ToString |
append(String fieldName,
float val) |
ToString |
append(String fieldName,
float[] val) |
ToString |
append(String fieldName,
int val) |
ToString |
append(String fieldName,
int[] val) |
ToString |
append(String fieldName,
long val) |
ToString |
append(String fieldName,
long[] val) |
ToString |
append(String fieldName,
Object val) |
ToString |
append(String fieldName,
Object[] val) |
ToString |
append(String fieldName,
short val) |
ToString |
append(String fieldName,
short[] val) |
ToString |
append(String fieldName,
String val) |
StringBuffer |
getBuffer()
Returns the internal buffer used to create the string
|
static ToString.Style |
getDefaultStyle()
Returns the default style object.
|
ToString.Style |
getStyle()
Returns the style being currently used by this instance.
|
static void |
main(String[] args)
Unit test
|
ToString |
reflect()
Uses reflection to get the contents of the object.
|
String |
render()
Renders the string
|
static void |
setDefaultStyle(ToString.Style style)
Sets the style object to use as the default.
|
String |
toString()
Returns information about the current state of the ToString
object itself.
|
public ToString(Object obj)
ToString.Style
.public ToString(Object obj, ToString.Style style)
obj
- the target objectstyle
- the formatting stylepublic ToString(Object obj, ToString.Style.VisibleLevel level)
obj
- the target objectstyle
- the formatting stylepublic static ToString.Style getDefaultStyle()
public static void setDefaultStyle(ToString.Style style)
style
- the default stylepublic ToString.Style getStyle()
public StringBuffer getBuffer()
public ToString reflect()
expandArray
is set to true. To print all array values, use the
append methods.public String toString()
render
method.public ToString append(Object str)