mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-06 10:08:32 +00:00
Fix to include superclass fields in reflection toString
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137217 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a84055c530
commit
891b71ff2c
@ -98,7 +98,7 @@
|
|||||||
* <code>reflectionToString</code>, uses <code>Field.setAccessible</code> to
|
* <code>reflectionToString</code>, uses <code>Field.setAccessible</code> to
|
||||||
* change the visibility of the fields. This will fail under a security manager,
|
* change the visibility of the fields. This will fail under a security manager,
|
||||||
* unless the appropriate permissions are set up correctly. It is also
|
* unless the appropriate permissions are set up correctly. It is also
|
||||||
* slower than testing explicitly and does not handle superclasses.</p>
|
* slower than testing explicitly.</p>
|
||||||
*
|
*
|
||||||
* <p>A typical invocation for this method would look like:</p>
|
* <p>A typical invocation for this method would look like:</p>
|
||||||
* <pre>
|
* <pre>
|
||||||
@ -112,7 +112,7 @@
|
|||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
* @version $Id: ToStringBuilder.java,v 1.10 2002/12/23 00:20:31 scolebourne Exp $
|
* @version $Id: ToStringBuilder.java,v 1.11 2002/12/31 20:17:53 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class ToStringBuilder {
|
public class ToStringBuilder {
|
||||||
|
|
||||||
@ -243,14 +243,14 @@ public static void setDefaultStyle(ToStringStyle style) {
|
|||||||
*
|
*
|
||||||
* <p>Transient members will be not be included, as they are likely derived.</p>
|
* <p>Transient members will be not be included, as they are likely derived.</p>
|
||||||
*
|
*
|
||||||
* <p>Static fields will be not be included.</p>
|
* <p>Static fields will not be included. Superclass fields will be appended.</p>
|
||||||
*
|
*
|
||||||
* @param object the Object to be output
|
* @param object the Object to be output
|
||||||
* @return the String result
|
* @return the String result
|
||||||
* @throws IllegalArgumentException if the Object is <code>null</code>
|
* @throws IllegalArgumentException if the Object is <code>null</code>
|
||||||
*/
|
*/
|
||||||
public static String reflectionToString(Object object) {
|
public static String reflectionToString(Object object) {
|
||||||
return reflectionToString(object, null, false);
|
return reflectionToString(object, null, false, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -265,7 +265,7 @@ public static String reflectionToString(Object object) {
|
|||||||
* <p>Transient members will be not be included, as they are likely
|
* <p>Transient members will be not be included, as they are likely
|
||||||
* derived.</p>
|
* derived.</p>
|
||||||
*
|
*
|
||||||
* <p>Static fields will be not be included.</p>
|
* <p>Static fields will not be included. Superclass fields will be appended.</p>
|
||||||
*
|
*
|
||||||
* <p>If the style is <code>null</code>, the default
|
* <p>If the style is <code>null</code>, the default
|
||||||
* <code>ToStringStyle</code> is used.</p>
|
* <code>ToStringStyle</code> is used.</p>
|
||||||
@ -278,7 +278,7 @@ public static String reflectionToString(Object object) {
|
|||||||
* <code>ToStringStyle</code> is <code>null</code>
|
* <code>ToStringStyle</code> is <code>null</code>
|
||||||
*/
|
*/
|
||||||
public static String reflectionToString(Object object, ToStringStyle style) {
|
public static String reflectionToString(Object object, ToStringStyle style) {
|
||||||
return reflectionToString(object, style, false);
|
return reflectionToString(object, style, false, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -295,7 +295,7 @@ public static String reflectionToString(Object object, ToStringStyle style) {
|
|||||||
* as they are likely derived fields, and not part of the value of the
|
* as they are likely derived fields, and not part of the value of the
|
||||||
* Object.</p>
|
* Object.</p>
|
||||||
*
|
*
|
||||||
* <p>Static fields will not be tested.</p>
|
* <p>Static fields will not be included. Superclass fields will be appended.</p>
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* If the style is <code>null</code>, the default
|
* If the style is <code>null</code>, the default
|
||||||
@ -308,33 +308,82 @@ public static String reflectionToString(Object object, ToStringStyle style) {
|
|||||||
* @return the String result
|
* @return the String result
|
||||||
* @throws IllegalArgumentException if the Object is <code>null</code>
|
* @throws IllegalArgumentException if the Object is <code>null</code>
|
||||||
*/
|
*/
|
||||||
public static String reflectionToString(Object object, ToStringStyle style,
|
public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients) {
|
||||||
boolean outputTransients) {
|
return reflectionToString(object, style, outputTransients, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>This method uses reflection to build a suitable
|
||||||
|
* <code>toString</code>.</p>
|
||||||
|
*
|
||||||
|
* <p>It uses <code>Field.setAccessible</code> to gain access to private
|
||||||
|
* fields. This means that it will throw a security exception if run
|
||||||
|
* under a security manger, if the permissions are not set up correctly.
|
||||||
|
* It is also not as efficient as testing explicitly. </p>
|
||||||
|
*
|
||||||
|
* <p>If the <code>outputTransients</code> is <code>true</code>,
|
||||||
|
* transient members will be output, otherwise they are ignored,
|
||||||
|
* as they are likely derived fields, and not part of the value of the
|
||||||
|
* Object.</p>
|
||||||
|
*
|
||||||
|
* <p>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.</p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* If the style is <code>null</code>, the default
|
||||||
|
* <code>ToStringStyle</code> is used.</p>
|
||||||
|
*
|
||||||
|
* @param object the Object to be output
|
||||||
|
* @param style the style of the <code>toString</code> to create,
|
||||||
|
* may be <code>null</code>
|
||||||
|
* @param outputTransients whether to include transient fields
|
||||||
|
* @param reflectUpToClass the superclass to reflect up to (inclusive), may be null
|
||||||
|
* @return the String result
|
||||||
|
* @throws IllegalArgumentException if the Object is <code>null</code>
|
||||||
|
*/
|
||||||
|
public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass) {
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
throw new IllegalArgumentException("The object must not be null");
|
throw new IllegalArgumentException("The object must not be null");
|
||||||
}
|
}
|
||||||
if (style == null) {
|
if (style == null) {
|
||||||
style = getDefaultStyle();
|
style = getDefaultStyle();
|
||||||
}
|
}
|
||||||
Field[] fields = object.getClass().getDeclaredFields();
|
|
||||||
Field.setAccessible(fields, true);
|
|
||||||
ToStringBuilder builder = new ToStringBuilder(object, style);
|
ToStringBuilder builder = new ToStringBuilder(object, style);
|
||||||
|
Class clazz = object.getClass();
|
||||||
|
reflectionAppend(object, clazz, builder, outputTransients);
|
||||||
|
while (clazz.getSuperclass() != null && clazz != reflectUpToClass) {
|
||||||
|
clazz = clazz.getSuperclass();
|
||||||
|
reflectionAppend(object, clazz, builder, outputTransients);
|
||||||
|
}
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the fields and values defined by the given object of the
|
||||||
|
* given Class.
|
||||||
|
*
|
||||||
|
* @param object the object to append details of
|
||||||
|
* @param clazz the class to append details of
|
||||||
|
* @param builder the builder to append to
|
||||||
|
* @param outputTransients whether to output transient fields
|
||||||
|
*/
|
||||||
|
private static void reflectionAppend(Object object, Class clazz, ToStringBuilder builder, boolean outputTransients) {
|
||||||
|
Field[] fields = clazz.getDeclaredFields();
|
||||||
|
Field.setAccessible(fields, true);
|
||||||
for (int i = 0; i < fields.length; ++i) {
|
for (int i = 0; i < fields.length; ++i) {
|
||||||
Field f = fields[i];
|
Field f = fields[i];
|
||||||
if (outputTransients || !Modifier.isTransient(f.getModifiers())) {
|
if (outputTransients || !Modifier.isTransient(f.getModifiers())) {
|
||||||
if (!Modifier.isStatic(f.getModifiers())) {
|
if (!Modifier.isStatic(f.getModifiers())) {
|
||||||
try {
|
try {
|
||||||
builder.append(f.getName(), f.get(object));
|
builder.append(f.getName(), f.get(object));
|
||||||
|
|
||||||
} catch (IllegalAccessException ex) {
|
} catch (IllegalAccessException ex) {
|
||||||
//this can't happen. Would get a Security exception instead
|
//this can't happen. Would get a Security exception instead
|
||||||
//throw a runtime exception in case the impossible happens.
|
//throw a runtime exception in case the impossible happens.
|
||||||
throw new InternalError("Unexpected IllegalAccessException");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return builder.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
@ -56,6 +56,7 @@
|
|||||||
package org.apache.commons.lang.builder;
|
package org.apache.commons.lang.builder;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
@ -65,7 +66,7 @@
|
|||||||
* Unit tests {@link org.apache.commons.lang.ToStringBuilder}.
|
* Unit tests {@link org.apache.commons.lang.ToStringBuilder}.
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||||
* @version $Id: ToStringBuilderTest.java,v 1.2 2002/12/08 20:48:46 scolebourne Exp $
|
* @version $Id: ToStringBuilderTest.java,v 1.3 2002/12/31 20:17:53 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class ToStringBuilderTest extends TestCase {
|
public class ToStringBuilderTest extends TestCase {
|
||||||
|
|
||||||
@ -167,6 +168,53 @@ public void testReflection() {
|
|||||||
assertEquals(baseStr + "[value=5]", ToStringBuilder.reflectionToString(base));
|
assertEquals(baseStr + "[value=5]", ToStringBuilder.reflectionToString(base));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String toBaseString(Object o) {
|
||||||
|
return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testReflectionHierarchyArrayList() {
|
||||||
|
List base = new ArrayList();
|
||||||
|
String baseStr = this.toBaseString(base);
|
||||||
|
assertEquals(baseStr + "[elementData={<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>},size=0,modCount=0]", ToStringBuilder.reflectionToString(base, null, true));
|
||||||
|
assertEquals(baseStr + "[size=0]", ToStringBuilder.reflectionToString(base, null, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testReflectionHierarchy() {
|
||||||
|
ReflectionTestFixtureA baseA = new ReflectionTestFixtureA();
|
||||||
|
String baseStr = this.toBaseString(baseA);
|
||||||
|
assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA));
|
||||||
|
assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null));
|
||||||
|
assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false));
|
||||||
|
assertEquals(baseStr + "[a=a,transientA=t]", ToStringBuilder.reflectionToString(baseA, null, true));
|
||||||
|
assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, null));
|
||||||
|
assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, Object.class));
|
||||||
|
assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, List.class));
|
||||||
|
assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, ReflectionTestFixtureA.class));
|
||||||
|
|
||||||
|
ReflectionTestFixtureB baseB = new ReflectionTestFixtureB();
|
||||||
|
baseStr = this.toBaseString(baseB);
|
||||||
|
assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB));
|
||||||
|
assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB));
|
||||||
|
assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null));
|
||||||
|
assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false));
|
||||||
|
assertEquals(baseStr + "[b=b,transientB=t,a=a,transientA=t]", ToStringBuilder.reflectionToString(baseB, null, true));
|
||||||
|
assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, null));
|
||||||
|
assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, Object.class));
|
||||||
|
assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, List.class));
|
||||||
|
assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, ReflectionTestFixtureA.class));
|
||||||
|
assertEquals(baseStr + "[b=b]", ToStringBuilder.reflectionToString(baseB, null, false, ReflectionTestFixtureB.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
static class ReflectionTestFixtureA {
|
||||||
|
private char a='a';
|
||||||
|
private transient char transientA='t';
|
||||||
|
}
|
||||||
|
|
||||||
|
static class ReflectionTestFixtureB extends ReflectionTestFixtureA {
|
||||||
|
private char b='b';
|
||||||
|
private transient char transientB='t';
|
||||||
|
}
|
||||||
|
|
||||||
public void testAppendSuper() {
|
public void testAppendSuper() {
|
||||||
assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString());
|
assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString());
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user