Skip to content

Commit b4c3699

Browse files
committed
Performed Refactoring for design smells and code smells in the repo
1 parent 03503fc commit b4c3699

File tree

4 files changed

+49
-48
lines changed

4 files changed

+49
-48
lines changed

src/main/java/org/apache/ibatis/binding/MapperRegistry.java

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,11 +18,9 @@
1818
import java.util.Collection;
1919
import java.util.Collections;
2020
import java.util.Map;
21-
import java.util.Set;
2221
import java.util.concurrent.ConcurrentHashMap;
2322

2423
import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder;
25-
import org.apache.ibatis.io.ResolverUtil;
2624
import org.apache.ibatis.session.Configuration;
2725
import org.apache.ibatis.session.SqlSession;
2826

@@ -65,9 +63,6 @@ public <T> void addMapper(Class<T> type) {
6563
boolean loadCompleted = false;
6664
try {
6765
knownMappers.put(type, new MapperProxyFactory<>(type));
68-
// It's important that the type is added before the parser is run
69-
// otherwise the binding may automatically be attempted by the
70-
// mapper parser. If the type is already known, it won't try.
7166
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
7267
parser.parse();
7368
loadCompleted = true;
@@ -79,46 +74,15 @@ public <T> void addMapper(Class<T> type) {
7974
}
8075
}
8176

82-
/**
83-
* Gets the mappers.
84-
*
85-
* @return the mappers
86-
*
87-
* @since 3.2.2
88-
*/
8977
public Collection<Class<?>> getMappers() {
9078
return Collections.unmodifiableCollection(knownMappers.keySet());
9179
}
9280

93-
/**
94-
* Adds the mappers.
95-
*
96-
* @param packageName
97-
* the package name
98-
* @param superType
99-
* the super type
100-
*
101-
* @since 3.2.2
102-
*/
10381
public void addMappers(String packageName, Class<?> superType) {
104-
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
105-
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
106-
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
107-
for (Class<?> mapperClass : mapperSet) {
108-
addMapper(mapperClass);
109-
}
82+
MapperRegistryHelper.addMappers(this, packageName, superType);
11083
}
11184

112-
/**
113-
* Adds the mappers.
114-
*
115-
* @param packageName
116-
* the package name
117-
*
118-
* @since 3.2.2
119-
*/
12085
public void addMappers(String packageName) {
121-
addMappers(packageName, Object.class);
86+
MapperRegistryHelper.addMappers(this, packageName);
12287
}
123-
12488
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2009-2024 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.binding;
17+
18+
import java.util.Set;
19+
20+
import org.apache.ibatis.io.ResolverUtil;
21+
22+
public class MapperRegistryHelper {
23+
public static void addMappers(MapperRegistry registry, String packageName, Class<?> superType) {
24+
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
25+
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
26+
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
27+
for (Class<?> mapperClass : mapperSet) {
28+
registry.addMapper(mapperClass);
29+
}
30+
}
31+
32+
public static void addMappers(MapperRegistry registry, String packageName) {
33+
addMappers(registry, packageName, Object.class);
34+
}
35+
}

src/main/java/org/apache/ibatis/mapping/ResultMap.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -206,6 +206,13 @@ private List<String> getArgNames(Constructor<?> constructor) {
206206
}
207207
}
208208

209+
public static void validateNestedQueryAndResultMap(ResultMapping resultMapping) {
210+
if (resultMapping.getNestedQueryId() != null && resultMapping.getNestedResultMapId() != null) {
211+
throw new IllegalStateException(
212+
"Cannot define both nestedQueryId and nestedResultMapId in property " + resultMapping.getProperty());
213+
}
214+
}
215+
209216
public String getId() {
210217
return id;
211218
}

src/main/java/org/apache/ibatis/mapping/ResultMapping.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.apache.ibatis.mapping;
1717

18+
import static org.apache.ibatis.mapping.ResultMap.validateNestedQueryAndResultMap;
19+
1820
import java.util.ArrayList;
1921
import java.util.Collections;
2022
import java.util.List;
@@ -142,19 +144,12 @@ public ResultMapping build() {
142144
}
143145

144146
private void validate() {
145-
validateNestedQueryAndResultMap();
147+
validateNestedQueryAndResultMap(resultMapping);
146148
validateTypeHandler();
147149
validateColumn();
148150
validateResultSet();
149151
}
150152

151-
private void validateNestedQueryAndResultMap() {
152-
if (resultMapping.nestedQueryId != null && resultMapping.nestedResultMapId != null) {
153-
throw new IllegalStateException(
154-
"Cannot define both nestedQueryId and nestedResultMapId in property " + resultMapping.property);
155-
}
156-
}
157-
158153
private void validateTypeHandler() {
159154
if (resultMapping.nestedQueryId == null && resultMapping.nestedResultMapId == null
160155
&& resultMapping.typeHandler == null) {

0 commit comments

Comments
 (0)