提交 4036ffc4 编写于 作者: P Phillip Webb 提交者: Chris Beams

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.
上级 20815216
...@@ -18,10 +18,12 @@ package org.springframework.core.annotation; ...@@ -18,10 +18,12 @@ package org.springframework.core.annotation;
import static java.lang.String.format; import static java.lang.String.format;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* {@link LinkedHashMap} subclass representing annotation attribute key/value pairs * {@link LinkedHashMap} subclass representing annotation attribute key/value pairs
...@@ -129,4 +131,28 @@ public class AnnotationAttributes extends LinkedHashMap<String, Object> { ...@@ -129,4 +131,28 @@ public class AnnotationAttributes extends LinkedHashMap<String, Object> {
attributeName, value.getClass().getSimpleName(), expectedType.getSimpleName())); attributeName, value.getClass().getSimpleName(), expectedType.getSimpleName()));
return (T) value; return (T) value;
} }
}
\ No newline at end of file public String toString() {
Iterator<Map.Entry<String, Object>> entries = entrySet().iterator();
StringBuilder sb = new StringBuilder("{");
while (entries.hasNext()) {
Map.Entry<String, Object> 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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册