17
17
18
18
import java .io .Serial ;
19
19
import java .lang .reflect .Method ;
20
+ import java .lang .reflect .Type ;
21
+ import java .util .Arrays ;
22
+ import java .util .stream .Collectors ;
20
23
21
24
import org .jspecify .annotations .Nullable ;
22
25
31
34
public final class QueryCreationException extends RepositoryCreationException {
32
35
33
36
private static final @ Serial long serialVersionUID = -1238456123580L ;
34
- private static final String MESSAGE_TEMPLATE = "Could not create query for method [%s]; Could not find property '%s' on domain class '%s'" ;
35
37
36
38
private final Method method ;
37
39
@@ -81,7 +83,8 @@ private QueryCreationException(@Nullable String message, @Nullable Throwable cau
81
83
public static QueryCreationException invalidProperty (QueryMethod method , String propertyName ) {
82
84
83
85
return new QueryCreationException (
84
- String .format (MESSAGE_TEMPLATE , method , propertyName , method .getDomainClass ().getName ()), method );
86
+ "Could not find property '%s' on domain class '%s'" .formatted (propertyName , method .getDomainClass ().getName ()),
87
+ method );
85
88
}
86
89
87
90
/**
@@ -92,7 +95,7 @@ public static QueryCreationException invalidProperty(QueryMethod method, String
92
95
* @return the {@link QueryCreationException}.
93
96
*/
94
97
public static QueryCreationException create (QueryMethod method , String message ) {
95
- return new QueryCreationException (createMessage ( message , method . getMethod ()) , method );
98
+ return new QueryCreationException (message , method );
96
99
}
97
100
98
101
/**
@@ -134,13 +137,10 @@ public static QueryCreationException create(QueryMethod method, String message,
134
137
*/
135
138
public static QueryCreationException create (@ Nullable String message , @ Nullable Throwable cause ,
136
139
Class <?> repositoryInterface , Method method ) {
137
- return new QueryCreationException (createMessage ( message , method ) ,
140
+ return new QueryCreationException (message ,
138
141
cause , repositoryInterface , method );
139
142
}
140
143
141
- private static String createMessage (@ Nullable String message , Method method ) {
142
- return String .format ("Could not create query for [%s]; Reason: %s" , method , message );
143
- }
144
144
145
145
/**
146
146
* @return the method causing the exception.
@@ -150,4 +150,19 @@ public Method getMethod() {
150
150
return method ;
151
151
}
152
152
153
+ @ Override
154
+ public String getLocalizedMessage () {
155
+
156
+ StringBuilder sb = new StringBuilder ();
157
+ sb .append (method .getDeclaringClass ().getSimpleName ()).append ('.' );
158
+ sb .append (method .getName ());
159
+
160
+ sb .append (method .getName ());
161
+ sb .append (Arrays .stream (method .getParameterTypes ()) //
162
+ .map (Type ::getTypeName ) //
163
+ .collect (Collectors .joining ("," , "(" , ")" )));
164
+
165
+ return "Cannot create query for method [%s]; %s" .formatted (sb .toString (), getMessage ());
166
+ }
167
+
153
168
}
0 commit comments