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

java.lang.Object
  拡張org.apache.commons.lang.builder.ToStringBuilder
      拡張org.apache.commons.lang.builder.ReflectionToStringBuilder

public class ReflectionToStringBuilder
extends ToStringBuilder

Assists in implementing Object.toString() methods using reflection.

This class uses reflection to determine the fields to append. Because these fields are usually private, the class 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.

A typical invocation for this method would look like:

 public String toString() {
   return ReflectionToStringBuilder.toString(this);
 }

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

 System.out.println("An object: " + ReflectionToStringBuilder.toString(anObject));

A subclass can control field output by overriding the methods:

For example, this method does not include the password field in the returned String:

 public String toString() {
     return (new ReflectionToStringBuilder(this) {
         protected boolean accept(Field f) {
             return super.accept(f) && !f.getName().equals("password");
         }
     }).toString();
 }

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

導入されたバージョン:
2.0
バージョン:
$Id: ReflectionToStringBuilder.java,v 1.1.1.1 2004/02/13 10:02:06 hioki Exp $
作成者:
Gary Gregory, Stephen Colebourne, Pete Gieser
翻訳状況:
訳者募集中

コンストラクタの概要
ReflectionToStringBuilder(Object object)
          Constructor.
ReflectionToStringBuilder(Object object, ToStringStyle style)
          Constructor.
ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)
          Constructor.
ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer, Class reflectUpToClass, boolean outputTransients)
          Constructor.
 
メソッドの概要
protected  boolean accept(Field field)
          Returns whether or not to append the given Field.
protected  void appendFieldsIn(Class clazz)
          Appends the fields and values defined by the given object of the given Class.
 Class getUpToClass()
          Gets the last super class to stop appending fields for.
protected  Object getValue(Field field)
          Calls java.lang.reflect.Field.get(Object).
 boolean isAppendTransients()
          Gets whether or not to append transient fields.
 ToStringBuilder reflectionAppendArray(Object array)
          Append to the toString an Object array.
 void setAppendTransients(boolean appendTransients)
          Sets whether or not to append transient fields.
 void setUpToClass(Class clazz)
          Sets the last super class to stop appending fields for.
 String toString()
          Gets the String built by this builder.
static String toString(Object object)
          This method uses reflection to build a suitable toString using the default ToStringStyle.
static String toString(Object object, ToStringStyle style)
          This method uses reflection to build a suitable toString.
static String toString(Object object, ToStringStyle style, boolean outputTransients)
          This method uses reflection to build a suitable toString.
static String toString(Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass)
          This method uses reflection to build a suitable toString.
 
クラス org.apache.commons.lang.builder.ToStringBuilder から継承したメソッド
append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, appendAsObjectToString, appendSuper, appendToString, getDefaultStyle, getObject, getStringBuffer, getStyle, reflectionToString, reflectionToString, reflectionToString, reflectionToString, setDefaultStyle
 
クラス java.lang.Object から継承したメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

コンストラクタの詳細

ReflectionToStringBuilder

public ReflectionToStringBuilder(Object object)

Constructor.

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

ReflectionToStringBuilder

public ReflectionToStringBuilder(Object object,
                                 ToStringStyle style)

Constructor.

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

ReflectionToStringBuilder

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

Constructor.

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

ReflectionToStringBuilder

public ReflectionToStringBuilder(Object object,
                                 ToStringStyle style,
                                 StringBuffer buffer,
                                 Class reflectUpToClass,
                                 boolean outputTransients)
Constructor.

パラメータ:
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
reflectUpToClass - the superclass to reflect up to (inclusive), may be null
outputTransients - whether to include transient fields
メソッドの詳細

toString

public static String toString(Object object)

This method uses reflection to build a suitable toString using the default ToStringStyle.

It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.

Transient members will be not be included, as they are likely derived. Static fields will not be included. Superclass fields will be appended.

パラメータ:
object - the Object to be output
戻り値:
the String result
例外:
IllegalArgumentException - if the Object is null

toString

public static String toString(Object object,
                              ToStringStyle style)

This method uses reflection to build a suitable toString.

It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.

Transient members will be not be included, as they are likely derived. Static fields will not be included. Superclass fields will be appended.

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

パラメータ:
object - the Object to be output
style - the style of the toString to create, may be null
戻り値:
the String result
例外:
IllegalArgumentException - if the Object or ToStringStyle is null

toString

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

This method uses reflection to build a suitable toString.

It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.

If the outputTransients is true, transient members will be output, otherwise they are ignored, as they are likely derived fields, and not part of the value of the Object.

Static fields will not be included. Superclass fields will be appended.

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

パラメータ:
object - the Object to be output
style - the style of the toString to create, may be null
outputTransients - whether to include transient fields
戻り値:
the String result
例外:
IllegalArgumentException - if the Object is null

toString

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

This method uses reflection to build a suitable toString.

It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.

If the outputTransients is true, transient members will be output, otherwise they are ignored, as they are likely derived fields, and not part of the value of the Object.

Static fields will not be included. Superclass fields will be appended up to and including the specified superclass. A null superclass is treated as java.lang.Object.

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

パラメータ:
object - the Object to be output
style - the style of the toString to create, may be null
outputTransients - whether to include transient fields
reflectUpToClass - the superclass to reflect up to (inclusive), may be null
戻り値:
the String result
例外:
IllegalArgumentException - if the Object is null

accept

protected boolean accept(Field field)
Returns whether or not to append the given Field.

パラメータ:
field - The Field to test.
戻り値:
Whether or not to append the given Field.

appendFieldsIn

protected void appendFieldsIn(Class clazz)

Appends the fields and values defined by the given object of the given Class.

If a cycle is detected as an object is "toString()'ed", such an object is rendered as if Object.toString() had been called and not implemented by the object.

パラメータ:
clazz - The class of object parameter

getUpToClass

public Class getUpToClass()

Gets the last super class to stop appending fields for.

戻り値:
The last super class to stop appending fields for.

getValue

protected Object getValue(Field field)
                   throws IllegalArgumentException,
                          IllegalAccessException

Calls java.lang.reflect.Field.get(Object).

例外:
IllegalArgumentException
IllegalAccessException
関連項目:
Field.get(Object)

isAppendTransients

public boolean isAppendTransients()

Gets whether or not to append transient fields.

戻り値:
Whether or not to append transient fields.

reflectionAppendArray

public ToStringBuilder reflectionAppendArray(Object array)

Append to the toString an Object array.

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

setAppendTransients

public void setAppendTransients(boolean appendTransients)

Sets whether or not to append transient fields.

パラメータ:
appendTransients - Whether or not to append transient fields.

setUpToClass

public void setUpToClass(Class clazz)

Sets the last super class to stop appending fields for.

パラメータ:
clazz - The last super class to stop appending fields for.

toString

public String toString()

Gets the String built by this builder.

オーバーライド:
クラス ToStringBuilder 内の toString
戻り値:
the built string


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