ReflectionToStringBuilder
that ignores collections and inaccessible (ie lazy-loaded) fields.
@author Carlos Sanchez
@author Ben Alex
@version $Id: ReflectionToStringBuilder.java,v 1.1 2006/04/01 03:37:47 itnaf Exp $
Assists in implementing {@link Object#toString()} methods using reflection.
This class uses reflection to determine the fields to append. Because these fields are usually private, the class uses {@link java.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[],boolean)}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 {@link ToStringStyle} passed into the constructor.
Assists in implementing {@link Object#toString()} methods using reflection.
This class uses reflection to determine the fields to append. Because these fields are usually private, the class uses {@link java.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[],boolean)} tochange 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 {@link ToStringStyle} passed into theconstructor.
|
|
|
|