Skip to content

Commit e5d4d4c

Browse files
authored
Merge pull request #1431 from jeffgbutler/java-merging
Refactor the Mergers and Associated Tests
2 parents 8ce80c6 + 213e3cc commit e5d4d4c

27 files changed

+933
-601
lines changed

core/mybatis-generator-core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@
100100
<artifactId>kotlin-stdlib</artifactId>
101101
<scope>test</scope>
102102
</dependency>
103+
<dependency>
104+
<groupId>org.reflections</groupId>
105+
<artifactId>reflections</artifactId>
106+
<scope>test</scope>
107+
</dependency>
103108
</dependencies>
104109

105110
<build>

core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/MyBatisGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import org.mybatis.generator.exception.ShellException;
5050
import org.mybatis.generator.internal.DefaultShellCallback;
5151
import org.mybatis.generator.internal.ObjectFactory;
52-
import org.mybatis.generator.internal.XmlFileMergerJaxp;
52+
import org.mybatis.generator.merge.xml.XmlFileMergerJaxp;
5353

5454
/**
5555
* This class is the main interface to MyBatis generator. A typical execution of the tool involves these steps:

core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/JavaMergingShellCallback.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.jspecify.annotations.Nullable;
2121
import org.mybatis.generator.exception.ShellException;
22+
import org.mybatis.generator.merge.java.JavaFileMerger;
2223

2324
public class JavaMergingShellCallback extends DefaultShellCallback {
2425

core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/JavaFileMerger.java renamed to core/mybatis-generator-core/src/main/java/org/mybatis/generator/merge/java/JavaFileMerger.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.mybatis.generator.internal;
16+
package org.mybatis.generator.merge.java;
1717

1818
import static org.mybatis.generator.internal.util.messages.Messages.getString;
1919

@@ -96,7 +96,8 @@ public static String getMergedSource(String newFileSource, String existingFileCo
9696
CompilationUnit existingCompilationUnit = existingParseResult.getResult().orElseThrow();
9797

9898
// Perform the merge
99-
CompilationUnit mergedCompilationUnit = performMerge(newCompilationUnit, existingCompilationUnit, javadocTags);
99+
CompilationUnit mergedCompilationUnit =
100+
performMerge(newCompilationUnit, existingCompilationUnit, javadocTags);
100101

101102
return mergedCompilationUnit.toString();
102103
} catch (Exception e) {
@@ -127,9 +128,9 @@ private static boolean hasGeneratedAnnotation(BodyDeclaration<?> member) {
127128
for (AnnotationExpr annotation : member.getAnnotations()) {
128129
String annotationName = annotation.getNameAsString();
129130
// Check for @Generated annotation (both javax and jakarta packages)
130-
if ("Generated".equals(annotationName) ||
131-
"javax.annotation.Generated".equals(annotationName) ||
132-
"jakarta.annotation.Generated".equals(annotationName)) {
131+
if ("Generated".equals(annotationName)
132+
|| "javax.annotation.Generated".equals(annotationName)
133+
|| "jakarta.annotation.Generated".equals(annotationName)) {
133134
return true;
134135
}
135136
}
@@ -177,12 +178,14 @@ public int compareTo(ImportInfo other) {
177178

178179
// Add imports from new file
179180
for (ImportDeclaration importDecl : mergedCompilationUnit.getImports()) {
180-
allImports.add(new ImportInfo(importDecl.getNameAsString(), importDecl.isStatic(), importDecl.isAsterisk()));
181+
allImports.add(
182+
new ImportInfo(importDecl.getNameAsString(), importDecl.isStatic(), importDecl.isAsterisk()));
181183
}
182184

183185
// Add imports from existing file (avoiding duplicates)
184186
for (ImportDeclaration importDecl : existingCompilationUnit.getImports()) {
185-
allImports.add(new ImportInfo(importDecl.getNameAsString(), importDecl.isStatic(), importDecl.isAsterisk()));
187+
allImports.add(
188+
new ImportInfo(importDecl.getNameAsString(), importDecl.isStatic(), importDecl.isAsterisk()));
186189
}
187190

188191
// Clear existing imports and add sorted imports
@@ -207,13 +210,14 @@ private static int getImportPriority(String importName) {
207210
}
208211
}
209212

210-
private static void addPreservedElements(CompilationUnit existingCompilationUnit, CompilationUnit mergedCompilationUnit, String[] javadocTags) {
213+
private static void addPreservedElements(CompilationUnit existingCompilationUnit,
214+
CompilationUnit mergedCompilationUnit, String[] javadocTags) {
211215
// Find the main type declarations
212216
TypeDeclaration<?> existingTypeDeclaration = findMainTypeDeclaration(existingCompilationUnit);
213217
TypeDeclaration<?> mergedTypeDeclaration = findMainTypeDeclaration(mergedCompilationUnit);
214218

215-
if (existingTypeDeclaration instanceof ClassOrInterfaceDeclaration existingClassDeclaration &&
216-
mergedTypeDeclaration instanceof ClassOrInterfaceDeclaration mergedClassDeclaration) {
219+
if (existingTypeDeclaration instanceof ClassOrInterfaceDeclaration existingClassDeclaration
220+
&& mergedTypeDeclaration instanceof ClassOrInterfaceDeclaration mergedClassDeclaration) {
217221

218222
// Add only non-generated members from the existing class to the end of merged class
219223
for (BodyDeclaration<?> member : existingClassDeclaration.getMembers()) {
@@ -224,7 +228,7 @@ private static void addPreservedElements(CompilationUnit existingCompilationUnit
224228
}
225229
}
226230

227-
private static TypeDeclaration<?> findMainTypeDeclaration(CompilationUnit compilationUnit) {
231+
private static @Nullable TypeDeclaration<?> findMainTypeDeclaration(CompilationUnit compilationUnit) {
228232
// Return the first public type declaration, or the first type declaration if no public one exists
229233
TypeDeclaration<?> firstType = null;
230234
for (TypeDeclaration<?> typeDeclaration : compilationUnit.getTypes()) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2006-2026 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+
@NullMarked
17+
package org.mybatis.generator.merge.java;
18+
19+
import org.jspecify.annotations.NullMarked;

core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/DomWriter.java renamed to core/mybatis-generator-core/src/main/java/org/mybatis/generator/merge/xml/DomWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.mybatis.generator.internal;
16+
package org.mybatis.generator.merge.xml;
1717

1818
import static org.mybatis.generator.internal.util.messages.Messages.getString;
1919

core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/XmlFileMergerJaxp.java renamed to core/mybatis-generator-core/src/main/java/org/mybatis/generator/merge/xml/XmlFileMergerJaxp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.mybatis.generator.internal;
16+
package org.mybatis.generator.merge.xml;
1717

1818
import static org.mybatis.generator.internal.util.messages.Messages.getString;
1919

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2006-2026 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+
@NullMarked
17+
package org.mybatis.generator.merge.xml;
18+
19+
import org.jspecify.annotations.NullMarked;

core/mybatis-generator-core/src/site/xhtml/running/runningWithAnt.xhtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2006-2025 the original author or authors.
4+
Copyright 2006-2026 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

core/mybatis-generator-core/src/site/xhtml/running/runningWithMaven.xhtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2006-2025 the original author or authors.
4+
Copyright 2006-2026 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)