org.apache.commons.lang.builder
クラス ToStringBuilder

java.lang.Object
  拡張org.apache.commons.lang.builder.ToStringBuilder
直系の既知のサブクラス:
ReflectionToStringBuilder

public class ToStringBuilder
extends Object

Assists in implementing Object.toString() methods.

This class enables a good and consistent toString() to be built for any class or object. This class aims to simplify the process by:

To use this class write code as follows:

 public class Person {
   String name;
   int age;
   boolean isSmoker;
 
   ...
 
   public String toString() {
     return new ToStringBuilder(this).
       append("name", name).
       append("age", age).
       append("smoker", smoker).
       toString();
   }
 }
 

This will produce a toString of the format: Person@7f54[name=Stephen,age=29,smoker=false]

To add the superclass toString, use appendSuper(java.lang.String). To append the toString from an object that is delegated to (or any other object), use appendToString(java.lang.String).

Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are usually private, the method, reflectionToString, uses AccessibleObject.setAccessible to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are set up correctly. It is also slower than testing explicitly.

A typical invocation for this method would look like:

 public String toString() {
   return ToStringBuilder.reflectionToString(this);
 }
 

You can also use the builder to debug 3rd party objects:

 System.out.println("An object: " + ToStringBuilder.reflectionToString(anObject));
 

The exact format of the toString is determined by the ToStringStyle passed into the constructor.

導入されたバージョン:
1.0
バージョン:
$Id: ToStringBuilder.java,v 1.1.1.1 2004/02/13 10:02:06 hioki Exp $
作成者:
Stephen Colebourne, Gary Gregory, Pete Gieser

コンストラクタの概要
ToStringBuilder(Object object)
          Constructor for ToStringBuilder.
ToStringBuilder(Object object, ToStringStyle style)
          Constructor for ToStringBuilder specifying the output style.
ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)
          Constructor for ToStringBuilder.
 
メソッドの概要
 ToStringBuilder append(boolean value)
          Append to the toString a boolean value.
 ToStringBuilder append(boolean[] array)
          Append to the toString a boolean array.
 ToStringBuilder append(byte value)
          Append to the toString a byte value.
 ToStringBuilder append(byte[] array)
          Append to the toString a byte array.
 ToStringBuilder append(char value)
          Append to the toString a char value.
 ToStringBuilder append(char[] array)
          Append to the toString a char array.
 ToStringBuilder append(double value)
          Append to the toString a double value.
 ToStringBuilder append(double[] array)
          Append to the toString a double array.
 ToStringBuilder append(float value)
          Append to the toString a float value.
 ToStringBuilder append(float[] array)
          Append to the toString a float array.
 ToStringBuilder append(int value)
          Append to the toString an int value.
 ToStringBuilder append(int[] array)
          Append to the toString an int array.
 ToStringBuilder append(long value)
          Append to the toString a long value.
 ToStringBuilder append(long[] array)
          Append to the toString a long array.
 ToStringBuilder append(Object object)
          Append to the toString an Object value.
 ToStringBuilder append(Object[] array)
          Append to the toString an Object array.
 ToStringBuilder append(short value)
          Append to the toString a short value.
 ToStringBuilder append(short[] array)
          Append to the toString a short array.
 ToStringBuilder append(String fieldName, boolean value)
          Append to the toString a boolean value.
 ToStringBuilder append(String fieldName, boolean[] array)
          Append to the toString a boolean array.
 ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail)
          Append to the toString a boolean array.
 ToStringBuilder append(String fieldName, byte value)
          Append to the toString an byte value.
 ToStringBuilder append(String fieldName, byte[] array)
          Append to the toString a byte array.
 ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail)
          Append to the toString a byte array.
 ToStringBuilder append(String fieldName, char value)
          Append to the toString a char value.
 ToStringBuilder append(String fieldName, char[] array)
          Append to the toString a char array.
 ToStringBuilder append(String fieldName, char[] array, boolean fullDetail)
          Append to the toString a char array.
 ToStringBuilder append(String fieldName, double value)
          Append to the toString a double value.
 ToStringBuilder append(String fieldName, double[] array)
          Append to the toString a double array.
 ToStringBuilder append(String fieldName, double[] array, boolean fullDetail)
          Append to the toString a double array.
 ToStringBuilder append(String fieldName, float value)
          Append to the toString an float value.
 ToStringBuilder append(String fieldName, float[] array)
          Append to the toString a float array.
 ToStringBuilder append(String fieldName, float[] array, boolean fullDetail)
          Append to the toString a float array.
 ToStringBuilder append(String fieldName, int value)
          Append to the toString an int value.
 ToStringBuilder append(String fieldName, int[] array)
          Append to the toString an int array.
 ToStringBuilder append(String fieldName, int[] array, boolean fullDetail)
          Append to the toString an int array.
 ToStringBuilder append(String fieldName, long value)
          Append to the toString a long value.
 ToStringBuilder append(String fieldName, long[] array)
          Append to the toString a long array.
 ToStringBuilder append(String fieldName, long[] array, boolean fullDetail)
          Append to the toString a long array.
 ToStringBuilder append(String fieldName, Object object)
          Append to the toString an Object value.
 ToStringBuilder append(String fieldName, Object[] array)
          Append to the toString an Object array.
 ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail)
          Append to the toString an Object array.
 ToStringBuilder append(String fieldName, Object object, boolean fullDetail)
          Append to the toString an Object value.
 ToStringBuilder append(String fieldName, short value)
          Append to the toString an short value.
 ToStringBuilder append(String fieldName, short[] array)
          Append to the toString a short array.
 ToStringBuilder append(String fieldName, short[] array, boolean fullDetail)
          Append to the toString a short array.
 ToStringBuilder appendAsObjectToString(Object object)
          Appends with the same format as the default Object toString() method.
 ToStringBuilder appendSuper(String superToString)
          Append the toString from the superclass.
 ToStringBuilder appendToString(String toString)
          Append the toString from another object.
static ToStringStyle getDefaultStyle()
          Gets the default ToStringStyle to use.
 Object getObject()
          Returns the Object being output.
 StringBuffer getStringBuffer()
          Gets the StringBuffer being populated.
 ToStringStyle getStyle()
          Gets the ToStringStyle being used.
static String reflectionToString(Object object)
          Forwards to ReflectionToStringBuilder.
static String reflectionToString(Object object, ToStringStyle style)
          Forwards to ReflectionToStringBuilder.
static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients)
          Forwards to ReflectionToStringBuilder.
static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass)
          Forwards to ReflectionToStringBuilder.
static void setDefaultStyle(ToStringStyle style)
          Sets the default ToStringStyle to use.
 String toString()
          Returns the built toString.
 
クラス java.lang.Object から継承したメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

コンストラクタの詳細

ToStringBuilder

public ToStringBuilder(Object object)

Constructor for ToStringBuilder.

This constructor outputs using the default style set with setDefaultStyle.

パラメータ:
object - the Object to build a toString for, must not be null
例外:
IllegalArgumentException - if the Object passed in is null

ToStringBuilder

public ToStringBuilder(Object object,
                       ToStringStyle style)

Constructor for ToStringBuilder specifying the output style.

If the style is null, the default style is used.

パラメータ:
object - the Object to build a toString for, must not be null
style - the style of the toString to create, may be null
例外:
IllegalArgumentException - if the Object passed in is null

ToStringBuilder

public ToStringBuilder(Object object,
                       ToStringStyle style,
                       StringBuffer buffer)

Constructor for ToStringBuilder.

If the style is null, the default style is used.

If the buffer is null, a new one is created.

パラメータ:
object - the Object to build a toString for, must not be null
style - the style of the toString to create, may be null
buffer - the StringBuffer to populate, may be null
例外:
IllegalArgumentException - if the Object passed in is null
メソッドの詳細

getDefaultStyle

public static ToStringStyle getDefaultStyle()

Gets the default ToStringStyle to use.

This could allow the ToStringStyle to be controlled for an entire application with one call.

This might be used to have a verbose ToStringStyle during development and a compact ToStringStyle in production.

戻り値:
the default ToStringStyle

reflectionToString

public static String reflectionToString(Object object)

Forwards to ReflectionToStringBuilder.

関連項目:
ReflectionToStringBuilder.toString(Object)

reflectionToString

public static String reflectionToString(Object object,
                                        ToStringStyle style)

Forwards to ReflectionToStringBuilder.

関連項目:
ReflectionToStringBuilder.toString(Object,ToStringStyle)

reflectionToString

public static String reflectionToString(Object object,
                                        ToStringStyle style,
                                        boolean outputTransients)

Forwards to ReflectionToStringBuilder.

関連項目:
ReflectionToStringBuilder.toString(Object,ToStringStyle,boolean)

reflectionToString

public static String reflectionToString(Object object,
                                        ToStringStyle style,
                                        boolean outputTransients,
                                        Class reflectUpToClass)

Forwards to ReflectionToStringBuilder.

導入されたバージョン:
2.0
関連項目:
ReflectionToStringBuilder.toString(Object,ToStringStyle,boolean,Class)

setDefaultStyle

public static void setDefaultStyle(ToStringStyle style)

Sets the default ToStringStyle to use.

パラメータ:
style - the default ToStringStyle
例外:
IllegalArgumentException - if the style is null

append

public ToStringBuilder append(boolean value)

Append to the toString a boolean value.

パラメータ:
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(boolean[] array)

Append to the toString a boolean array.

パラメータ:
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(byte value)

Append to the toString a byte value.

パラメータ:
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(byte[] array)

Append to the toString a byte array.

パラメータ:
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(char value)

Append to the toString a char value.

パラメータ:
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(char[] array)

Append to the toString a char array.

パラメータ:
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(double value)

Append to the toString a double value.

パラメータ:
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(double[] array)

Append to the toString a double array.

パラメータ:
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(float value)

Append to the toString a float value.

パラメータ:
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(float[] array)

Append to the toString a float array.

パラメータ:
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(int value)

Append to the toString an int value.

パラメータ:
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(int[] array)

Append to the toString an int array.

パラメータ:
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(long value)

Append to the toString a long value.

パラメータ:
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(long[] array)

Append to the toString a long array.

パラメータ:
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(Object object)

Append to the toString an Object value.

パラメータ:
object - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(Object[] array)

Append to the toString an Object array.

パラメータ:
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(short value)

Append to the toString a short value.

パラメータ:
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(short[] array)

Append to the toString a short array.

パラメータ:
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              boolean value)

Append to the toString a boolean value.

パラメータ:
fieldName - the field name
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              boolean[] array)

Append to the toString a boolean array.

パラメータ:
fieldName - the field name
array - the array to add to the hashCode
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              boolean[] array,
                              boolean fullDetail)

Append to the toString a boolean array.

A boolean parameter controls the level of detail to show. Setting true will output the array in full. Setting false will output a summary, typically the size of the array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
fullDetail - true for detail, false for summary info
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              byte value)

Append to the toString an byte value.

パラメータ:
fieldName - the field name
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              byte[] array)

Append to the toString a byte array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              byte[] array,
                              boolean fullDetail)

Append to the toString a byte array.

A boolean parameter controls the level of detail to show. Setting true will output the array in full. Setting false will output a summary, typically the size of the array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
fullDetail - true for detail, false for summary info
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              char value)

Append to the toString a char value.

パラメータ:
fieldName - the field name
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              char[] array)

Append to the toString a char array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              char[] array,
                              boolean fullDetail)

Append to the toString a char array.

A boolean parameter controls the level of detail to show. Setting true will output the array in full. Setting false will output a summary, typically the size of the array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
fullDetail - true for detail, false for summary info
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              double value)

Append to the toString a double value.

パラメータ:
fieldName - the field name
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              double[] array)

Append to the toString a double array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              double[] array,
                              boolean fullDetail)

Append to the toString a double array.

A boolean parameter controls the level of detail to show. Setting true will output the array in full. Setting false will output a summary, typically the size of the array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
fullDetail - true for detail, false for summary info
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              float value)

Append to the toString an float value.

パラメータ:
fieldName - the field name
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              float[] array)

Append to the toString a float array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              float[] array,
                              boolean fullDetail)

Append to the toString a float array.

A boolean parameter controls the level of detail to show. Setting true will output the array in full. Setting false will output a summary, typically the size of the array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
fullDetail - true for detail, false for summary info
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              int value)

Append to the toString an int value.

パラメータ:
fieldName - the field name
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              int[] array)

Append to the toString an int array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              int[] array,
                              boolean fullDetail)

Append to the toString an int array.

A boolean parameter controls the level of detail to show. Setting true will output the array in full. Setting false will output a summary, typically the size of the array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
fullDetail - true for detail, false for summary info
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              long value)

Append to the toString a long value.

パラメータ:
fieldName - the field name
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              long[] array)

Append to the toString a long array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              long[] array,
                              boolean fullDetail)

Append to the toString a long array.

A boolean parameter controls the level of detail to show. Setting true will output the array in full. Setting false will output a summary, typically the size of the array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
fullDetail - true for detail, false for summary info
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              Object object)

Append to the toString an Object value.

パラメータ:
fieldName - the field name
object - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              Object object,
                              boolean fullDetail)

Append to the toString an Object value.

パラメータ:
fieldName - the field name
object - the value to add to the toString
fullDetail - true for detail, false for summary info
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              Object[] array)

Append to the toString an Object array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              Object[] array,
                              boolean fullDetail)

Append to the toString an Object array.

A boolean parameter controls the level of detail to show. Setting true will output the array in full. Setting false will output a summary, typically the size of the array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
fullDetail - true for detail, false for summary info
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              short value)

Append to the toString an short value.

パラメータ:
fieldName - the field name
value - the value to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              short[] array)

Append to the toString a short array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
戻り値:
this

append

public ToStringBuilder append(String fieldName,
                              short[] array,
                              boolean fullDetail)

Append to the toString a short array.

A boolean parameter controls the level of detail to show. Setting true will output the array in full. Setting false will output a summary, typically the size of the array.

パラメータ:
fieldName - the field name
array - the array to add to the toString
fullDetail - true for detail, false for summary info
戻り値:
this

appendAsObjectToString

public ToStringBuilder appendAsObjectToString(Object object)

Appends with the same format as the default Object toString() method. Appends the class name followed by System.identityHashCode(java.lang.Object).

パラメータ:
object - the Object whose class name and id to output
導入されたバージョン:
2.0

appendSuper

public ToStringBuilder appendSuper(String superToString)

Append the toString from the superclass.

This method assumes that the superclass uses the same ToStringStyle as this one.

If superToString is null, no change is made.

パラメータ:
superToString - the result of super.toString()
戻り値:
this
導入されたバージョン:
2.0

appendToString

public ToStringBuilder appendToString(String toString)

Append the toString from another object.

This method is useful where a class delegates most of the implementation of its properties to another class. You can then call toString() on the other class and pass the result into this method.

   private AnotherObject delegate;
   private String fieldInThisClass;
 
   public String toString() {
     return new ToStringBuilder(this).
       appendToString(delegate.toString()).
       append(fieldInThisClass).
       toString();
   }

This method assumes that the other object uses the same ToStringStyle as this one.

If the toString is null, no change is made.

パラメータ:
toString - the result of toString() on another object
戻り値:
this
導入されたバージョン:
2.0

getStringBuffer

public StringBuffer getStringBuffer()

Gets the StringBuffer being populated.

戻り値:
the StringBuffer being populated

getStyle

public ToStringStyle getStyle()

Gets the ToStringStyle being used.

戻り値:
the ToStringStyle being used
導入されたバージョン:
2.0

toString

public String toString()

Returns the built toString.

This method appends the end of data indicator, and can only be called once. Use getStringBuffer() to get the current string state.

戻り値:
the String toString

getObject

public Object getObject()

Returns the Object being output.

戻り値:
The object being output.
導入されたバージョン:
2.0


このドキュメントは、Ja-Jakartaにより訳されました。 コメントがある場合は report@jajakarta.orgまでお願いします。
Translated into Japanese by jajakarta.org. The original page is here.
Copyright (c) 2002-2003 - Apache Software Foundation