Skip to content

Commit 186ecbd

Browse files
committed
Initial light refactoring to remove some duplicated code
1 parent d9d5cb8 commit 186ecbd

File tree

9 files changed

+82
-134
lines changed

9 files changed

+82
-134
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 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.
@@ -65,7 +65,7 @@ default void generationStarted(int totalTasks) {}
6565
/**
6666
* Called to note the start of the file saving phase, and to note the
6767
* maximum number of startTask messages that will be sent for the file
68-
* saving phase phase.
68+
* saving phase.
6969
*
7070
* @param totalTasks
7171
* the maximum number of times startTask will be called for the
@@ -87,8 +87,8 @@ default void startTask(String taskName) {}
8787
default void done() {}
8888

8989
/**
90-
* The method is called periodically during a long running method.
91-
* If the the implementation throws <code>InterruptedException</code> then
90+
* The method is called periodically during a long-running method.
91+
* If the implementation throws <code>InterruptedException</code> then
9292
* the method will be canceled. Any files that have already been saved will
9393
* remain on the file system.
9494
*

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

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 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.
@@ -22,17 +22,16 @@
2222
import java.sql.SQLException;
2323
import java.util.ArrayList;
2424
import java.util.HashMap;
25-
import java.util.HashSet;
2625
import java.util.List;
2726
import java.util.Map;
2827
import java.util.Set;
29-
import java.util.StringTokenizer;
3028

3129
import org.mybatis.generator.config.Configuration;
3230
import org.mybatis.generator.config.xml.ConfigurationParser;
3331
import org.mybatis.generator.exception.InvalidConfigurationException;
3432
import org.mybatis.generator.exception.XMLParserException;
3533
import org.mybatis.generator.internal.DefaultShellCallback;
34+
import org.mybatis.generator.internal.util.StringUtility;
3635
import org.mybatis.generator.logging.LogFactory;
3736

3837
/**
@@ -79,42 +78,22 @@ public static void main(String[] args) {
7978
return;
8079
}
8180

82-
Set<String> fullyqualifiedTables = new HashSet<>();
83-
if (arguments.containsKey(TABLES)) {
84-
StringTokenizer st = new StringTokenizer(arguments.get(TABLES), ","); //$NON-NLS-1$
85-
while (st.hasMoreTokens()) {
86-
String s = st.nextToken().trim();
87-
if (s.length() > 0) {
88-
fullyqualifiedTables.add(s);
89-
}
90-
}
91-
}
81+
Set<String> fullyQualifiedTables = StringUtility.tokenize(arguments.get(TABLES));
9282

93-
Set<String> contexts = new HashSet<>();
94-
if (arguments.containsKey(CONTEXT_IDS)) {
95-
StringTokenizer st = new StringTokenizer(
96-
arguments.get(CONTEXT_IDS), ","); //$NON-NLS-1$
97-
while (st.hasMoreTokens()) {
98-
String s = st.nextToken().trim();
99-
if (s.length() > 0) {
100-
contexts.add(s);
101-
}
102-
}
103-
}
83+
Set<String> contexts = StringUtility.tokenize(arguments.get(CONTEXT_IDS));
10484

10585
try {
10686
ConfigurationParser cp = new ConfigurationParser(warnings);
10787
Configuration config = cp.parseConfiguration(configurationFile);
10888

109-
DefaultShellCallback shellCallback = new DefaultShellCallback(
110-
arguments.containsKey(OVERWRITE));
89+
DefaultShellCallback shellCallback = new DefaultShellCallback(arguments.containsKey(OVERWRITE));
11190

11291
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
11392

11493
ProgressCallback progressCallback = arguments.containsKey(VERBOSE) ? new VerboseProgressCallback()
11594
: null;
11695

117-
myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
96+
myBatisGenerator.generate(progressCallback, contexts, fullyQualifiedTables);
11897

11998
} catch (XMLParserException e) {
12099
writeLine(getString("Progress.3")); //$NON-NLS-1$

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 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.
@@ -33,17 +33,15 @@ public DefaultShellCallback(boolean overwrite) {
3333
}
3434

3535
@Override
36-
public File getDirectory(String targetProject, String targetPackage)
37-
throws ShellException {
38-
// targetProject is interpreted as a directory that must exist
36+
public File getDirectory(String targetProject, String targetPackage) throws ShellException {
37+
// targetProject is interpreted as a directory that must already exist
3938
//
40-
// targetPackage is interpreted as a sub directory, but in package
41-
// format (with dots instead of slashes). The sub directory will be
42-
// created
43-
// if it does not already exist
39+
// targetPackage is interpreted as a subdirectory, but in package
40+
// format (with dots instead of slashes). The subdirectory will be
41+
// created if it does not already exist
4442

45-
File project = new File(targetProject);
46-
if (!project.isDirectory()) {
43+
File targetProjectDirectory = new File(targetProject);
44+
if (!targetProjectDirectory.isDirectory()) {
4745
throw new ShellException(getString("Warning.9", //$NON-NLS-1$
4846
targetProject));
4947
}
@@ -55,7 +53,7 @@ public File getDirectory(String targetProject, String targetPackage)
5553
sb.append(File.separatorChar);
5654
}
5755

58-
File directory = new File(project, sb.toString());
56+
File directory = new File(targetProjectDirectory, sb.toString());
5957
if (!directory.isDirectory()) {
6058
boolean rc = directory.mkdirs();
6159
if (!rc) {

core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/util/StringUtility.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 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.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.mybatis.generator.internal.util;
1717

18+
import java.util.HashSet;
19+
import java.util.Set;
1820
import java.util.StringTokenizer;
1921

2022
public class StringUtility {
@@ -100,4 +102,26 @@ public static boolean stringContainsSQLWildcard(String s) {
100102

101103
return s.indexOf('%') != -1 || s.indexOf('_') != -1;
102104
}
105+
106+
/**
107+
* Given an input string, tokenize on the commas and trim all token.
108+
* Returns an empty set if the input string is null.
109+
*
110+
* @param in strong to tokenize.
111+
* @return Set of tokens
112+
*/
113+
public static Set<String> tokenize(String in) {
114+
Set<String> answer = new HashSet<>();
115+
if (StringUtility.stringHasValue(in)) {
116+
StringTokenizer st = new StringTokenizer(in, ","); //$NON-NLS-1$
117+
while (st.hasMoreTokens()) {
118+
String s = st.nextToken().trim();
119+
if (s.length() > 0) {
120+
answer.add(s);
121+
}
122+
}
123+
}
124+
return answer;
125+
}
126+
103127
}

core/mybatis-generator-core/src/site/xhtml/whatsNew.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-2022 the original author or authors.
4+
Copyright 2006-2023 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.
Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 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.
@@ -16,65 +16,43 @@
1616
package org.mybatis.generator.maven;
1717

1818
import java.io.File;
19-
import java.util.StringTokenizer;
2019

2120
import org.mybatis.generator.exception.ShellException;
2221
import org.mybatis.generator.internal.DefaultShellCallback;
23-
import org.mybatis.generator.internal.util.messages.Messages;
22+
23+
import static org.mybatis.generator.internal.util.messages.Messages.getString;
2424

2525
/**
2626
* Shell callback that calculates the Maven output directory.
2727
*
2828
* @author Jeff Butler
2929
*/
3030
public class MavenShellCallback extends DefaultShellCallback {
31-
private MyBatisGeneratorMojo mybatisGeneratorMojo;
31+
private final MyBatisGeneratorMojo mybatisGeneratorMojo;
3232

3333
public MavenShellCallback(MyBatisGeneratorMojo mybatisGeneratorMojo, boolean overwrite) {
3434
super(overwrite);
3535
this.mybatisGeneratorMojo = mybatisGeneratorMojo;
3636
}
3737

3838
@Override
39-
public File getDirectory(String targetProject, String targetPackage)
40-
throws ShellException {
41-
if (!"MAVEN".equals(targetProject)) {
42-
return super.getDirectory(targetProject, targetPackage);
43-
}
44-
45-
// targetProject is the output directory from the MyBatis generator
46-
// Mojo. It will be created if necessary
47-
//
48-
// targetPackage is interpreted as a sub directory, but in package
49-
// format (with dots instead of slashes). The sub directory will be created
50-
// if it does not already exist
51-
52-
File project = mybatisGeneratorMojo.getOutputDirectory();
53-
if (!project.exists()) {
54-
project.mkdirs();
55-
}
56-
57-
if (!project.isDirectory()) {
58-
throw new ShellException(Messages.getString("Warning.9", //$NON-NLS-1$
59-
project.getAbsolutePath()));
60-
}
61-
62-
StringBuilder sb = new StringBuilder();
63-
StringTokenizer st = new StringTokenizer(targetPackage, "."); //$NON-NLS-1$
64-
while (st.hasMoreTokens()) {
65-
sb.append(st.nextToken());
66-
sb.append(File.separatorChar);
67-
}
68-
69-
File directory = new File(project, sb.toString());
70-
if (!directory.isDirectory()) {
71-
boolean rc = directory.mkdirs();
72-
if (!rc) {
73-
throw new ShellException(Messages.getString("Warning.10", //$NON-NLS-1$
74-
directory.getAbsolutePath()));
39+
public File getDirectory(String targetProject, String targetPackage) throws ShellException {
40+
if ("MAVEN".equals(targetProject)) {
41+
// targetProject is the output directory from the MyBatis generator
42+
// Mojo. It will be created if necessary
43+
44+
File project = mybatisGeneratorMojo.getOutputDirectory();
45+
if (!project.exists()) {
46+
boolean rc = project.mkdirs();
47+
if (!rc) {
48+
throw new ShellException(getString("Warning.10", //$NON-NLS-1$
49+
project.getAbsolutePath()));
50+
}
7551
}
76-
}
7752

78-
return directory;
53+
return super.getDirectory(project.getAbsolutePath(), targetPackage);
54+
} else {
55+
return super.getDirectory(targetProject, targetPackage);
56+
}
7957
}
8058
}

core/mybatis-generator-maven-plugin/src/main/java/org/mybatis/generator/maven/MyBatisGeneratorMojo.java

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 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.
@@ -22,7 +22,6 @@
2222
import java.util.HashSet;
2323
import java.util.List;
2424
import java.util.Set;
25-
import java.util.StringTokenizer;
2625

2726
import org.apache.maven.artifact.DependencyResolutionRequiredException;
2827
import org.apache.maven.model.Resource;
@@ -44,6 +43,8 @@
4443
import org.mybatis.generator.internal.util.StringUtility;
4544
import org.mybatis.generator.internal.util.messages.Messages;
4645
import org.mybatis.generator.logging.LogFactory;
46+
import org.slf4j.Logger;
47+
import org.slf4j.LoggerFactory;
4748

4849
/**
4950
* Goal which generates MyBatis artifacts.
@@ -181,69 +182,38 @@ public void execute() throws MojoExecutionException {
181182
ObjectFactory.addExternalClassLoader(cl);
182183

183184
if (configurationFile == null) {
184-
throw new MojoExecutionException(
185-
Messages.getString("RuntimeError.0")); //$NON-NLS-1$
185+
throw new MojoExecutionException(Messages.getString("RuntimeError.0")); //$NON-NLS-1$
186186
}
187187

188-
List<String> warnings = new ArrayList<>();
189-
190188
if (!configurationFile.exists()) {
191-
throw new MojoExecutionException(Messages.getString(
192-
"RuntimeError.1", configurationFile.toString())); //$NON-NLS-1$
189+
throw new MojoExecutionException(Messages.getString("RuntimeError.1", configurationFile.toString())); //$NON-NLS-1$
193190
}
194191

195192
runScriptIfNecessary();
196193

197-
Set<String> fullyqualifiedTables = new HashSet<>();
198-
if (StringUtility.stringHasValue(tableNames)) {
199-
StringTokenizer st = new StringTokenizer(tableNames, ","); //$NON-NLS-1$
200-
while (st.hasMoreTokens()) {
201-
String s = st.nextToken().trim();
202-
if (s.length() > 0) {
203-
fullyqualifiedTables.add(s);
204-
}
205-
}
206-
}
194+
Set<String> fullyQualifiedTables = StringUtility.tokenize(tableNames);
207195

208-
Set<String> contextsToRun = new HashSet<>();
209-
if (StringUtility.stringHasValue(contexts)) {
210-
StringTokenizer st = new StringTokenizer(contexts, ","); //$NON-NLS-1$
211-
while (st.hasMoreTokens()) {
212-
String s = st.nextToken().trim();
213-
if (s.length() > 0) {
214-
contextsToRun.add(s);
215-
}
216-
}
217-
}
196+
Set<String> contextsToRun = StringUtility.tokenize(contexts);
197+
198+
List<String> warnings = new ArrayList<>();
218199

219200
try {
220-
ConfigurationParser cp = new ConfigurationParser(
221-
project.getProperties(), warnings);
201+
ConfigurationParser cp = new ConfigurationParser(project.getProperties(), warnings);
222202
Configuration config = cp.parseConfiguration(configurationFile);
223203

224204
ShellCallback callback = new MavenShellCallback(this, overwrite);
225205

226-
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
227-
callback, warnings);
206+
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
228207

229-
myBatisGenerator.generate(new MavenProgressCallback(getLog(),
230-
verbose), contextsToRun, fullyqualifiedTables);
208+
myBatisGenerator.generate(new MavenProgressCallback(getLog(), verbose), contextsToRun, fullyQualifiedTables);
231209

232-
} catch (XMLParserException e) {
210+
} catch (XMLParserException | InvalidConfigurationException e) {
233211
for (String error : e.getErrors()) {
234212
getLog().error(error);
235213
}
236214

237215
throw new MojoExecutionException(e.getMessage());
238-
} catch (SQLException e) {
239-
throw new MojoExecutionException(e.getMessage());
240-
} catch (IOException e) {
241-
throw new MojoExecutionException(e.getMessage());
242-
} catch (InvalidConfigurationException e) {
243-
for (String error : e.getErrors()) {
244-
getLog().error(error);
245-
}
246-
216+
} catch (SQLException | IOException e) {
247217
throw new MojoExecutionException(e.getMessage());
248218
} catch (InterruptedException e) {
249219
// ignore (will never happen with the DefaultShellCallback)
@@ -253,8 +223,7 @@ public void execute() throws MojoExecutionException {
253223
getLog().warn(error);
254224
}
255225

256-
if (project != null && outputDirectory != null
257-
&& outputDirectory.exists()) {
226+
if (project != null && outputDirectory != null && outputDirectory.exists()) {
258227
project.addCompileSourceRoot(outputDirectory.getAbsolutePath());
259228

260229
Resource resource = new Resource();

0 commit comments

Comments
 (0)