Refactor toString() so it doesn't break with lazy loaded associations.

This commit is contained in:
Ben Alex 2005-05-12 06:06:57 +00:00
parent 5dcbb416ec
commit 37983a0689
3 changed files with 51 additions and 62 deletions

View File

@ -15,7 +15,7 @@
package net.sf.acegisecurity.domain.impl;
import net.sf.acegisecurity.domain.util.CollectionIgnoringReflectionToStringBuilder;
import net.sf.acegisecurity.domain.util.ReflectionToStringBuilder;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
@ -77,6 +77,6 @@ public abstract class BusinessObject implements Serializable, Cloneable {
* @see java.lang.Object#toString()
*/
public String toString() {
return new CollectionIgnoringReflectionToStringBuilder(this).toString();
return new ReflectionToStringBuilder(this).toString();
}
}

View File

@ -1,51 +0,0 @@
/* Copyright 2004, 2005 Acegi Technology Pty Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sf.acegisecurity.domain.util;
import java.lang.reflect.Field;
/**
* A <code>toString()</code> builder that ignores collections.
*
* @author Carlos Sanchez
* @version $Id$
*
* @see org.apache.commons.lang.builder.ReflectionToStringBuilder
*/
public class CollectionIgnoringReflectionToStringBuilder
extends ReflectionToStringBuilder {
//~ Constructors ===========================================================
public CollectionIgnoringReflectionToStringBuilder(Object object) {
super(object);
}
//~ Methods ================================================================
/**
* Check if the field is a collection and return false in that case.
*
* @see org.apache.commons.lang.builder.ReflectionToStringBuilder#accept(java.lang.reflect.Field)
*/
protected boolean accept(Field field) {
if (CollectionUtils.isCollection(field.getType())) {
return false;
}
return super.accept(field);
}
}

View File

@ -15,28 +15,34 @@
package net.sf.acegisecurity.domain.util;
import org.apache.commons.lang.builder.ToStringStyle;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Collection;
import net.sf.acegisecurity.domain.PersistableEntity;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Customized Commons Lang <code>ReflectionToStringBuilder</code>.
* Customized Commons Lang <code>ReflectionToStringBuilder</code>
* that ignores collections and inaccessible (ie lazy-loaded) fields.
*
* @author Carlos Sanchez
* @version $Revision$
*
* @see org.apache.commons.lang.builder.ReflectionToStringBuilder
* @author Ben Alex
* @version $Id$
*/
public class ReflectionToStringBuilder
extends org.apache.commons.lang.builder.ReflectionToStringBuilder {
//~ Static fields/initializers =============================================
private static DateFormat formatter = DateFormat.getDateTimeInstance();
protected final transient Log logger = LogFactory.getLog(getClass());
private static DateFormat formatter = DateFormat.getDateTimeInstance();
//~ Constructors ===========================================================
@ -64,4 +70,38 @@ public class ReflectionToStringBuilder
return value;
}
}
protected boolean accept(Field field) {
// Ignore if field inaccessible or collection
try {
Object o = getValue(field);
if (o != null) {
if (o instanceof PersistableEntity) {
Serializable id = ((PersistableEntity)o).getInternalId();
if (logger.isDebugEnabled()) {
logger.debug(field + " id: " + id);
}
}
if (o instanceof Collection) {
int size = ((Collection)o).size();
this.append(field.getName(), "<collection with " + size + " elements>");
if (logger.isDebugEnabled()) {
logger.debug(field + " size: " + size);
}
}
}
} catch (Exception fieldInaccessible) {
this.append(field.getName(), "<inaccessible>");
if (logger.isDebugEnabled()) {
logger.debug("Inaccessible: " + field);
}
return false;
}
if (logger.isDebugEnabled()) {
logger.debug("Accessible: " + field);
}
return true;
}
}