Skip to content

Commit 877284a

Browse files
committed
Improve the error message when return type does not matched the CharSequence or its subclass
See gh-1279
1 parent b1cfb84 commit 877284a

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/main/java/org/apache/ibatis/builder/annotation/ProviderMethodResolver.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.util.Arrays;
21-
import java.util.Date;
2221
import java.util.List;
2322
import java.util.stream.Collectors;
2423

@@ -50,18 +49,25 @@ public interface ProviderMethodResolver {
5049
* @throws BuilderException Throws when cannot resolve a target method
5150
*/
5251
default Method resolveMethod(ProviderContext context) {
53-
List<Method> targetMethods = Arrays.stream(getClass().getMethods())
52+
List<Method> sameNameMethods = Arrays.stream(getClass().getMethods())
5453
.filter(m -> m.getName().equals(context.getMapperMethod().getName()))
54+
.collect(Collectors.toList());
55+
if (sameNameMethods.isEmpty()) {
56+
throw new BuilderException("Cannot resolve the provider method because '"
57+
+ context.getMapperMethod().getName() + "' not found in SqlProvider '" + getClass().getName() + "'.");
58+
}
59+
List<Method> targetMethods = sameNameMethods.stream()
5560
.filter(m -> CharSequence.class.isAssignableFrom(m.getReturnType()))
5661
.collect(Collectors.toList());
5762
if (targetMethods.size() == 1) {
5863
return targetMethods.get(0);
5964
}
6065
if (targetMethods.isEmpty()) {
61-
throw new BuilderException("Cannot resolve the provide method because '"
62-
+ context.getMapperMethod().getName() + "' not found in SqlProvider '" + getClass().getName() + "'.");
66+
throw new BuilderException("Cannot resolve the provider method because '"
67+
+ context.getMapperMethod().getName() + "' does not return the CharSequence or its subclass in SqlProvider '"
68+
+ getClass().getName() + "'.");
6369
} else {
64-
throw new BuilderException("Cannot resolve the provide method because '"
70+
throw new BuilderException("Cannot resolve the provider method because '"
6571
+ context.getMapperMethod().getName() + "' is found multiple in SqlProvider '" + getClass().getName() + "'.");
6672
}
6773
}

src/test/java/org/apache/ibatis/submitted/sqlprovider/ProviderMethodResolutionTest.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,20 @@ void shouldResolveWhenDefaultResolverMatchedMethodIsOne() {
6666
}
6767

6868
@Test
69-
void shouldErrorWhenDefaultResolverMatchedMethodIsNone() {
69+
void shouldErrorWhenDefaultResolverMethodNameMatchedMethodIsNone() {
7070
BuilderException e = Assertions.assertThrows(BuilderException.class,
71-
() -> sqlSessionFactory.getConfiguration().addMapper(DefaultProvideMethodResolverMatchedMethodIsNoneMapper.class));
71+
() -> sqlSessionFactory.getConfiguration().addMapper(DefaultProvideMethodResolverMethodNameMatchedMethodIsNoneMapper.class));
7272
assertEquals(
73-
"Cannot resolve the provide method because 'insert' not found in SqlProvider 'org.apache.ibatis.submitted.sqlprovider.ProviderMethodResolutionTest$DefaultProvideMethodResolverMatchedMethodIsNoneMapper$MethodResolverBasedSqlProvider'.",
73+
"Cannot resolve the provider method because 'insert' not found in SqlProvider 'org.apache.ibatis.submitted.sqlprovider.ProviderMethodResolutionTest$DefaultProvideMethodResolverMethodNameMatchedMethodIsNoneMapper$MethodResolverBasedSqlProvider'.",
74+
e.getCause().getMessage());
75+
}
76+
77+
@Test
78+
void shouldErrorWhenDefaultResolverReturnTypeMatchedMethodIsNone() {
79+
BuilderException e = Assertions.assertThrows(BuilderException.class,
80+
() -> sqlSessionFactory.getConfiguration().addMapper(DefaultProvideMethodResolverReturnTypeMatchedMethodIsNoneMapper.class));
81+
assertEquals(
82+
"Cannot resolve the provider method because 'insert' does not return the CharSequence or its subclass in SqlProvider 'org.apache.ibatis.submitted.sqlprovider.ProviderMethodResolutionTest$DefaultProvideMethodResolverReturnTypeMatchedMethodIsNoneMapper$MethodResolverBasedSqlProvider'.",
7483
e.getCause().getMessage());
7584
}
7685

@@ -79,7 +88,7 @@ void shouldErrorWhenDefaultResolverMatchedMethodIsMultiple() {
7988
BuilderException e = Assertions.assertThrows(BuilderException.class,
8089
() -> sqlSessionFactory.getConfiguration().addMapper(DefaultProvideMethodResolverMatchedMethodIsMultipleMapper.class));
8190
assertEquals(
82-
"Cannot resolve the provide method because 'update' is found multiple in SqlProvider 'org.apache.ibatis.submitted.sqlprovider.ProviderMethodResolutionTest$DefaultProvideMethodResolverMatchedMethodIsMultipleMapper$MethodResolverBasedSqlProvider'.",
91+
"Cannot resolve the provider method because 'update' is found multiple in SqlProvider 'org.apache.ibatis.submitted.sqlprovider.ProviderMethodResolutionTest$DefaultProvideMethodResolverMatchedMethodIsMultipleMapper$MethodResolverBasedSqlProvider'.",
8392
e.getCause().getMessage());
8493
}
8594

@@ -187,7 +196,20 @@ default Method resolveMethod(ProviderContext context) {
187196
}
188197
}
189198

190-
interface DefaultProvideMethodResolverMatchedMethodIsNoneMapper {
199+
interface DefaultProvideMethodResolverMethodNameMatchedMethodIsNoneMapper {
200+
201+
@InsertProvider(type = MethodResolverBasedSqlProvider.class)
202+
int insert();
203+
204+
class MethodResolverBasedSqlProvider implements ProviderMethodResolver {
205+
public static String provideInsertSql() {
206+
return "INSERT INTO foo (name) VALUES(#{name})";
207+
}
208+
}
209+
210+
}
211+
212+
interface DefaultProvideMethodResolverReturnTypeMatchedMethodIsNoneMapper {
191213

192214
@InsertProvider(type = MethodResolverBasedSqlProvider.class)
193215
int insert();

0 commit comments

Comments
 (0)