[slf4j-dev] svn commit: r1260 - in slf4j/trunk/slf4j-ext/src: main/java/org/slf4j/instrumentation test/java/org/slf4j/instrumentation

ravn at slf4j.org ravn at slf4j.org
Sun Dec 21 22:48:18 CET 2008


Author: ravn
Date: Sun Dec 21 22:48:17 2008
New Revision: 1260

Modified:
   slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/ToStringHelper.java
   slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/instrumentation/ToStringHelperTest.java

Log:
more unit tests for nested arrays


Modified: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/ToStringHelper.java
==============================================================================
--- slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/ToStringHelper.java	(original)
+++ slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/ToStringHelper.java	Sun Dec 21 22:48:17 2008
@@ -5,6 +5,23 @@
 
 public class ToStringHelper {
 
+	/**
+	 * String separating each element when rendering an array. To be compatible
+	 * with lists comma-space is used.
+	 */
+
+	private static final String ELEMENT_SEPARATOR = ", ";
+
+	/**
+	 * unrenderableClasses is essentially a Set of Class objects which has for
+	 * some reason failed to render properly when invoked through a toString
+	 * method call. To avoid memory leaks a data structure using weak references
+	 * is needed, but unfortunately the runtime library does not contain a
+	 * WeakHashSet class, so the behavior is emulated with a WeakHashmap with
+	 * the class as the key, and a Long containing the value of
+	 * System.currentTimeMilis when an instance of the class failed to render.
+	 */
+
 	final static Map<Class, Object> unrenderableClasses = new WeakHashMap<Class, Object>();
 
 	public static String render(Object o) {
@@ -15,86 +32,7 @@
 		if (unrenderableClasses.containsKey(objectClass) == false) {
 			try {
 				if (objectClass.isArray()) {
-					Class componentType = objectClass.getComponentType();
-					StringBuffer sb = new StringBuffer("[");
-					if (componentType.isPrimitive() == true) {
-						if (Boolean.TYPE.equals(componentType)) {
-							boolean[] ba = (boolean[]) o;
-							for (int i = 0; i < ba.length; i++) {
-								if (i > 0) {
-									sb.append(", ");
-								}
-								sb.append(ba[i]);
-							}
-						} else if (Integer.TYPE.equals(componentType)) {
-							int[] ia = (int[]) o;
-							for (int i = 0; i < ia.length; i++) {
-								if (i > 0) {
-									sb.append(", ");
-								}
-								sb.append(ia[i]);
-							}
-
-						} else if (Long.TYPE.equals(componentType)) {
-							long[] ia = (long[]) o;
-							for (int i = 0; i < ia.length; i++) {
-								if (i > 0) {
-									sb.append(", ");
-								}
-								sb.append(ia[i]);
-							}
-						} else if (Double.TYPE.equals(componentType)) {
-							double[] ia = (double[]) o;
-							for (int i = 0; i < ia.length; i++) {
-								if (i > 0) {
-									sb.append(", ");
-								}
-								sb.append(ia[i]);
-							}
-						} else if (Float.TYPE.equals(componentType)) {
-							float[] ia = (float[]) o;
-							for (int i = 0; i < ia.length; i++) {
-								if (i > 0) {
-									sb.append(", ");
-								}
-								sb.append(ia[i]);
-							}
-						} else if (Character.TYPE.equals(componentType)) {
-							char[] ia = (char[]) o;
-							for (int i = 0; i < ia.length; i++) {
-								if (i > 0) {
-									sb.append(", ");
-								}
-								sb.append(ia[i]);
-							}
-						} else if (Short.TYPE.equals(componentType)) {
-							short[] ia = (short[]) o;
-							for (int i = 0; i < ia.length; i++) {
-								if (i > 0) {
-									sb.append(", ");
-								}
-								sb.append(ia[i]);
-							}
-						} else if (Byte.TYPE.equals(componentType)) {
-							byte[] ia = (byte[]) o;
-							for (int i = 0; i < ia.length; i++) {
-								if (i > 0) {
-									sb.append(", ");
-								}
-								sb.append(ia[i]);
-							}
-						}
-					} else {
-						Object[] oa = (Object[]) o;
-						for (int i = 0; i < oa.length; i++) {
-							if (i > 0) {
-								sb.append(", ");
-							}
-							sb.append(render(oa[i]));
-						}
-					}
-					sb.append("]");
-					return sb.toString();
+					return renderArray(o, objectClass).toString();
 				} else {
 					return o.toString();
 				}
@@ -105,4 +43,88 @@
 		}
 		return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
 	}
+
+	private static StringBuffer renderArray(Object o, Class objectClass) {
+		Class componentType = objectClass.getComponentType();
+		StringBuffer sb = new StringBuffer("[");
+
+		if (componentType.isPrimitive() == false) {
+			Object[] oa = (Object[]) o;
+			for (int i = 0; i < oa.length; i++) {
+				if (i > 0) {
+					sb.append(ELEMENT_SEPARATOR);
+				}
+				sb.append(render(oa[i]));
+			}
+		} else {
+			if (Boolean.TYPE.equals(componentType)) {
+				boolean[] ba = (boolean[]) o;
+				for (int i = 0; i < ba.length; i++) {
+					if (i > 0) {
+						sb.append(ELEMENT_SEPARATOR);
+					}
+					sb.append(ba[i]);
+				}
+			} else if (Integer.TYPE.equals(componentType)) {
+				int[] ia = (int[]) o;
+				for (int i = 0; i < ia.length; i++) {
+					if (i > 0) {
+						sb.append(ELEMENT_SEPARATOR);
+					}
+					sb.append(ia[i]);
+				}
+
+			} else if (Long.TYPE.equals(componentType)) {
+				long[] ia = (long[]) o;
+				for (int i = 0; i < ia.length; i++) {
+					if (i > 0) {
+						sb.append(ELEMENT_SEPARATOR);
+					}
+					sb.append(ia[i]);
+				}
+			} else if (Double.TYPE.equals(componentType)) {
+				double[] ia = (double[]) o;
+				for (int i = 0; i < ia.length; i++) {
+					if (i > 0) {
+						sb.append(ELEMENT_SEPARATOR);
+					}
+					sb.append(ia[i]);
+				}
+			} else if (Float.TYPE.equals(componentType)) {
+				float[] ia = (float[]) o;
+				for (int i = 0; i < ia.length; i++) {
+					if (i > 0) {
+						sb.append(ELEMENT_SEPARATOR);
+					}
+					sb.append(ia[i]);
+				}
+			} else if (Character.TYPE.equals(componentType)) {
+				char[] ia = (char[]) o;
+				for (int i = 0; i < ia.length; i++) {
+					if (i > 0) {
+						sb.append(ELEMENT_SEPARATOR);
+					}
+					sb.append(ia[i]);
+				}
+			} else if (Short.TYPE.equals(componentType)) {
+				short[] ia = (short[]) o;
+				for (int i = 0; i < ia.length; i++) {
+					if (i > 0) {
+						sb.append(ELEMENT_SEPARATOR);
+					}
+					sb.append(ia[i]);
+				}
+			} else if (Byte.TYPE.equals(componentType)) {
+				byte[] ia = (byte[]) o;
+				for (int i = 0; i < ia.length; i++) {
+					if (i > 0) {
+						sb.append(ELEMENT_SEPARATOR);
+					}
+					sb.append(ia[i]);
+				}
+			}
+		}
+		sb.append("]");
+		return sb;
+	}
 }

Modified: slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/instrumentation/ToStringHelperTest.java
==============================================================================
--- slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/instrumentation/ToStringHelperTest.java	(original)
+++ slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/instrumentation/ToStringHelperTest.java	Sun Dec 21 22:48:17 2008
@@ -8,31 +8,47 @@
 		assertEquals("", "null", ToStringHelper.render(null));
 		assertEquals("", "a", ToStringHelper.render("a"));
 		assertEquals("", "[]", ToStringHelper.render(new String[0]));
+
 		assertEquals("", "[a]", ToStringHelper.render(new String[] { "a" }));
+
 		assertEquals("", "[a, b]", ToStringHelper.render(new String[] { "a",
 				"b" }));
+
 		assertEquals("", "[a, b, c]", ToStringHelper.render(new String[] { "a",
 				"b", "c" }));
+
 		assertEquals("", "[[a], [b, c]]", ToStringHelper.render(new String[][] {
 				{ "a" }, { "b", "c" } }));
+
+		assertEquals("", "[0, [a], [b, c]]", ToStringHelper
+				.render(new Object[] { "0", new String[] { "a" },
+						new Object[] { "b", "c" } }));
+
 		assertEquals("", "[1]", ToStringHelper.render(new int[] { 1 }));
+
 		assertEquals("", "[1, 2, 3]", ToStringHelper
 				.render(new int[] { 1, 2, 3 }));
+
 		assertEquals("", "[1, 2, 3]", ToStringHelper.render(new long[] { 1, 2,
 				3 }));
+
 		assertEquals("", "[1, 2, 3]", ToStringHelper.render(new short[] { 1, 2,
 				3 }));
+
 		assertEquals("", "[[1, 2], [], [3, 4]]", ToStringHelper
 				.render(new byte[][] { { 1, 2 }, {}, { 3, 4 } }));
+
 		assertEquals("", "[1.0, 2.0, 3.0]", ToStringHelper.render(new float[] {
 				1, 2, 3 }));
+
 		assertEquals("", "[1.0, 2.0, 3.0]", ToStringHelper.render(new double[] {
 				1, 2, 3 }));
+
+		assertEquals("", "[[1.0, 2.0, 3.0]]", ToStringHelper
+				.render(new double[][] { { 1, 2, 3 } }));
+
 		assertEquals("", "[true, false, true]", ToStringHelper
 				.render(new boolean[] { true, false, true }));
-		assertEquals("", "null", ToStringHelper.render(null));
-		assertEquals("", "null", ToStringHelper.render(null));
-		assertEquals("", "null", ToStringHelper.render(null));
 	}
 
 }



More information about the slf4j-dev mailing list