From 5c3ec55e15922c58bb2f39145de9fe641840bb50 Mon Sep 17 00:00:00 2001
From: Matthew Jason Benson For example, the detail version of the array based methods will
* output the whole array, whereas the summary method will just output
* the array length. If you want to format the output of certain objects, such as dates, you
* must create a subclass and override a method.
*
* A registry of objects used by
@@ -73,17 +74,17 @@ public abstract class ToStringStyle implements Serializable {
/**
* The default toString style. Using the Using the
Person
* example from {@link ToStringBuilder}, the output would look like this:
- *
+ *
*
* Person@182f0db[name=John Doe,age=33,smoker=false]
*
*/
public static final ToStringStyle DEFAULT_STYLE = new DefaultToStringStyle();
-
+
/**
* The multi line toString style. Using the Using the Person
* example from {@link ToStringBuilder}, the output would look like this:
- *
+ *
*
* Person@182f0db[
* name=John Doe
@@ -93,26 +94,26 @@ public abstract class ToStringStyle implements Serializable {
*
*/
public static final ToStringStyle MULTI_LINE_STYLE = new MultiLineToStringStyle();
-
+
/**
* The no field names toString style. Using the Using the
* Person
example from {@link ToStringBuilder}, the output
* would look like this:
- *
+ *
*
* Person@182f0db[John Doe,33,false]
*
*/
public static final ToStringStyle NO_FIELD_NAMES_STYLE = new NoFieldNameToStringStyle();
-
+
/**
* The short prefix toString style. Using the Person
example
* from {@link ToStringBuilder}, the output would look like this:
- *
+ *
*
* Person[name=John Doe,age=33,smoker=false]
*
- *
+ *
* @since 2.1
*/
public static final ToStringStyle SHORT_PREFIX_STYLE = new ShortPrefixToStringStyle();
@@ -120,38 +121,32 @@ public abstract class ToStringStyle implements Serializable {
/**
* The simple toString style. Using the Using the Person
* example from {@link ToStringBuilder}, the output would look like this:
- *
+ *
*
* John Doe,33,false
*
*/
public static final ToStringStyle SIMPLE_STYLE = new SimpleToStringStyle();
-
+
/**
* reflectionToString
methods
* to detect cyclical object references and avoid infinite loops.
*
true
if the registry contains the given
@@ -174,13 +169,21 @@ static boolean isRegistered(Object value) {
* Registers the given object. Used by the reflection methods to avoid
* infinite loops.
*
- *
+ *
* @param value
* The object to register.
*/
static void register(Object value) {
if (value != null) {
- getRegistry().add(value);
+ WeakHashMap* Unregisters the given object. *
- * + * ** Used by the reflection methods to avoid infinite loops. *
- * + * * @param value * The object to unregister. */ static void unregister(Object value) { - getRegistry().remove(value); + if (value != null) { + WeakHashMaptrue
.
*/
private boolean useFieldNames = true;
-
+
/**
* Whether to use the class name, the default is true
.
*/
private boolean useClassName = true;
-
+
/**
* Whether to use short class names, the default is false
.
*/
private boolean useShortClassName = false;
-
+
/**
* Whether to use the identity hash code, the default is true
.
*/
@@ -224,78 +238,78 @@ static void unregister(Object value) {
* The content start '['
.
*/
private String contentStart = "[";
-
+
/**
* The content end ']'
.
*/
private String contentEnd = "]";
-
+
/**
* The field name value separator '='
.
*/
private String fieldNameValueSeparator = "=";
-
+
/**
* Whether the field separator should be added before any other fields.
*/
private boolean fieldSeparatorAtStart = false;
-
+
/**
* Whether the field separator should be added after any other fields.
*/
private boolean fieldSeparatorAtEnd = false;
-
+
/**
* The field separator ','
.
*/
private String fieldSeparator = ",";
-
+
/**
* The array start '{'
.
*/
private String arrayStart = "{";
-
+
/**
* The array separator ','
.
*/
private String arraySeparator = ",";
-
+
/**
* The detail for array content.
*/
private boolean arrayContentDetail = true;
-
+
/**
* The array end '}'
.
*/
private String arrayEnd = "}";
-
+
/**
* The value to use when fullDetail is null
,
* the default value is true
.
*/
private boolean defaultFullDetail = true;
-
+
/**
* The null
text '<null>'
.
*/
private String nullText = "'.
*/
private String sizeStartText = "'>'
.
*/
private String sizeEndText = ">";
-
+
/**
* The summary object text start '<'
.
*/
private String summaryObjectStartText = "<";
-
+
/**
* The summary object text start '>'
.
*/
@@ -315,9 +329,9 @@ protected ToStringStyle() {
/**
* Append to the toString
the superclass toString.
NOTE: It assumes that the toString has been created from the same ToStringStyle.
- * + * *A null
superToString
is ignored.
StringBuffer
to populate
* @param superToString the super.toString()
* @since 2.0
@@ -329,9 +343,9 @@ public void appendSuper(StringBuffer buffer, String superToString) {
/**
* Append to the toString
another toString.
NOTE: It assumes that the toString has been created from the same ToStringStyle.
- * + * *A null
toString
is ignored.
StringBuffer
to populate
* @param toString the additional toString
* @since 2.0
@@ -353,7 +367,7 @@ public void appendToString(StringBuffer buffer, String toString) {
/**
* Append to the toString
the start of data indicator.
StringBuffer
to populate
* @param object the Object
to build a toString
for
*/
@@ -370,7 +384,7 @@ public void appendStart(StringBuffer buffer, Object object) {
/**
* Append to the toString
the end of data indicator.
StringBuffer
to populate
* @param object the Object
to build a
* toString
for.
@@ -385,7 +399,7 @@ public void appendEnd(StringBuffer buffer, Object object) {
/**
* Remove the last field separator from the buffer.
- * + * * @param buffer theStringBuffer
to populate
* @since 2.0
*/
@@ -456,7 +470,7 @@ protected void appendInternal(StringBuffer buffer, String fieldName, Object valu
&& !(value instanceof Number || value instanceof Boolean || value instanceof Character)) {
appendCyclicObject(buffer, fieldName, value);
return;
- }
+ }
register(value);
@@ -467,77 +481,77 @@ protected void appendInternal(StringBuffer buffer, String fieldName, Object valu
} else {
appendSummarySize(buffer, fieldName, ((Collection>) value).size());
}
-
+
} else if (value instanceof Map, ?>) {
if (detail) {
appendDetail(buffer, fieldName, (Map, ?>) value);
} else {
appendSummarySize(buffer, fieldName, ((Map, ?>) value).size());
}
-
+
} else if (value instanceof long[]) {
if (detail) {
appendDetail(buffer, fieldName, (long[]) value);
} else {
appendSummary(buffer, fieldName, (long[]) value);
}
-
+
} else if (value instanceof int[]) {
if (detail) {
appendDetail(buffer, fieldName, (int[]) value);
} else {
appendSummary(buffer, fieldName, (int[]) value);
}
-
+
} else if (value instanceof short[]) {
if (detail) {
appendDetail(buffer, fieldName, (short[]) value);
} else {
appendSummary(buffer, fieldName, (short[]) value);
}
-
+
} else if (value instanceof byte[]) {
if (detail) {
appendDetail(buffer, fieldName, (byte[]) value);
} else {
appendSummary(buffer, fieldName, (byte[]) value);
}
-
+
} else if (value instanceof char[]) {
if (detail) {
appendDetail(buffer, fieldName, (char[]) value);
} else {
appendSummary(buffer, fieldName, (char[]) value);
}
-
+
} else if (value instanceof double[]) {
if (detail) {
appendDetail(buffer, fieldName, (double[]) value);
} else {
appendSummary(buffer, fieldName, (double[]) value);
}
-
+
} else if (value instanceof float[]) {
if (detail) {
appendDetail(buffer, fieldName, (float[]) value);
} else {
appendSummary(buffer, fieldName, (float[]) value);
}
-
+
} else if (value instanceof boolean[]) {
if (detail) {
appendDetail(buffer, fieldName, (boolean[]) value);
} else {
appendSummary(buffer, fieldName, (boolean[]) value);
}
-
+
} else if (value.getClass().isArray()) {
if (detail) {
appendDetail(buffer, fieldName, (Object[]) value);
} else {
appendSummary(buffer, fieldName, (Object[]) value);
}
-
+
} else {
if (detail) {
appendDetail(buffer, fieldName, value);
@@ -549,17 +563,17 @@ protected void appendInternal(StringBuffer buffer, String fieldName, Object valu
unregister(value);
}
}
-
+
/**
* Append to the toString
an Object
* value that has been detected to participate in a cycle. This
* implementation will print the standard string value of the value.
StringBuffer
to populate
* @param fieldName the field name, typically not used as already appended
* @param value the value to add to the toString
,
* not null
- *
+ *
* @since 2.2
*/
protected void appendCyclicObject(StringBuffer buffer, String fieldName, Object value) {
@@ -1428,7 +1442,7 @@ protected void appendSummary(StringBuffer buffer, String fieldName, boolean[] ar
/**
* Append to the toString
the class name.
StringBuffer
to populate
* @param object the Object
whose name to output
*/
@@ -1445,7 +1459,7 @@ protected void appendClassName(StringBuffer buffer, Object object) {
/**
* Append the {@link System#identityHashCode(java.lang.Object)}.
- * + * * @param buffer theStringBuffer
to populate
* @param object the Object
whose id to output
*/
@@ -1459,7 +1473,7 @@ protected void appendIdentityHashCode(StringBuffer buffer, Object object) {
/**
* Append to the toString
the content start.
StringBuffer
to populate
*/
protected void appendContentStart(StringBuffer buffer) {
@@ -1468,7 +1482,7 @@ protected void appendContentStart(StringBuffer buffer) {
/**
* Append to the toString
the content end.
StringBuffer
to populate
*/
protected void appendContentEnd(StringBuffer buffer) {
@@ -1479,7 +1493,7 @@ protected void appendContentEnd(StringBuffer buffer) {
* Append to the toString
an indicator for null
.
The default indicator is '<null>'
.
StringBuffer
to populate
* @param fieldName the field name, typically not used as already appended
*/
@@ -1489,7 +1503,7 @@ protected void appendNullText(StringBuffer buffer, String fieldName) {
/**
* Append to the toString
the field separator.
StringBuffer
to populate
*/
protected void appendFieldSeparator(StringBuffer buffer) {
@@ -1498,7 +1512,7 @@ protected void appendFieldSeparator(StringBuffer buffer) {
/**
* Append to the toString
the field start.
StringBuffer
to populate
* @param fieldName the field name
*/
@@ -1511,7 +1525,7 @@ protected void appendFieldStart(StringBuffer buffer, String fieldName) {
/**
* Append to the toString
the field end.
StringBuffer
to populate
* @param fieldName the field name, typically not used as already appended
*/
@@ -1550,7 +1564,7 @@ protected void appendSummarySize(StringBuffer buffer, String fieldName, int size
* null
indicating that it doesn't care about
* the detail level. In this case the default detail level is
* used.
- *
+ *
* @param fullDetailRequest the detail level requested
* @return whether full detail is to be shown
*/
@@ -1886,9 +1900,9 @@ protected void setFieldSeparator(String fieldSeparator) {
//---------------------------------------------------------------------
/**
- * Gets whether the field separator should be added at the start + *
Gets whether the field separator should be added at the start * of each buffer.
- * + * * @return the fieldSeparatorAtStart flag * @since 2.0 */ @@ -1897,9 +1911,9 @@ protected boolean isFieldSeparatorAtStart() { } /** - *Sets whether the field separator should be added at the start + *
Sets whether the field separator should be added at the start * of each buffer.
- * + * * @param fieldSeparatorAtStart the fieldSeparatorAtStart flag * @since 2.0 */ @@ -1910,9 +1924,9 @@ protected void setFieldSeparatorAtStart(boolean fieldSeparatorAtStart) { //--------------------------------------------------------------------- /** - *Gets whether the field separator should be added at the end + *
Gets whether the field separator should be added at the end * of each buffer.
- * + * * @return fieldSeparatorAtEnd flag * @since 2.0 */ @@ -1921,9 +1935,9 @@ protected boolean isFieldSeparatorAtEnd() { } /** - *Sets whether the field separator should be added at the end + *
Sets whether the field separator should be added at the end * of each buffer.
- * + * * @param fieldSeparatorAtEnd the fieldSeparatorAtEnd flag * @since 2.0 */ @@ -2097,7 +2111,7 @@ private static final class DefaultToStringStyle extends ToStringStyle { /** * Required for serialization support. - * + * * @see java.io.Serializable */ private static final long serialVersionUID = 1L; @@ -2157,7 +2171,7 @@ private Object readResolve() { } //---------------------------------------------------------------------------- - + /** *ToStringStyle
that prints out the short
* class name and no identity hashcode.