From 4036ffc4d4f62626208dccc63ffdefc112d4ed1c Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 30 Oct 2012 22:02:17 -0700 Subject: [PATCH] Improve #toString for AnnotationAttributes Improve the #toString method for AnnotationAttributes to print array values as a comma-separated list. This change is primarily to provide better variable inspection when debugging code within an IDE. --- .../core/annotation/AnnotationAttributes.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java index 1ea4ba3a47..50e13dac06 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java @@ -18,10 +18,12 @@ package org.springframework.core.annotation; import static java.lang.String.format; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * {@link LinkedHashMap} subclass representing annotation attribute key/value pairs @@ -129,4 +131,28 @@ public class AnnotationAttributes extends LinkedHashMap { attributeName, value.getClass().getSimpleName(), expectedType.getSimpleName())); return (T) value; } -} \ No newline at end of file + + public String toString() { + Iterator> entries = entrySet().iterator(); + StringBuilder sb = new StringBuilder("{"); + while (entries.hasNext()) { + Map.Entry entry = entries.next(); + sb.append(entry.getKey()); + sb.append('='); + sb.append(valueToString(entry.getValue())); + sb.append(entries.hasNext() ? ", " : ""); + } + sb.append("}"); + return sb.toString(); + } + + private String valueToString(Object value) { + if (value == this) { + return "(this Map)"; + } + if (value instanceof Object[]) { + return "[" + StringUtils.arrayToCommaDelimitedString((Object[]) value) + "]"; + } + return String.valueOf(value); + } +}