Package org.eclipse.lsp4j.jsonrpc.util
Class ToStringBuilder
- java.lang.Object
-
- org.eclipse.lsp4j.jsonrpc.util.ToStringBuilder
-
public final class ToStringBuilder extends java.lang.ObjectHelps with the construction of goodObject.toString()representations.You can customize the output using the builder-style methods
singleLine()skipNulls()andhideFieldNames().You can either directly list fields to include via
add(String, Object)andadd(Object)or you can let the builder do it automatically using reflection, either including the fields declared in this class or including all superclasses.The builder will automatically handle cycles in the object tree. It also pretty prints arrays and Iterables.
This class is not thread safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classToStringBuilder.ToStringContext
-
Constructor Summary
Constructors Constructor Description ToStringBuilder(java.lang.Object instance)Creates a new ToStringBuilder for the given object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ToStringBuilderadd(java.lang.Object value)ToStringBuilderadd(java.lang.String fieldName, java.lang.Object value)ToStringBuilderaddAllFields()Adds all fields declared in the object's class and its superclasses to the output.ToStringBuilderaddAllFields(java.util.function.Predicate<java.lang.reflect.Field> condition)Adds all fields declared in the object's class and its superclasses that satisfy the given condition to the output.ToStringBuilderaddDeclaredFields()Adds all fields declared directly in the object's class to the outputToStringBuilderaddDeclaredFields(java.util.function.Predicate<java.lang.reflect.Field> condition)Adds all fields declared directly in the object's class that satisfy the given condition to the outputToStringBuilderaddField(java.lang.String fieldName)ToStringBuilderhideFieldNames()Field names will not be included in the output.ToStringBuildersingleLine()Fields are printed on a single line, separated by commas instead of newlinesToStringBuilderskipNulls()Fields with null values will be excluded from the outputjava.lang.StringtoString()ToStringBuilderverbatimValues()By default, Iterables, Arrays and multiline Strings are pretty-printed.
-
-
-
Method Detail
-
singleLine
public ToStringBuilder singleLine()
Fields are printed on a single line, separated by commas instead of newlines- Returns:
- this
-
skipNulls
public ToStringBuilder skipNulls()
Fields with null values will be excluded from the output- Returns:
- this
-
hideFieldNames
public ToStringBuilder hideFieldNames()
Field names will not be included in the output. Useful for small classes.- Returns:
- this
-
verbatimValues
public ToStringBuilder verbatimValues()
By default, Iterables, Arrays and multiline Strings are pretty-printed. Switching to their normal representation makes the toString method significantly faster.- Returns:
- this
- Since:
- 2.9
-
addDeclaredFields
public ToStringBuilder addDeclaredFields()
Adds all fields declared directly in the object's class to the output- Returns:
- this
-
addDeclaredFields
public ToStringBuilder addDeclaredFields(java.util.function.Predicate<java.lang.reflect.Field> condition)
Adds all fields declared directly in the object's class that satisfy the given condition to the output- Parameters:
condition-- Returns:
- this
-
addAllFields
public ToStringBuilder addAllFields()
Adds all fields declared in the object's class and its superclasses to the output.- Returns:
- this
-
addAllFields
public ToStringBuilder addAllFields(java.util.function.Predicate<java.lang.reflect.Field> condition)
Adds all fields declared in the object's class and its superclasses that satisfy the given condition to the output.- Parameters:
condition-- Returns:
- this
-
addField
public ToStringBuilder addField(java.lang.String fieldName)
- Parameters:
fieldName- the name of the field to add to the output using reflection- Returns:
- this
-
add
public ToStringBuilder add(java.lang.String fieldName, java.lang.Object value)
- Parameters:
value- the value to add to the outputfieldName- the field name to list the value under- Returns:
- this
-
add
public ToStringBuilder add(java.lang.Object value)
- Parameters:
value- the value to add to the output without a field name- Returns:
- this
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- the String representation of the processed object
-
-