Skip to content

Commit 4036ffc

Browse files
philwebbcbeams
authored andcommitted
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.
1 parent 2081521 commit 4036ffc

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import static java.lang.String.format;
2020

21+
import java.util.Iterator;
2122
import java.util.LinkedHashMap;
2223
import java.util.Map;
2324

2425
import org.springframework.util.Assert;
26+
import org.springframework.util.StringUtils;
2527

2628
/**
2729
* {@link LinkedHashMap} subclass representing annotation attribute key/value pairs
@@ -129,4 +131,28 @@ private <T> T doGet(String attributeName, Class<T> expectedType) {
129131
attributeName, value.getClass().getSimpleName(), expectedType.getSimpleName()));
130132
return (T) value;
131133
}
132-
}
134+
135+
public String toString() {
136+
Iterator<Map.Entry<String, Object>> entries = entrySet().iterator();
137+
StringBuilder sb = new StringBuilder("{");
138+
while (entries.hasNext()) {
139+
Map.Entry<String, Object> entry = entries.next();
140+
sb.append(entry.getKey());
141+
sb.append('=');
142+
sb.append(valueToString(entry.getValue()));
143+
sb.append(entries.hasNext() ? ", " : "");
144+
}
145+
sb.append("}");
146+
return sb.toString();
147+
}
148+
149+
private String valueToString(Object value) {
150+
if (value == this) {
151+
return "(this Map)";
152+
}
153+
if (value instanceof Object[]) {
154+
return "[" + StringUtils.arrayToCommaDelimitedString((Object[]) value) + "]";
155+
}
156+
return String.valueOf(value);
157+
}
158+
}

0 commit comments

Comments
 (0)