Apply patch for bug 17250 (http://issues.apache.org/bugzilla/show_bug.cgi?id=17250) with some additional minor improvements.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137262 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2003-03-20 05:32:11 +00:00
parent 804ccc397d
commit 4ecadc12ba
1 changed files with 88 additions and 43 deletions

View File

@ -54,7 +54,9 @@
package org.apache.commons.lang.builder;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
/**
* <p><code>ToString</code> generation routine.</p>
*
@ -113,7 +115,7 @@
* @author Stephen Colebourne
* @author Gary Gregory
* @since 1.0
* @version $Id: ToStringBuilder.java,v 1.14 2003/01/19 18:49:05 scolebourne Exp $
* @version $Id: ToStringBuilder.java,v 1.15 2003/03/20 05:32:11 ggregory Exp $
*/
public class ToStringBuilder {
@ -343,8 +345,11 @@ public static String reflectionToString(Object object, ToStringStyle style, bool
* @throws IllegalArgumentException if the Object is <code>null</code>
*/
public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass) {
if (style == null) {
style = getDefaultStyle();
}
if (object == null) {
throw new IllegalArgumentException("The object must not be null");
return style.getNullText();
}
if (style == null) {
style = getDefaultStyle();
@ -364,29 +369,69 @@ public static String reflectionToString(Object object, ToStringStyle style, bool
* given Class.
*
* @param object the object to append details of
* @param clazz the class to append details of
* @param clazz the class of object parameter
* @param builder the builder to append to
* @param useTransients whether to output transient fields
*/
private static void reflectionAppend(Object object, Class clazz, ToStringBuilder builder, boolean useTransients) {
if (clazz.isArray()) {
reflectionAppendArray(object, clazz, builder);
return;
}
Field[] fields = clazz.getDeclaredFields();
Field.setAccessible(fields, true);
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
if ((f.getName().indexOf('$') == -1) &&
(useTransients || !Modifier.isTransient(f.getModifiers())) &&
(!Modifier.isStatic(f.getModifiers()))) {
if ((f.getName().indexOf('$') == -1)
&& (useTransients || !Modifier.isTransient(f.getModifiers()))
&& (!Modifier.isStatic(f.getModifiers()))) {
try {
builder.append(f.getName(), f.get(object));
} catch (IllegalAccessException ex) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
throw new InternalError("Unexpected IllegalAccessException");
throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage());
}
}
}
}
/**
* Appends the array elements in the given <code>Object</code> of the
* given <code>Class</code> to a <code>ToStringBuilder</code>.
*
* @param object the array object to append details of
* @param clazz the array class of the object parameter
* @param builder the builder to append to
*/
private static void reflectionAppendArray(Object object, Class clazz, ToStringBuilder builder) {
try {
// A multi-dimension array invokes the append(Object) method.
// A single-dimension array of primitive type pt invokes the append(pt[]) method.
builder.getClass().getDeclaredMethod("append", new Class[] { clazz.getComponentType().isArray() ? Object.class : clazz }).invoke(
builder,
new Object[] { object });
} catch (SecurityException e) {
// "This cannot happen"
throw new InternalError("Unexpected SecurityException: " + e.getMessage());
} catch (NoSuchMethodException e) {
// "This cannot happen"
throw new InternalError("Unexpected NoSuchMethodException: " + e.getMessage());
} catch (IllegalArgumentException e) {
// Method.invoke exception
// "This cannot happen"
throw new InternalError("Unexpected IllegalArgumentException: " + e.getMessage());
} catch (IllegalAccessException e) {
// Method.invoke exception
// "This cannot happen"
throw new InternalError("Unexpected IllegalAccessException: " + e.getMessage());
} catch (InvocationTargetException e) {
// Method.invoke exception
// "This cannot happen"
throw new InternalError("Unexpected InvocationTargetException: " + e.getMessage());
}
}
//----------------------------------------------------------------------------
/**