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.type;
17  
18  import java.sql.CallableStatement;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  import java.util.HashSet;
23  import java.util.Set;
24  import java.util.stream.Collectors;
25  
26  import org.apache.ibatis.executor.ExecutorException;
27  
28  public class ConflictedTypeHandler implements TypeHandler<Object> {
29  
30    private final Class<?> javaType;
31    private final JdbcType jdbcType;
32    private final Set<TypeHandler<?>> conflictedTypeHandlers = new HashSet<>();
33  
34    public ConflictedTypeHandler(Class<?> javaType, JdbcType jdbcType, TypeHandler<?> existing, TypeHandler<?> added) {
35      super();
36      this.javaType = javaType;
37      this.jdbcType = jdbcType;
38      if (existing instanceof ConflictedTypeHandler) {
39        conflictedTypeHandlers.addAll(((ConflictedTypeHandler) existing).getConflictedTypeHandlers());
40      } else {
41        conflictedTypeHandlers.add(existing);
42      }
43      conflictedTypeHandlers.add(added);
44    }
45  
46    private Set<TypeHandler<?>> getConflictedTypeHandlers() {
47      return conflictedTypeHandlers;
48    }
49  
50    @Override
51    public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
52      throw exception();
53    }
54  
55    @Override
56    public Object getResult(ResultSet rs, String columnName) throws SQLException {
57      throw exception();
58    }
59  
60    @Override
61    public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
62      throw exception();
63    }
64  
65    @Override
66    public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
67      throw exception();
68    }
69  
70    private ExecutorException exception() {
71      return new ExecutorException(
72          "Multiple type-aware handlers are registered and being looked up without type; javaType=" + javaType
73              + ", jdbcType=" + jdbcType + ", type handlers="
74              + conflictedTypeHandlers.stream().map(x -> x.getClass().getName()).collect(Collectors.joining(",")));
75    }
76  }