Skip to content

Filter anonymous class and interface when package scan #363

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
Apr 12, 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
6 changes: 5 additions & 1 deletion src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,10 @@ protected SqlSessionFactory buildSqlSessionFactory() throws Exception {
Optional.ofNullable(this.vfs).ifPresent(targetConfiguration::setVfsImpl);

if (hasLength(this.typeAliasesPackage)) {
scanClasses(this.typeAliasesPackage, this.typeAliasesSuperType)
scanClasses(this.typeAliasesPackage, this.typeAliasesSuperType).stream()
.filter(clazz -> !clazz.isAnonymousClass())
.filter(clazz -> !clazz.isInterface())
.filter(clazz -> !clazz.isMemberClass())
.forEach(targetConfiguration.getTypeAliasRegistry()::registerAlias);
}

Expand All @@ -480,6 +483,7 @@ 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)
Expand Down
13 changes: 12 additions & 1 deletion src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Properties;
import java.util.UUID;

import org.apache.ibatis.cache.impl.PerpetualCache;
import org.apache.ibatis.io.JBoss6VFS;
Expand All @@ -42,6 +43,7 @@
import org.mybatis.spring.type.DummyTypeAlias;
import org.mybatis.spring.type.DummyTypeHandler;
import org.mybatis.spring.type.SuperType;
import org.mybatis.spring.type.TypeHandlerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

Expand Down Expand Up @@ -341,13 +343,20 @@ void testAddATypeAlias() throws Exception {
@Test
void testSearchATypeAliasPackage() throws Exception {
setupFactoryBean();
factoryBean.setTypeAliasesPackage("org.mybatis.spring.type");
factoryBean.setTypeAliasesPackage("org.mybatis.spring.type, org.mybatis.spring.scan");

TypeAliasRegistry typeAliasRegistry = factoryBean.getObject().getConfiguration().getTypeAliasRegistry();
System.out.println(typeAliasRegistry.getTypeAliases().keySet());
assertThat(typeAliasRegistry.getTypeAliases().size()).isEqualTo(81);
typeAliasRegistry.resolveAlias("testAlias");
typeAliasRegistry.resolveAlias("testAlias2");
typeAliasRegistry.resolveAlias("dummyTypeHandler");
typeAliasRegistry.resolveAlias("dummyTypeHandler2");
typeAliasRegistry.resolveAlias("superType");
typeAliasRegistry.resolveAlias("dummyMapperFactoryBean");
typeAliasRegistry.resolveAlias("scanclass1");
typeAliasRegistry.resolveAlias("scanclass2");
typeAliasRegistry.resolveAlias("scanenum");
}

@Test
Expand Down Expand Up @@ -384,6 +393,7 @@ void testSearchATypeHandlerPackage() throws Exception {
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);
}

@Test
Expand Down Expand Up @@ -444,4 +454,5 @@ private void assertConfig(SqlSessionFactory factory, String environment,
assertThat(factory.getConfiguration().getParameterMapNames().size()).isEqualTo(0);
assertThat(factory.getConfiguration().getSqlFragments().size()).isEqualTo(0);
}

}
19 changes: 19 additions & 0 deletions src/test/java/org/mybatis/spring/scan/ScanAnnotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.spring.scan;

public @interface ScanAnnotation {
}
51 changes: 51 additions & 0 deletions src/test/java/org/mybatis/spring/scan/ScanClass1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.spring.scan;

import java.util.function.Supplier;

import org.apache.ibatis.jdbc.SQL;

public class ScanClass1 {

public static class StaticInnerClass {

}

public class InnerClass {

}

public enum InnerEnum {

}

public @interface InnerAnnotation {

}

public String createSqlUsingAnonymousClass() {
return new SQL() {{
SELECT("a");
FROM("test1");
}}.toString();
}

public Supplier<String> createSqlSupplier() {
return () -> "SELECT a FROM test1";
}

}
51 changes: 51 additions & 0 deletions src/test/java/org/mybatis/spring/scan/ScanClass2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.spring.scan;

import java.util.function.Supplier;

import org.apache.ibatis.jdbc.SQL;

public class ScanClass2 {

public static class StaticInnerClass {

}

public class InnerClass {

}

public enum InnerEnum {

}

public @interface InnerAnnotation {

}

public String createSqlUsingAnonymousClass() {
return new SQL() {{
SELECT("a");
FROM("test2");
}}.toString();
}

public Supplier<String> createSqlSupplier() {
return () -> "SELECT a FROM test2";
}

}
19 changes: 19 additions & 0 deletions src/test/java/org/mybatis/spring/scan/ScanEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.spring.scan;

public enum ScanEnum {
}
19 changes: 19 additions & 0 deletions src/test/java/org/mybatis/spring/scan/ScanInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.spring.scan;

public interface ScanInterface {
}
16 changes: 16 additions & 0 deletions src/test/java/org/mybatis/spring/scan/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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.spring.scan;
81 changes: 81 additions & 0 deletions src/test/java/org/mybatis/spring/type/TypeHandlerFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* 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.spring.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.UUID;

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

public interface TypeHandlerFactory {

static TypeHandler<String> handler1() {
return new TypeHandler<String>() {
@Override
public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) {

}

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

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

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

static TypeHandler<UUID> handler2() {
return new InnerTypeHandler();
}

@MappedTypes({UUID.class})
class InnerTypeHandler implements TypeHandler<UUID> {

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

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

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

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

}

}
16 changes: 16 additions & 0 deletions src/test/java/org/mybatis/spring/type/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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.spring.type;