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