incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1494206 - in /sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl: SlingInvocationHandler.java lang/GetMethodToStringImpl.java lang/JDPToStringImpl.java
Date Tue, 18 Jun 2013 17:11:05 GMT
Author: dklco
Date: Tue Jun 18 17:11:04 2013
New Revision: 1494206

URL: http://svn.apache.org/r1494206
Log:
Cleaned up the toString implementation to combine the two classes and cleaning up the method
naming

Added:
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPToStringImpl.java
      - copied, changed from r1494195, sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/GetMethodToStringImpl.java
Removed:
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/GetMethodToStringImpl.java
Modified:
    sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java

Modified: sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java?rev=1494206&r1=1494205&r2=1494206&view=diff
==============================================================================
--- sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
(original)
+++ sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
Tue Jun 18 17:11:04 2013
@@ -35,7 +35,7 @@ import org.apache.sling.api.resource.Val
 import org.apache.sling.commons.proxy.SlingProxyService;
 import org.apache.sling.commons.proxy.annotations.SlingChildren;
 import org.apache.sling.commons.proxy.annotations.SlingReference;
-import org.apache.sling.commons.proxy.impl.lang.GetMethodToStringImpl;
+import org.apache.sling.commons.proxy.impl.lang.JDPToStringImpl;
 import org.apache.sling.commons.proxy.impl.lang.JDPEqualsImpl;
 import org.apache.sling.commons.proxy.impl.lang.JDPHashCodeImpl;
 import org.apache.sling.commons.proxy.impl.lang.MethodType;
@@ -307,7 +307,7 @@ public class SlingInvocationHandler impl
 			throw new UnsupportedOperationException(
 					"Setter methods not yet implemented.");
 		} else if (to.isType(MethodType.ToString)) {
-			return new GetMethodToStringImpl().toString(proxy);
+			return new JDPToStringImpl().toString(proxy);
 		} else if (to.isType(MethodType.HashCode)) {
 			return new JDPHashCodeImpl().hashCode(proxy);
 		} else if (to.isType(MethodType.Equals)) {

Copied: sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPToStringImpl.java
(from r1494195, sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/GetMethodToStringImpl.java)
URL: http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPToStringImpl.java?p2=sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPToStringImpl.java&p1=sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/GetMethodToStringImpl.java&r1=1494195&r2=1494206&rev=1494206&view=diff
==============================================================================
--- sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/GetMethodToStringImpl.java
(original)
+++ sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/lang/JDPToStringImpl.java
Tue Jun 18 17:11:04 2013
@@ -28,9 +28,7 @@ import org.apache.sling.commons.proxy.im
  * 'get' methods on the provided Object.
  * 
  */
-public final class GetMethodToStringImpl {
-
-	private final JDPToStringImpl tostring = new JDPToStringImpl();
+public final class JDPToStringImpl {
 
 	/**
 	 * Adding the interface information to the string representation.
@@ -76,7 +74,7 @@ public final class GetMethodToStringImpl
 				sb.append(m.getName()).append(": ");
 				try {
 					final Object rtnObj = m.invoke(obj, (Object[]) null);
-					sb.append(this.tostring.toString(rtnObj));
+					sb.append(getValueString(rtnObj));
 					if (i < (list.size() - 1)) {
 						sb.append(", ");
 					}
@@ -92,6 +90,81 @@ public final class GetMethodToStringImpl
 	}
 
 	/**
+	 * Generates a toString based on the objects classes and methods.
+	 * 
+	 * @param obj
+	 *            the object to generate the string for
+	 * @param sb
+	 *            the string builder to write to
+	 */
+	private void handleByType(Object obj, StringBuilder sb) {
+		if (obj.getClass().isArray()) {
+			Object[] oa = (Object[]) obj;
+			sb.append("[ ");
+			for (int ndx = 0, max = oa.length; ndx < max; ndx++) {
+				sb.append(getValueString(oa[ndx]));
+				if (ndx + 1 < max) {
+					sb.append(" , ");
+				}
+			}
+			sb.append(" ]");
+		} else {
+			sb.append(obj);
+		}
+	}
+
+	/**
+	 * Generates a String representation of a value object. Limits the length
+	 * and handles null.
+	 * 
+	 * @param obj
+	 *            the object to generate a string of
+	 * @return the string representation
+	 */
+	public String getValueString(Object obj) {
+		StringBuilder sb = new StringBuilder(256);
+
+		if (obj != null) {
+			if (!supportsToString(obj, sb)) {
+				handleByType(obj, sb);
+			}
+		} else {
+			sb.append("null");
+		}
+
+		return sb.toString();
+	}
+
+	/**
+	 * Checks to see if the specified object contains a dedicated toString
+	 * method.
+	 * 
+	 * @param obj
+	 *            the object to check
+	 * @param sb
+	 *            container for the results
+	 * @return whether or not the object has a dedicated toString method
+	 */
+	private boolean supportsToString(Object obj, StringBuilder sb) {
+		try {
+			Method m = obj.getClass().getDeclaredMethod("toString",
+					(Class[]) null);
+			try {
+				Object result = m.invoke(obj, (Object[]) null);
+				sb.append(result);
+			} catch (Exception ex) {
+				sb.append("caused - ").append(ex.getClass().getName())
+						.append(" - Message = ").append(ex.getMessage());
+			}
+			return true;
+		} catch (SecurityException ex) {
+		} catch (NoSuchMethodException ex) {
+		}
+
+		return false;
+	}
+
+	/**
 	 * Generates a String representation of an object.
 	 * 
 	 * @param obj
@@ -103,11 +176,13 @@ public final class GetMethodToStringImpl
 
 		if (obj != null) {
 			this.addInterfaceInfo(obj, sb);
-			this.addMethodReturnValues(obj, Methods.getBeanGetterMethods(obj), sb);
+			this.addMethodReturnValues(obj, Methods.getBeanGetterMethods(obj),
+					sb);
 		} else {
 			sb.append("null");
 		}
 
 		return sb.toString();
 	}
+
 }



Mime
View raw message