2
0
Эх сурвалжийг харах

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

Ben Alex 20 жил өмнө
parent
commit
37983a0689

+ 2 - 2
domain/src/main/java/org/acegisecurity/domain/impl/BusinessObject.java

@@ -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();
     }
 }

+ 0 - 51
domain/src/main/java/org/acegisecurity/domain/util/CollectionIgnoringReflectionToStringBuilder.java

@@ -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);
-    }
-}

+ 49 - 9
domain/src/main/java/org/acegisecurity/domain/util/ReflectionToStringBuilder.java

@@ -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;
+    }	
 }