1
1
/*
2
- * Copyright 2012-2014 the original author or authors.
2
+ * Copyright 2012-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springframework .data .mapping ;
17
17
18
+ import java .util .ArrayList ;
19
+ import java .util .Arrays ;
18
20
import java .util .List ;
19
21
22
+ import org .springframework .beans .PropertyMatches ;
20
23
import org .springframework .data .util .TypeInformation ;
21
24
import org .springframework .util .Assert ;
25
+ import org .springframework .util .StringUtils ;
22
26
23
27
/**
24
28
* Exception being thrown when creating {@link PropertyPath} instances.
@@ -29,10 +33,12 @@ public class PropertyReferenceException extends RuntimeException {
29
33
30
34
private static final long serialVersionUID = -5254424051438976570L ;
31
35
private static final String ERROR_TEMPLATE = "No property %s found for type %s!" ;
36
+ private static final String HINTS_TEMPLATE = " Did you mean %s?" ;
32
37
33
38
private final String propertyName ;
34
39
private final TypeInformation <?> type ;
35
40
private final List <PropertyPath > alreadyResolvedPath ;
41
+ private final List <String > propertyMatches ;
36
42
37
43
/**
38
44
* Creates a new {@link PropertyReferenceException}.
@@ -41,14 +47,16 @@ public class PropertyReferenceException extends RuntimeException {
41
47
* @param type the type the property could not be found on.
42
48
* @param alreadyResolvedPah the previously calculated {@link PropertyPath}s.
43
49
*/
44
- public PropertyReferenceException (String propertyName , TypeInformation <?> type , List <PropertyPath > alreadyResolvedPah ) {
50
+ public PropertyReferenceException (String propertyName , TypeInformation <?> type ,
51
+ List <PropertyPath > alreadyResolvedPah ) {
45
52
46
53
Assert .hasText (propertyName );
47
54
Assert .notNull (type );
48
55
49
56
this .propertyName = propertyName ;
50
57
this .type = type ;
51
58
this .alreadyResolvedPath = alreadyResolvedPah ;
59
+ this .propertyMatches = detectPotentialMatches (propertyName , type .getType ());
52
60
}
53
61
54
62
/**
@@ -76,11 +84,18 @@ public TypeInformation<?> getType() {
76
84
@ Override
77
85
public String getMessage () {
78
86
79
- StringBuilder builder = new StringBuilder (String .format (ERROR_TEMPLATE , propertyName , type .getType ()
80
- .getSimpleName ()));
87
+ StringBuilder builder = new StringBuilder (
88
+ String .format (ERROR_TEMPLATE , propertyName , type .getType ().getSimpleName ()));
89
+
90
+ if (!propertyMatches .isEmpty ()) {
91
+ String matches = StringUtils .collectionToDelimitedString (propertyMatches , "," , "'" , "'" );
92
+ builder .append (String .format (HINTS_TEMPLATE , matches ));
93
+ }
81
94
82
95
if (!alreadyResolvedPath .isEmpty ()) {
83
- builder .append (" Traversed path: " ).append (alreadyResolvedPath .get (0 ).toString ()).append ("." );
96
+ builder .append (" Traversed path: " );
97
+ builder .append (alreadyResolvedPath .get (0 ).toString ());
98
+ builder .append ("." );
84
99
}
85
100
86
101
return builder .toString ();
@@ -105,4 +120,20 @@ public PropertyPath getBaseProperty() {
105
120
public boolean hasDeeperResolutionDepthThan (PropertyReferenceException exception ) {
106
121
return this .alreadyResolvedPath .size () > exception .alreadyResolvedPath .size ();
107
122
}
123
+
124
+ /**
125
+ * Detects all potential matches for the given property name and type.
126
+ *
127
+ * @param propertyName must not be {@literal null} or empty.
128
+ * @param type must not be {@literal null}.
129
+ * @return
130
+ */
131
+ private static List <String > detectPotentialMatches (String propertyName , Class <?> type ) {
132
+
133
+ List <String > result = new ArrayList <String >();
134
+ result .addAll (Arrays .asList (PropertyMatches .forField (propertyName , type ).getPossibleMatches ()));
135
+ result .addAll (Arrays .asList (PropertyMatches .forProperty (propertyName , type ).getPossibleMatches ()));
136
+
137
+ return result ;
138
+ }
108
139
}
0 commit comments