Skip to content

Commit

Permalink
Performed Refactoring for design smells and code smells in the repo
Browse files Browse the repository at this point in the history
  • Loading branch information
nav2508 committed Nov 28, 2024
1 parent 03503fc commit b4c3699
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 48 deletions.
42 changes: 3 additions & 39 deletions src/main/java/org/apache/ibatis/binding/MapperRegistry.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 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.
Expand All @@ -18,11 +18,9 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;

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

/**
* Gets the mappers.
*
* @return the mappers
*
* @since 3.2.2
*/
public Collection<Class<?>> getMappers() {
return Collections.unmodifiableCollection(knownMappers.keySet());
}

/**
* Adds the mappers.
*
* @param packageName
* the package name
* @param superType
* the super type
*
* @since 3.2.2
*/
public void addMappers(String packageName, Class<?> superType) {
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
for (Class<?> mapperClass : mapperSet) {
addMapper(mapperClass);
}
MapperRegistryHelper.addMappers(this, packageName, superType);
}

/**
* Adds the mappers.
*
* @param packageName
* the package name
*
* @since 3.2.2
*/
public void addMappers(String packageName) {
addMappers(packageName, Object.class);
MapperRegistryHelper.addMappers(this, packageName);
}

}
35 changes: 35 additions & 0 deletions src/main/java/org/apache/ibatis/binding/MapperRegistryHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2009-2024 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
*
* https://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.apache.ibatis.binding;

import java.util.Set;

import org.apache.ibatis.io.ResolverUtil;

public class MapperRegistryHelper {
public static void addMappers(MapperRegistry registry, String packageName, Class<?> superType) {
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
for (Class<?> mapperClass : mapperSet) {
registry.addMapper(mapperClass);
}
}

public static void addMappers(MapperRegistry registry, String packageName) {
addMappers(registry, packageName, Object.class);
}
}
9 changes: 8 additions & 1 deletion src/main/java/org/apache/ibatis/mapping/ResultMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 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.
Expand Down Expand Up @@ -206,6 +206,13 @@ private List<String> getArgNames(Constructor<?> constructor) {
}
}

public static void validateNestedQueryAndResultMap(ResultMapping resultMapping) {
if (resultMapping.getNestedQueryId() != null && resultMapping.getNestedResultMapId() != null) {
throw new IllegalStateException(
"Cannot define both nestedQueryId and nestedResultMapId in property " + resultMapping.getProperty());
}
}

public String getId() {
return id;
}
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/org/apache/ibatis/mapping/ResultMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.apache.ibatis.mapping;

import static org.apache.ibatis.mapping.ResultMap.validateNestedQueryAndResultMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -142,19 +144,12 @@ public ResultMapping build() {
}

private void validate() {
validateNestedQueryAndResultMap();
validateNestedQueryAndResultMap(resultMapping);
validateTypeHandler();
validateColumn();
validateResultSet();
}

private void validateNestedQueryAndResultMap() {
if (resultMapping.nestedQueryId != null && resultMapping.nestedResultMapId != null) {
throw new IllegalStateException(
"Cannot define both nestedQueryId and nestedResultMapId in property " + resultMapping.property);
}
}

private void validateTypeHandler() {
if (resultMapping.nestedQueryId == null && resultMapping.nestedResultMapId == null
&& resultMapping.typeHandler == null) {
Expand Down

0 comments on commit b4c3699

Please sign in to comment.