Skip to content

Allow to scan TypeHandler that pass Class<?> to constructor #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void setDatabaseIdProvider(DatabaseIdProvider databaseIdProvider) {

/**
* Gets the VFS.
*
*
* @return a specified VFS
*/
public Class<? extends VFS> getVfs() {
Expand All @@ -195,7 +195,7 @@ public Class<? extends VFS> getVfs() {

/**
* Sets the VFS.
*
*
* @param vfs
* a VFS
*/
Expand All @@ -205,7 +205,7 @@ public void setVfs(Class<? extends VFS> vfs) {

/**
* Gets the Cache.
*
*
* @return a specified Cache
*/
public Cache getCache() {
Expand All @@ -214,7 +214,7 @@ public Cache getCache() {

/**
* Sets the Cache.
*
*
* @param cache
* a Cache
*/
Expand Down Expand Up @@ -270,7 +270,7 @@ public void setTypeAliasesSuperType(Class<?> typeAliasesSuperType) {
*
* <p>
* Since 2.0.1, allow to specify a wildcard such as {@code com.example.*.typehandler}.
*
*
* @since 1.0.1
*
* @param typeHandlersPackage
Expand Down Expand Up @@ -331,7 +331,7 @@ public void setConfigLocation(Resource configLocation) {

/**
* Set a customized MyBatis configuration.
*
*
* @param configuration
* MyBatis configuration
* @since 1.3.0
Expand Down Expand Up @@ -535,7 +535,6 @@ protected SqlSessionFactory buildSqlSessionFactory() throws Exception {
if (hasLength(this.typeHandlersPackage)) {
scanClasses(this.typeHandlersPackage, TypeHandler.class).stream().filter(clazz -> !clazz.isAnonymousClass())
.filter(clazz -> !clazz.isInterface()).filter(clazz -> !Modifier.isAbstract(clazz.getModifiers()))
.filter(clazz -> ClassUtils.getConstructorIfAvailable(clazz) != null)
.forEach(targetConfiguration.getTypeHandlerRegistry()::register);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.core.jdk.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

@MappedTypes({ AtomicInteger.class, AtomicLong.class })
public class AtomicNumberTypeHandler implements TypeHandler<Number> {

public AtomicNumberTypeHandler(Class<?> type) {
}

@Override
public void setParameter(PreparedStatement ps, int i, Number parameter, JdbcType jdbcType) throws SQLException {
}

@Override
public Number getResult(ResultSet rs, String columnName) throws SQLException {
return null;
}

@Override
public Number getResult(CallableStatement cs, int columnIndex) throws SQLException {
return null;
}

@Override
public Number getResult(ResultSet rs, int columnIndex) throws SQLException {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.math.BigInteger;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.ibatis.cache.impl.PerpetualCache;
import org.apache.ibatis.io.JBoss6VFS;
Expand All @@ -43,6 +45,7 @@
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.junit.jupiter.api.Test;
import org.mybatis.core.jdk.type.AtomicNumberTypeHandler;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.mybatis.spring.type.DummyTypeAlias;
import org.mybatis.spring.type.DummyTypeHandler;
Expand Down Expand Up @@ -400,12 +403,14 @@ void testSearchATypeAliasPackageWithSamePackage() throws Exception {
@Test
void testSearchATypeHandlerPackage() throws Exception {
setupFactoryBean();
factoryBean.setTypeHandlersPackage("org.**.type");
factoryBean.setTypeHandlersPackage("org.mybatis.**.type");

TypeHandlerRegistry typeHandlerRegistry = factoryBean.getObject().getConfiguration().getTypeHandlerRegistry();
assertThat(typeHandlerRegistry.hasTypeHandler(BigInteger.class)).isTrue();
assertThat(typeHandlerRegistry.hasTypeHandler(BigDecimal.class)).isTrue();
assertThat(typeHandlerRegistry.getTypeHandler(UUID.class)).isInstanceOf(TypeHandlerFactory.InnerTypeHandler.class);
assertThat(typeHandlerRegistry.getTypeHandler(AtomicInteger.class)).isInstanceOf(AtomicNumberTypeHandler.class);
assertThat(typeHandlerRegistry.getTypeHandler(AtomicLong.class)).isInstanceOf(AtomicNumberTypeHandler.class);
}

@Test
Expand Down