Skip to content

Commit f16366e

Browse files
committed
Introduce SimpleValueStyler for use with ToStringCreator
DefaultValueStyler hard codes conventions for styling that are verbose and do not align well with standard toString() implementations in the JDK for arrays, collections, and maps. Furthermore, the default styling for classes and methods may not be suitable or desirable for certain use cases. To address these shortcomings, this commit introduces a SimpleValueStyler for use with ToStringCreator. The default behavior of SimpleValueStyler aligns with toString() implementations for arrays, collections, and maps in the JDK, and styling for classes and methods is configurable via a dedicated constructor. Closes gh-29381
1 parent 388f7bf commit f16366e

File tree

2 files changed

+338
-0
lines changed

2 files changed

+338
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.style;
18+
19+
import java.lang.reflect.Method;
20+
import java.util.Arrays;
21+
import java.util.Collection;
22+
import java.util.Map;
23+
import java.util.StringJoiner;
24+
import java.util.function.Function;
25+
import java.util.stream.Collectors;
26+
27+
/**
28+
* {@link ValueStyler} that converts objects to String form — generally for
29+
* debugging purposes — using simple styling conventions that mimic the
30+
* {@code toString()} styling conventions for standard JDK implementations of
31+
* collections, maps, and arrays.
32+
*
33+
* <p>Uses the reflective visitor pattern underneath the hood to nicely
34+
* encapsulate styling algorithms for each type of styled object.
35+
*
36+
* <p>Favor {@link SimpleValueStyler} over {@link DefaultValueStyler} when you
37+
* wish to use styling similar to the JDK or when you need configurable control
38+
* over the styling of classes and methods.
39+
*
40+
* @author Sam Brannen
41+
* @since 6.0
42+
*/
43+
public class SimpleValueStyler extends DefaultValueStyler {
44+
45+
/**
46+
* Default {@link Class} styling function: {@link Class#getCanonicalName()}.
47+
*/
48+
public static final Function<Class<?>, String> DEFAULT_CLASS_STYLER = Class::getCanonicalName;
49+
50+
/**
51+
* Default {@link Method} styling function: converts the supplied {@link Method}
52+
* to a simple string representation of the method's signature in the form of
53+
* {@code <method name>(<parameter types>)}, where {@code <parameter types>}
54+
* is a comma-separated list of the {@linkplain Class#getSimpleName() simple names}
55+
* of the parameter types.
56+
* <p>For example, if the supplied method is a reference to
57+
* {@link String#getBytes(java.nio.charset.Charset)}, this function will
58+
* return {@code "getBytes(Charset)"}.
59+
*/
60+
public static final Function<Method, String> DEFAULT_METHOD_STYLER = SimpleValueStyler::toSimpleMethodSignature;
61+
62+
63+
private final Function<Class<?>, String> classStyler;
64+
65+
private final Function<Method, String> methodStyler;
66+
67+
68+
/**
69+
* Create a {@code SimpleValueStyler} using the {@link #DEFAULT_CLASS_STYLER}
70+
* and {@link #DEFAULT_METHOD_STYLER}.
71+
*/
72+
public SimpleValueStyler() {
73+
this(DEFAULT_CLASS_STYLER, DEFAULT_METHOD_STYLER);
74+
}
75+
76+
/**
77+
* Create a {@code SimpleValueStyler} using the supplied class and method stylers.
78+
* @param classStyler a function that applies styling to a {@link Class}
79+
* @param methodStyler a function that applies styling to a {@link Method}
80+
*/
81+
public SimpleValueStyler(Function<Class<?>, String> classStyler, Function<Method, String> methodStyler) {
82+
this.classStyler = classStyler;
83+
this.methodStyler = methodStyler;
84+
}
85+
86+
87+
@Override
88+
protected String styleNull() {
89+
return "null";
90+
}
91+
92+
@Override
93+
protected String styleString(String str) {
94+
return "\"" + str + "\"";
95+
}
96+
97+
@Override
98+
protected String styleClass(Class<?> clazz) {
99+
return this.classStyler.apply(clazz);
100+
}
101+
102+
@Override
103+
protected String styleMethod(Method method) {
104+
return this.methodStyler.apply(method);
105+
}
106+
107+
@Override
108+
protected <K, V> String styleMap(Map<K, V> map) {
109+
StringJoiner result = new StringJoiner(", ", "{", "}");
110+
for (Map.Entry<K, V> entry : map.entrySet()) {
111+
result.add(style(entry));
112+
}
113+
return result.toString();
114+
}
115+
116+
@Override
117+
protected String styleCollection(Collection<?> collection) {
118+
StringJoiner result = new StringJoiner(", ", "[", "]");
119+
for (Object element : collection) {
120+
result.add(style(element));
121+
}
122+
return result.toString();
123+
}
124+
125+
@Override
126+
protected String styleArray(Object[] array) {
127+
StringJoiner result = new StringJoiner(", ", "[", "]");
128+
for (Object element : array) {
129+
result.add(style(element));
130+
}
131+
return result.toString();
132+
}
133+
134+
private static String toSimpleMethodSignature(Method method) {
135+
String parameterList = Arrays.stream(method.getParameterTypes())
136+
.map(Class::getSimpleName)
137+
.collect(Collectors.joining(", "));
138+
return String.format("%s(%s)", method.getName(), parameterList);
139+
}
140+
141+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.style;
18+
19+
import java.lang.reflect.Method;
20+
import java.nio.charset.Charset;
21+
import java.util.LinkedHashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import org.junit.jupiter.api.Nested;
26+
import org.junit.jupiter.api.Test;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Unit tests for {@link SimpleValueStyler}.
32+
*
33+
* @author Sam Brannen
34+
* @since 6.0
35+
*/
36+
class SimpleValueStylerTests {
37+
38+
@Nested
39+
class CommonStyling {
40+
41+
private final SimpleValueStyler styler = new SimpleValueStyler();
42+
43+
@Test
44+
void styleBasics() throws NoSuchMethodException {
45+
assertThat(styler.style(null)).isEqualTo("null");
46+
assertThat(styler.style(true)).isEqualTo("true");
47+
assertThat(styler.style(99.9)).isEqualTo("99.9");
48+
assertThat(styler.style("str")).isEqualTo("\"str\"");
49+
}
50+
51+
@Test
52+
void stylePlainObject() {
53+
Object obj = new Object();
54+
55+
assertThat(styler.style(obj)).isEqualTo(String.valueOf(obj));
56+
}
57+
58+
@Test
59+
void styleMaps() {
60+
assertThat(styler.style(Map.of())).isEqualTo("{}");
61+
assertThat(styler.style(Map.of("key", 1))).isEqualTo("{\"key\" -> 1}");
62+
63+
Map<String, Integer> map = new LinkedHashMap<>() {{
64+
put("key1", 1);
65+
put("key2", 2);
66+
}};
67+
assertThat(styler.style(map)).isEqualTo("{\"key1\" -> 1, \"key2\" -> 2}");
68+
}
69+
70+
@Test
71+
void styleMapEntries() {
72+
Map<String, Integer> map = Map.of("key1", 1, "key2", 2);
73+
74+
assertThat(map.entrySet()).map(styler::style).containsExactlyInAnyOrder("\"key1\" -> 1", "\"key2\" -> 2");
75+
}
76+
77+
@Test
78+
void styleLists() {
79+
assertThat(styler.style(List.of())).isEqualTo("[]");
80+
assertThat(styler.style(List.of(1))).isEqualTo("[1]");
81+
assertThat(styler.style(List.of(1, 2))).isEqualTo("[1, 2]");
82+
}
83+
84+
@Test
85+
void stylePrimitiveArrays() {
86+
int[] array = new int[0];
87+
assertThat(styler.style(array)).isEqualTo("[]");
88+
89+
array = new int[] { 1 };
90+
assertThat(styler.style(array)).isEqualTo("[1]");
91+
92+
array = new int[] { 1, 2 };
93+
assertThat(styler.style(array)).isEqualTo("[1, 2]");
94+
}
95+
96+
@Test
97+
void styleObjectArrays() {
98+
String[] array = new String[0];
99+
assertThat(styler.style(array)).isEqualTo("[]");
100+
101+
array = new String[] { "str1" };
102+
assertThat(styler.style(array)).isEqualTo("[\"str1\"]");
103+
104+
array = new String[] { "str1", "str2" };
105+
assertThat(styler.style(array)).isEqualTo("[\"str1\", \"str2\"]");
106+
}
107+
108+
}
109+
110+
@Nested
111+
class DefaultClassAndMethodStylers {
112+
113+
private final SimpleValueStyler styler = new SimpleValueStyler();
114+
115+
@Test
116+
void styleClass() {
117+
assertThat(styler.style(String.class)).isEqualTo("java.lang.String");
118+
assertThat(styler.style(getClass())).isEqualTo(getClass().getCanonicalName());
119+
assertThat(styler.style(String[].class)).isEqualTo("java.lang.String[]");
120+
assertThat(styler.style(int[][].class)).isEqualTo("int[][]");
121+
}
122+
123+
@Test
124+
void styleMethod() throws NoSuchMethodException {
125+
assertThat(styler.style(String.class.getMethod("toString"))).isEqualTo("toString()");
126+
assertThat(styler.style(String.class.getMethod("getBytes", Charset.class))).isEqualTo("getBytes(Charset)");
127+
}
128+
129+
@Test
130+
void styleClassMap() {
131+
Map<String, Class<?>> map = new LinkedHashMap<>() {{
132+
put("key1", Integer.class);
133+
put("key2", DefaultClassAndMethodStylers.class);
134+
}};
135+
assertThat(styler.style(map)).isEqualTo(
136+
"{\"key1\" -> java.lang.Integer, \"key2\" -> %s}",
137+
DefaultClassAndMethodStylers.class.getCanonicalName());
138+
}
139+
140+
@Test
141+
void styleClassList() {
142+
assertThat(styler.style(List.of(Integer.class, String.class)))
143+
.isEqualTo("[java.lang.Integer, java.lang.String]");
144+
}
145+
146+
@Test
147+
void styleClassArray() {
148+
Class<?>[] array = new Class<?>[] { Integer.class, getClass() };
149+
assertThat(styler.style(array))
150+
.isEqualTo("[%s, %s]", Integer.class.getCanonicalName(), getClass().getCanonicalName());
151+
}
152+
153+
}
154+
155+
@Nested
156+
class CustomClassAndMethodStylers {
157+
158+
private final SimpleValueStyler styler = new SimpleValueStyler(Class::getSimpleName, Method::toGenericString);
159+
160+
@Test
161+
void styleClass() {
162+
assertThat(styler.style(String.class)).isEqualTo("String");
163+
assertThat(styler.style(getClass())).isEqualTo(getClass().getSimpleName());
164+
assertThat(styler.style(String[].class)).isEqualTo("String[]");
165+
assertThat(styler.style(int[][].class)).isEqualTo("int[][]");
166+
}
167+
168+
@Test
169+
void styleMethod() throws NoSuchMethodException {
170+
Method method = String.class.getMethod("toString");
171+
assertThat(styler.style(method)).isEqualTo(method.toGenericString());
172+
}
173+
174+
@Test
175+
void styleClassMap() {
176+
Map<String, Class<?>> map = new LinkedHashMap<>() {{
177+
put("key1", Integer.class);
178+
put("key2", CustomClassAndMethodStylers.class);
179+
}};
180+
assertThat(styler.style(map)).isEqualTo(
181+
"{\"key1\" -> %s, \"key2\" -> %s}",
182+
Integer.class.getSimpleName(), CustomClassAndMethodStylers.class.getSimpleName());
183+
}
184+
@Test
185+
void styleClassList() {
186+
assertThat(styler.style(List.of(Integer.class, String.class))).isEqualTo("[Integer, String]");
187+
}
188+
189+
@Test
190+
void styleClassArray() {
191+
Class<?>[] array = new Class<?>[] { Integer.class, getClass() };
192+
assertThat(styler.style(array)).isEqualTo("[%s, %s]", Integer.class.getSimpleName(), getClass().getSimpleName());
193+
}
194+
195+
}
196+
197+
}

0 commit comments

Comments
 (0)