Skip to content

Commit f9af086

Browse files
authored
Merge pull request mybatis#363 from kazuki43zoo/mybatisgh-362
Filter anonymous class and interface when package scan
2 parents 1a3e2e4 + b2e086f commit f9af086

10 files changed

+289
-2
lines changed

src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,10 @@ protected SqlSessionFactory buildSqlSessionFactory() throws Exception {
460460
Optional.ofNullable(this.vfs).ifPresent(targetConfiguration::setVfsImpl);
461461

462462
if (hasLength(this.typeAliasesPackage)) {
463-
scanClasses(this.typeAliasesPackage, this.typeAliasesSuperType)
463+
scanClasses(this.typeAliasesPackage, this.typeAliasesSuperType).stream()
464+
.filter(clazz -> !clazz.isAnonymousClass())
465+
.filter(clazz -> !clazz.isInterface())
466+
.filter(clazz -> !clazz.isMemberClass())
464467
.forEach(targetConfiguration.getTypeAliasRegistry()::registerAlias);
465468
}
466469

@@ -480,6 +483,7 @@ protected SqlSessionFactory buildSqlSessionFactory() throws Exception {
480483

481484
if (hasLength(this.typeHandlersPackage)) {
482485
scanClasses(this.typeHandlersPackage, TypeHandler.class).stream()
486+
.filter(clazz -> !clazz.isAnonymousClass())
483487
.filter(clazz -> !clazz.isInterface())
484488
.filter(clazz -> !Modifier.isAbstract(clazz.getModifiers()))
485489
.filter(clazz -> ClassUtils.getConstructorIfAvailable(clazz) != null)

src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.math.BigDecimal;
2222
import java.math.BigInteger;
2323
import java.util.Properties;
24+
import java.util.UUID;
2425

2526
import org.apache.ibatis.cache.impl.PerpetualCache;
2627
import org.apache.ibatis.io.JBoss6VFS;
@@ -42,6 +43,7 @@
4243
import org.mybatis.spring.type.DummyTypeAlias;
4344
import org.mybatis.spring.type.DummyTypeHandler;
4445
import org.mybatis.spring.type.SuperType;
46+
import org.mybatis.spring.type.TypeHandlerFactory;
4547
import org.springframework.core.io.ClassPathResource;
4648
import org.springframework.core.io.Resource;
4749

@@ -341,13 +343,20 @@ void testAddATypeAlias() throws Exception {
341343
@Test
342344
void testSearchATypeAliasPackage() throws Exception {
343345
setupFactoryBean();
344-
factoryBean.setTypeAliasesPackage("org.mybatis.spring.type");
346+
factoryBean.setTypeAliasesPackage("org.mybatis.spring.type, org.mybatis.spring.scan");
345347

346348
TypeAliasRegistry typeAliasRegistry = factoryBean.getObject().getConfiguration().getTypeAliasRegistry();
349+
System.out.println(typeAliasRegistry.getTypeAliases().keySet());
350+
assertThat(typeAliasRegistry.getTypeAliases().size()).isEqualTo(81);
347351
typeAliasRegistry.resolveAlias("testAlias");
348352
typeAliasRegistry.resolveAlias("testAlias2");
349353
typeAliasRegistry.resolveAlias("dummyTypeHandler");
354+
typeAliasRegistry.resolveAlias("dummyTypeHandler2");
350355
typeAliasRegistry.resolveAlias("superType");
356+
typeAliasRegistry.resolveAlias("dummyMapperFactoryBean");
357+
typeAliasRegistry.resolveAlias("scanclass1");
358+
typeAliasRegistry.resolveAlias("scanclass2");
359+
typeAliasRegistry.resolveAlias("scanenum");
351360
}
352361

353362
@Test
@@ -384,6 +393,7 @@ void testSearchATypeHandlerPackage() throws Exception {
384393
TypeHandlerRegistry typeHandlerRegistry = factoryBean.getObject().getConfiguration().getTypeHandlerRegistry();
385394
assertThat(typeHandlerRegistry.hasTypeHandler(BigInteger.class)).isTrue();
386395
assertThat(typeHandlerRegistry.hasTypeHandler(BigDecimal.class)).isTrue();
396+
assertThat(typeHandlerRegistry.getTypeHandler(UUID.class)).isInstanceOf(TypeHandlerFactory.InnerTypeHandler.class);
387397
}
388398

389399
@Test
@@ -444,4 +454,5 @@ private void assertConfig(SqlSessionFactory factory, String environment,
444454
assertThat(factory.getConfiguration().getParameterMapNames().size()).isEqualTo(0);
445455
assertThat(factory.getConfiguration().getSqlFragments().size()).isEqualTo(0);
446456
}
457+
447458
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2010-2019 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+
* http://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.mybatis.spring.scan;
17+
18+
public @interface ScanAnnotation {
19+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2010-2019 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+
* http://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.mybatis.spring.scan;
17+
18+
import java.util.function.Supplier;
19+
20+
import org.apache.ibatis.jdbc.SQL;
21+
22+
public class ScanClass1 {
23+
24+
public static class StaticInnerClass {
25+
26+
}
27+
28+
public class InnerClass {
29+
30+
}
31+
32+
public enum InnerEnum {
33+
34+
}
35+
36+
public @interface InnerAnnotation {
37+
38+
}
39+
40+
public String createSqlUsingAnonymousClass() {
41+
return new SQL() {{
42+
SELECT("a");
43+
FROM("test1");
44+
}}.toString();
45+
}
46+
47+
public Supplier<String> createSqlSupplier() {
48+
return () -> "SELECT a FROM test1";
49+
}
50+
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2010-2019 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+
* http://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.mybatis.spring.scan;
17+
18+
import java.util.function.Supplier;
19+
20+
import org.apache.ibatis.jdbc.SQL;
21+
22+
public class ScanClass2 {
23+
24+
public static class StaticInnerClass {
25+
26+
}
27+
28+
public class InnerClass {
29+
30+
}
31+
32+
public enum InnerEnum {
33+
34+
}
35+
36+
public @interface InnerAnnotation {
37+
38+
}
39+
40+
public String createSqlUsingAnonymousClass() {
41+
return new SQL() {{
42+
SELECT("a");
43+
FROM("test2");
44+
}}.toString();
45+
}
46+
47+
public Supplier<String> createSqlSupplier() {
48+
return () -> "SELECT a FROM test2";
49+
}
50+
51+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2010-2019 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+
* http://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.mybatis.spring.scan;
17+
18+
public enum ScanEnum {
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2010-2019 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+
* http://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.mybatis.spring.scan;
17+
18+
public interface ScanInterface {
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright 2010-2019 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+
* http://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.mybatis.spring.scan;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright 2010-2019 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+
* http://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.mybatis.spring.type;
17+
18+
import java.sql.CallableStatement;
19+
import java.sql.PreparedStatement;
20+
import java.sql.ResultSet;
21+
import java.util.UUID;
22+
23+
import org.apache.ibatis.type.JdbcType;
24+
import org.apache.ibatis.type.MappedTypes;
25+
import org.apache.ibatis.type.TypeHandler;
26+
27+
public interface TypeHandlerFactory {
28+
29+
static TypeHandler<String> handler1() {
30+
return new TypeHandler<String>() {
31+
@Override
32+
public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) {
33+
34+
}
35+
36+
@Override
37+
public String getResult(ResultSet rs, String columnName) {
38+
return null;
39+
}
40+
41+
@Override
42+
public String getResult(ResultSet rs, int columnIndex) {
43+
return null;
44+
}
45+
46+
@Override
47+
public String getResult(CallableStatement cs, int columnIndex) {
48+
return null;
49+
}
50+
};
51+
}
52+
53+
static TypeHandler<UUID> handler2() {
54+
return new InnerTypeHandler();
55+
}
56+
57+
@MappedTypes({UUID.class})
58+
class InnerTypeHandler implements TypeHandler<UUID> {
59+
60+
@Override
61+
public void setParameter(PreparedStatement ps, int i, UUID parameter, JdbcType jdbcType) {
62+
}
63+
64+
@Override
65+
public UUID getResult(ResultSet rs, String columnName) {
66+
return null;
67+
}
68+
69+
@Override
70+
public UUID getResult(ResultSet rs, int columnIndex) {
71+
return null;
72+
}
73+
74+
@Override
75+
public UUID getResult(CallableStatement cs, int columnIndex) {
76+
return null;
77+
}
78+
79+
}
80+
81+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright 2010-2019 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+
* http://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.mybatis.spring.type;

0 commit comments

Comments
 (0)