Skip to content

Commit 834368c

Browse files
committed
Polishing.
See #3373
1 parent 1c57978 commit 834368c

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

src/main/java/org/springframework/data/repository/core/RepositoryCreationException.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.repository.core;
1717

18+
import org.jspecify.annotations.Nullable;
19+
1820
import org.springframework.dao.InvalidDataAccessApiUsageException;
1921

2022
/**
@@ -34,7 +36,7 @@ public class RepositoryCreationException extends InvalidDataAccessApiUsageExcept
3436
* @param msg the detail message.
3537
* @param repositoryInterface the repository interface.
3638
*/
37-
public RepositoryCreationException(String msg, Class<?> repositoryInterface) {
39+
public RepositoryCreationException(@Nullable String msg, Class<?> repositoryInterface) {
3840
super(msg);
3941
this.repositoryInterface = repositoryInterface;
4042
}
@@ -46,12 +48,13 @@ public RepositoryCreationException(String msg, Class<?> repositoryInterface) {
4648
* @param cause the root cause from the data access API in use.
4749
* @param repositoryInterface the repository interface.
4850
*/
49-
public RepositoryCreationException(String msg, Throwable cause, Class<?> repositoryInterface) {
51+
public RepositoryCreationException(@Nullable String msg, @Nullable Throwable cause, Class<?> repositoryInterface) {
5052
super(msg, cause);
5153
this.repositoryInterface = repositoryInterface;
5254
}
5355

5456
public Class<?> getRepositoryInterface() {
5557
return repositoryInterface;
5658
}
59+
5760
}

src/main/java/org/springframework/data/repository/query/QueryCreationException.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import java.io.Serial;
1919
import java.lang.reflect.Method;
20+
import java.lang.reflect.Type;
21+
import java.util.Arrays;
22+
import java.util.stream.Collectors;
2023

2124
import org.jspecify.annotations.Nullable;
2225

@@ -31,7 +34,6 @@
3134
public final class QueryCreationException extends RepositoryCreationException {
3235

3336
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'";
3537

3638
private final Method method;
3739

@@ -81,7 +83,8 @@ private QueryCreationException(@Nullable String message, @Nullable Throwable cau
8183
public static QueryCreationException invalidProperty(QueryMethod method, String propertyName) {
8284

8385
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);
8588
}
8689

8790
/**
@@ -92,7 +95,7 @@ public static QueryCreationException invalidProperty(QueryMethod method, String
9295
* @return the {@link QueryCreationException}.
9396
*/
9497
public static QueryCreationException create(QueryMethod method, String message) {
95-
return new QueryCreationException(createMessage(message, method.getMethod()), method);
98+
return new QueryCreationException(message, method);
9699
}
97100

98101
/**
@@ -134,13 +137,10 @@ public static QueryCreationException create(QueryMethod method, String message,
134137
*/
135138
public static QueryCreationException create(@Nullable String message, @Nullable Throwable cause,
136139
Class<?> repositoryInterface, Method method) {
137-
return new QueryCreationException(createMessage(message, method),
140+
return new QueryCreationException(message,
138141
cause, repositoryInterface, method);
139142
}
140143

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-
}
144144

145145
/**
146146
* @return the method causing the exception.
@@ -150,4 +150,19 @@ public Method getMethod() {
150150
return method;
151151
}
152152

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+
153168
}

src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ protected Optional<QueryLookupStrategy> getQueryLookupStrategy(QueryLookupStrate
539539
};
540540

541541
assertThatThrownBy(() -> factory.getRepository(WithQueryMethodUsingInvalidProperty.class))
542-
.isInstanceOf(QueryCreationException.class).hasMessageContaining("findAllByName")
542+
.isInstanceOf(QueryCreationException.class)
543543
.hasMessageContaining("No property 'name' found for type 'Object'");
544544
}
545545

0 commit comments

Comments
 (0)