|
27 | 27 | import org.springframework.util.StringUtils;
|
28 | 28 |
|
29 | 29 | /**
|
30 |
| - * Helper class for calculating bean property matches, according to. |
31 |
| - * Used by BeanWrapperImpl to suggest alternatives for an invalid property name. |
| 30 | + * Helper class for calculating property matches, according to a configurable |
| 31 | + * distance. Provide the list of potential matches and an easy way to generate |
| 32 | + * an error message. Works for both java bean properties and fields. |
| 33 | + * <p> |
| 34 | + * Mainly for use within the framework and in particular the binding facility |
32 | 35 | *
|
33 | 36 | * @author Alef Arendsen
|
34 | 37 | * @author Arjen Poutsma
|
35 | 38 | * @author Juergen Hoeller
|
36 | 39 | * @author Stephane Nicoll
|
37 | 40 | * @since 2.0
|
38 | 41 | * @see #forProperty(String, Class)
|
| 42 | + * @see #forField(String, Class) |
39 | 43 | */
|
40 |
| -abstract class PropertyMatches { |
| 44 | +public abstract class PropertyMatches { |
41 | 45 |
|
42 | 46 | //---------------------------------------------------------------------
|
43 | 47 | // Static section
|
@@ -123,6 +127,21 @@ public String[] getPossibleMatches() {
|
123 | 127 | */
|
124 | 128 | public abstract String buildErrorMessage();
|
125 | 129 |
|
| 130 | + protected void appendHintMessage(StringBuilder msg) { |
| 131 | + msg.append("Did you mean "); |
| 132 | + for (int i = 0; i < this.possibleMatches.length; i++) { |
| 133 | + msg.append('\''); |
| 134 | + msg.append(this.possibleMatches[i]); |
| 135 | + if (i < this.possibleMatches.length - 2) { |
| 136 | + msg.append("', "); |
| 137 | + } |
| 138 | + else if (i == this.possibleMatches.length - 2) { |
| 139 | + msg.append("', or "); |
| 140 | + } |
| 141 | + } |
| 142 | + msg.append("'?"); |
| 143 | + } |
| 144 | + |
126 | 145 | /**
|
127 | 146 | * Calculate the distance between the given two Strings
|
128 | 147 | * according to the Levenshtein algorithm.
|
@@ -208,18 +227,7 @@ public String buildErrorMessage() {
|
208 | 227 | msg.append("Does the parameter type of the setter match the return type of the getter?");
|
209 | 228 | }
|
210 | 229 | else {
|
211 |
| - msg.append("Did you mean "); |
212 |
| - for (int i = 0; i < possibleMatches.length; i++) { |
213 |
| - msg.append('\''); |
214 |
| - msg.append(possibleMatches[i]); |
215 |
| - if (i < possibleMatches.length - 2) { |
216 |
| - msg.append("', "); |
217 |
| - } |
218 |
| - else if (i == possibleMatches.length - 2) { |
219 |
| - msg.append("', or "); |
220 |
| - } |
221 |
| - } |
222 |
| - msg.append("'?"); |
| 230 | + appendHintMessage(msg); |
223 | 231 | }
|
224 | 232 | return msg.toString();
|
225 | 233 | }
|
@@ -258,18 +266,7 @@ public String buildErrorMessage() {
|
258 | 266 | msg.append("' has no matching field. ");
|
259 | 267 |
|
260 | 268 | if (!ObjectUtils.isEmpty(possibleMatches)) {
|
261 |
| - msg.append("Did you mean "); |
262 |
| - for (int i = 0; i < possibleMatches.length; i++) { |
263 |
| - msg.append('\''); |
264 |
| - msg.append(possibleMatches[i]); |
265 |
| - if (i < possibleMatches.length - 2) { |
266 |
| - msg.append("', "); |
267 |
| - } |
268 |
| - else if (i == possibleMatches.length - 2) { |
269 |
| - msg.append("', or "); |
270 |
| - } |
271 |
| - } |
272 |
| - msg.append("'?"); |
| 269 | + appendHintMessage(msg); |
273 | 270 | }
|
274 | 271 | return msg.toString();
|
275 | 272 | }
|
|
0 commit comments