[slf4j-dev] svn commit: r1259 - 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 21:30:59 CET 2008


Author: ravn
Date: Sun Dec 21 21:30:58 2008
New Revision: 1259

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

Log:
added improved rendering of arrays of arrays, and primitive elements.


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 21:30:58 2008
@@ -14,7 +14,90 @@
 		Class objectClass = o.getClass();
 		if (unrenderableClasses.containsKey(objectClass) == false) {
 			try {
-				return o.toString();
+				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();
+				} else {
+					return o.toString();
+				}
 			} catch (Exception e) {
 				Long now = new Long(System.currentTimeMillis());
 				unrenderableClasses.put(objectClass, now);

Added: slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/instrumentation/ToStringHelperTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/instrumentation/ToStringHelperTest.java	Sun Dec 21 21:30:58 2008
@@ -0,0 +1,38 @@
+package org.slf4j.instrumentation;
+
+import junit.framework.TestCase;
+
+public class ToStringHelperTest extends TestCase {
+
+	public void testRenderer() {
+		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("", "[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("", "[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