Class ToString
java.lang.Object
fc.util.ToString
Makes creating toString methods easier. (For example,
provides ability to introspect and write field values). Idea
inspired by a similar apache/jakarta utility.
Automatically prints this entire object using reflection.
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.
public String toString() {
return new ToString(this).
append("foo","some-value").
append("bar",123).
render();
}
Another example:Automatically prints this entire object using reflection.
public String toString() {
return new ToString(this).reflect().
render();
}
Note: Don't forget the render() call at the end.
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();
}
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionCreates a ToString for the specified object, using the defaultToString.Style.ToString(Object obj, ToString.Style style) Creates a ToString for the specified object using the specified styleToString(Object obj, ToString.Style.VisibleLevel level) Creates a ToString for the specified object with the specified visibility level. -
Method Summary
Modifier and TypeMethodDescriptionAppends an arbitrary string to the result.Returns the internal buffer used to create the stringstatic ToString.StyleReturns the default style object.getStyle()Returns the style being currently used by this instance.static voidUnit testreflect()Uses reflection to get the contents of the object.render()Renders the stringstatic voidsetDefaultStyle(ToString.Style style) Sets the style object to use as the default.toString()Returns information about the current state of the ToString object itself.
-
Constructor Details
-
ToString
Creates a ToString for the specified object, using the defaultToString.Style. -
ToString
Creates a ToString for the specified object using the specified style- Parameters:
obj- the target objectstyle- the formatting style
-
ToString
Creates a ToString for the specified object with the specified visibility level.- Parameters:
obj- the target objectstyle- the formatting style
-
-
Method Details
-
getDefaultStyle
Returns the default style object. Changes to this will affect the default formatting -
setDefaultStyle
Sets the style object to use as the default. This style will be used by default by all new instances of ToString.- Parameters:
style- the default style
-
getStyle
Returns the style being currently used by this instance. Modifications to this style this will affect rendering output appropriately. -
getBuffer
Returns the internal buffer used to create the string -
reflect
-
render
-
toString
-
main
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-
append
-