View Javadoc
1   /*
2    *    Copyright 2009-2025 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;
17  
18  import java.lang.reflect.Type;
19  import java.util.Arrays;
20  import java.util.HashSet;
21  import java.util.Set;
22  import java.util.regex.Pattern;
23  
24  import org.apache.ibatis.mapping.ParameterMode;
25  import org.apache.ibatis.mapping.ResultSetType;
26  import org.apache.ibatis.session.Configuration;
27  import org.apache.ibatis.type.JdbcType;
28  import org.apache.ibatis.type.TypeAliasRegistry;
29  import org.apache.ibatis.type.TypeHandler;
30  import org.apache.ibatis.type.TypeHandlerRegistry;
31  
32  /**
33   * @author Clinton Begin
34   */
35  public abstract class BaseBuilder {
36    protected final Configuration configuration;
37    protected final TypeAliasRegistry typeAliasRegistry;
38    protected final TypeHandlerRegistry typeHandlerRegistry;
39  
40    public BaseBuilder(Configuration configuration) {
41      this.configuration = configuration;
42      this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
43      this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
44    }
45  
46    public Configuration getConfiguration() {
47      return configuration;
48    }
49  
50    protected Pattern parseExpression(String regex, String defaultValue) {
51      return Pattern.compile(regex == null ? defaultValue : regex);
52    }
53  
54    protected Boolean booleanValueOf(String value, Boolean defaultValue) {
55      return value == null ? defaultValue : Boolean.valueOf(value);
56    }
57  
58    protected Integer integerValueOf(String value, Integer defaultValue) {
59      return value == null ? defaultValue : Integer.valueOf(value);
60    }
61  
62    protected Set<String> stringSetValueOf(String value, String defaultValue) {
63      value = value == null ? defaultValue : value;
64      return new HashSet<>(Arrays.asList(value.split(",")));
65    }
66  
67    protected JdbcType resolveJdbcType(String alias) {
68      try {
69        return alias == null ? null : JdbcType.valueOf(alias);
70      } catch (IllegalArgumentException e) {
71        throw new BuilderException("Error resolving JdbcType. Cause: " + e, e);
72      }
73    }
74  
75    protected ResultSetType resolveResultSetType(String alias) {
76      try {
77        return alias == null ? null : ResultSetType.valueOf(alias);
78      } catch (IllegalArgumentException e) {
79        throw new BuilderException("Error resolving ResultSetType. Cause: " + e, e);
80      }
81    }
82  
83    protected ParameterMode resolveParameterMode(String alias) {
84      try {
85        return alias == null ? null : ParameterMode.valueOf(alias);
86      } catch (IllegalArgumentException e) {
87        throw new BuilderException("Error resolving ParameterMode. Cause: " + e, e);
88      }
89    }
90  
91    protected Object createInstance(String alias) {
92      Class<?> clazz = resolveClass(alias);
93      try {
94        return clazz == null ? null : clazz.getDeclaredConstructor().newInstance();
95      } catch (Exception e) {
96        throw new BuilderException("Error creating instance. Cause: " + e, e);
97      }
98    }
99  
100   protected <T> Class<? extends T> resolveClass(String alias) {
101     try {
102       return alias == null ? null : resolveAlias(alias);
103     } catch (Exception e) {
104       throw new BuilderException("Error resolving class. Cause: " + e, e);
105     }
106   }
107 
108   @Deprecated(since = "3.6.0", forRemoval = true)
109   protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, String typeHandlerAlias) {
110     return resolveTypeHandler(javaType, null, typeHandlerAlias);
111   }
112 
113   @Deprecated(since = "3.6.0", forRemoval = true)
114   protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends TypeHandler<?>> typeHandlerType) {
115     return resolveTypeHandler(javaType, null, typeHandlerType);
116   }
117 
118   protected TypeHandler<?> resolveTypeHandler(Type propertyType, JdbcType jdbcType, String typeHandlerAlias) {
119     Class<? extends TypeHandler<?>> typeHandlerType = null;
120     typeHandlerType = resolveClass(typeHandlerAlias);
121     if (typeHandlerType != null && !TypeHandler.class.isAssignableFrom(typeHandlerType)) {
122       throw new BuilderException("Type " + typeHandlerType.getName()
123           + " is not a valid TypeHandler because it does not implement TypeHandler interface");
124     }
125     return resolveTypeHandler(propertyType, jdbcType, typeHandlerType);
126   }
127 
128   protected TypeHandler<?> resolveTypeHandler(Type javaType, JdbcType jdbcType,
129       Class<? extends TypeHandler<?>> typeHandlerType) {
130     if (typeHandlerType == null && jdbcType == null) {
131       return null;
132     }
133     return configuration.getTypeHandlerRegistry().getTypeHandler(javaType, jdbcType, typeHandlerType);
134   }
135 
136   protected <T> Class<? extends T> resolveAlias(String alias) {
137     return typeAliasRegistry.resolveAlias(alias);
138   }
139 }