View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * The interface that resolve an SQL provider method via an SQL provider class.
27   * <p>
28   * This interface need to implements at an SQL provider class and it need to define the default constructor for creating
29   * a new instance.
30   *
31   * @since 3.5.1
32   *
33   * @author Kazuki Shimizu
34   */
35  public interface ProviderMethodResolver {
36  
37    /**
38     * Resolve an SQL provider method.
39     * <p>
40     * The default implementation return a method that matches following conditions.
41     * <ul>
42     * <li>Method name matches with mapper method</li>
43     * <li>Return type matches the {@link CharSequence}({@link String}, {@link StringBuilder}, etc...)</li>
44     * </ul>
45     * If matched method is zero or multiple, it throws a {@link BuilderException}.
46     *
47     * @param context
48     *          a context for SQL provider
49     *
50     * @return an SQL provider method
51     *
52     * @throws BuilderException
53     *           Throws when cannot resolve a target method
54     */
55    default Method resolveMethod(ProviderContext context) {
56      List<Method> sameNameMethods = Arrays.stream(getClass().getMethods())
57          .filter(m -> m.getName().equals(context.getMapperMethod().getName())).collect(Collectors.toList());
58      if (sameNameMethods.isEmpty()) {
59        throw new BuilderException("Cannot resolve the provider method because '" + context.getMapperMethod().getName()
60            + "' not found in SqlProvider '" + getClass().getName() + "'.");
61      }
62      List<Method> targetMethods = sameNameMethods.stream()
63          .filter(m -> CharSequence.class.isAssignableFrom(m.getReturnType())).collect(Collectors.toList());
64      if (targetMethods.size() == 1) {
65        return targetMethods.get(0);
66      }
67      if (targetMethods.isEmpty()) {
68        throw new BuilderException("Cannot resolve the provider method because '" + context.getMapperMethod().getName()
69            + "' does not return the CharSequence or its subclass in SqlProvider '" + getClass().getName() + "'.");
70      }
71      throw new BuilderException("Cannot resolve the provider method because '" + context.getMapperMethod().getName()
72          + "' is found multiple in SqlProvider '" + getClass().getName() + "'.");
73    }
74  
75  }