BaseBuilder.java

  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. import java.util.Arrays;
  18. import java.util.HashSet;
  19. import java.util.Set;
  20. import java.util.regex.Pattern;

  21. import org.apache.ibatis.mapping.ParameterMode;
  22. import org.apache.ibatis.mapping.ResultSetType;
  23. import org.apache.ibatis.session.Configuration;
  24. import org.apache.ibatis.type.JdbcType;
  25. import org.apache.ibatis.type.TypeAliasRegistry;
  26. import org.apache.ibatis.type.TypeHandler;
  27. import org.apache.ibatis.type.TypeHandlerRegistry;

  28. /**
  29.  * @author Clinton Begin
  30.  */
  31. public abstract class BaseBuilder {
  32.   protected final Configuration configuration;
  33.   protected final TypeAliasRegistry typeAliasRegistry;
  34.   protected final TypeHandlerRegistry typeHandlerRegistry;

  35.   public BaseBuilder(Configuration configuration) {
  36.     this.configuration = configuration;
  37.     this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
  38.     this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
  39.   }

  40.   public Configuration getConfiguration() {
  41.     return configuration;
  42.   }

  43.   protected Pattern parseExpression(String regex, String defaultValue) {
  44.     return Pattern.compile(regex == null ? defaultValue : regex);
  45.   }

  46.   protected Boolean booleanValueOf(String value, Boolean defaultValue) {
  47.     return value == null ? defaultValue : Boolean.valueOf(value);
  48.   }

  49.   protected Integer integerValueOf(String value, Integer defaultValue) {
  50.     return value == null ? defaultValue : Integer.valueOf(value);
  51.   }

  52.   protected Set<String> stringSetValueOf(String value, String defaultValue) {
  53.     value = value == null ? defaultValue : value;
  54.     return new HashSet<>(Arrays.asList(value.split(",")));
  55.   }

  56.   protected JdbcType resolveJdbcType(String alias) {
  57.     try {
  58.       return alias == null ? null : JdbcType.valueOf(alias);
  59.     } catch (IllegalArgumentException e) {
  60.       throw new BuilderException("Error resolving JdbcType. Cause: " + e, e);
  61.     }
  62.   }

  63.   protected ResultSetType resolveResultSetType(String alias) {
  64.     try {
  65.       return alias == null ? null : ResultSetType.valueOf(alias);
  66.     } catch (IllegalArgumentException e) {
  67.       throw new BuilderException("Error resolving ResultSetType. Cause: " + e, e);
  68.     }
  69.   }

  70.   protected ParameterMode resolveParameterMode(String alias) {
  71.     try {
  72.       return alias == null ? null : ParameterMode.valueOf(alias);
  73.     } catch (IllegalArgumentException e) {
  74.       throw new BuilderException("Error resolving ParameterMode. Cause: " + e, e);
  75.     }
  76.   }

  77.   protected Object createInstance(String alias) {
  78.     Class<?> clazz = resolveClass(alias);
  79.     try {
  80.       return clazz == null ? null : clazz.getDeclaredConstructor().newInstance();
  81.     } catch (Exception e) {
  82.       throw new BuilderException("Error creating instance. Cause: " + e, e);
  83.     }
  84.   }

  85.   protected <T> Class<? extends T> resolveClass(String alias) {
  86.     try {
  87.       return alias == null ? null : resolveAlias(alias);
  88.     } catch (Exception e) {
  89.       throw new BuilderException("Error resolving class. Cause: " + e, e);
  90.     }
  91.   }

  92.   protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, String typeHandlerAlias) {
  93.     if (typeHandlerAlias == null) {
  94.       return null;
  95.     }
  96.     Class<?> type = resolveClass(typeHandlerAlias);
  97.     if (type != null && !TypeHandler.class.isAssignableFrom(type)) {
  98.       throw new BuilderException(
  99.           "Type " + type.getName() + " is not a valid TypeHandler because it does not implement TypeHandler interface");
  100.     }
  101.     @SuppressWarnings("unchecked") // already verified it is a TypeHandler
  102.     Class<? extends TypeHandler<?>> typeHandlerType = (Class<? extends TypeHandler<?>>) type;
  103.     return resolveTypeHandler(javaType, typeHandlerType);
  104.   }

  105.   protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends TypeHandler<?>> typeHandlerType) {
  106.     if (typeHandlerType == null) {
  107.       return null;
  108.     }
  109.     // javaType ignored for injected handlers see issue #746 for full detail
  110.     TypeHandler<?> handler = typeHandlerRegistry.getMappingTypeHandler(typeHandlerType);
  111.     // if handler not in registry, create a new one, otherwise return directly
  112.     return handler == null ? typeHandlerRegistry.getInstance(javaType, typeHandlerType) : handler;
  113.   }

  114.   protected <T> Class<? extends T> resolveAlias(String alias) {
  115.     return typeAliasRegistry.resolveAlias(alias);
  116.   }
  117. }