1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.builder.annotation;
17
18 import java.lang.reflect.Method;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.stream.Collectors;
22
23 import org.apache.ibatis.builder.BuilderException;
24
25
26
27
28
29
30
31
32
33
34 public interface ProviderMethodResolver {
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 default Method resolveMethod(ProviderContext context) {
51 List<Method> sameNameMethods = Arrays.stream(getClass().getMethods())
52 .filter(m -> m.getName().equals(context.getMapperMethod().getName()))
53 .collect(Collectors.toList());
54 if (sameNameMethods.isEmpty()) {
55 throw new BuilderException("Cannot resolve the provider method because '"
56 + context.getMapperMethod().getName() + "' not found in SqlProvider '" + getClass().getName() + "'.");
57 }
58 List<Method> targetMethods = sameNameMethods.stream()
59 .filter(m -> CharSequence.class.isAssignableFrom(m.getReturnType()))
60 .collect(Collectors.toList());
61 if (targetMethods.size() == 1) {
62 return targetMethods.get(0);
63 }
64 if (targetMethods.isEmpty()) {
65 throw new BuilderException("Cannot resolve the provider method because '"
66 + context.getMapperMethod().getName() + "' does not return the CharSequence or its subclass in SqlProvider '"
67 + getClass().getName() + "'.");
68 } else {
69 throw new BuilderException("Cannot resolve the provider method because '"
70 + context.getMapperMethod().getName() + "' is found multiple in SqlProvider '" + getClass().getName() + "'.");
71 }
72 }
73
74 }