Skip to content

Commit 4f652a7

Browse files
[ARCHETYPE-678] Code cleanups
1 parent 1e32a01 commit 4f652a7

File tree

42 files changed

+161
-301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+161
-301
lines changed

archetype-common/src/main/java/org/apache/maven/archetype/DefaultArchetypeManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
import java.io.File;
2626
import java.io.FileInputStream;
27-
import java.io.FileOutputStream;
2827
import java.io.IOException;
28+
import java.nio.file.Files;
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.zip.ZipEntry;
@@ -95,7 +95,7 @@ public void zip(File sourceDirectory, File archive) throws IOException {
9595
getLogger().warn("Could not create new file \"" + archive.getPath() + "\" or the file already exists.");
9696
}
9797

98-
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(archive))) {
98+
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(archive.toPath()))) {
9999
zos.setLevel(9);
100100

101101
zipper(zos, sourceDirectory.getAbsolutePath().length(), sourceDirectory);

archetype-common/src/main/java/org/apache/maven/archetype/common/ArchetypeArtifactManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.eclipse.aether.repository.RemoteRepository;
3232

3333
public interface ArchetypeArtifactManager {
34-
String ROLE = ArchetypeArtifactManager.class.getName();
3534

3635
Model getArchetypePom(File jar) throws XmlPullParserException, UnknownArchetype, IOException;
3736

archetype-common/src/main/java/org/apache/maven/archetype/common/ArchetypeFilesResolver.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
/** @author rafale */
2828
public interface ArchetypeFilesResolver {
29-
String ROLE = ArchetypeFilesResolver.class.getName();
3029

3130
List<String> getFilesWithExtension(List<String> files, String extension);
3231

archetype-common/src/main/java/org/apache/maven/archetype/common/Constants.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,24 @@ public interface Constants {
5555

5656
String EXCLUDE_PATTERNS = "excludePatterns";
5757

58-
List<String> DEFAULT_FILTERED_EXTENSIONS = Arrays.asList(new String[] {
59-
"java",
60-
"xml",
61-
"txt",
62-
"groovy",
63-
"cs",
64-
"mdo",
65-
"aj",
66-
"jsp",
67-
"gsp",
68-
"vm",
69-
"html",
70-
"xhtml",
71-
"properties",
72-
".classpath",
73-
".project"
74-
});
75-
76-
List<String> DEFAULT_LANGUAGES = Arrays.asList(new String[] {"java", "groovy", "csharp", "aspectj"});
58+
List<String> DEFAULT_FILTERED_EXTENSIONS = Arrays.asList(
59+
"java",
60+
"xml",
61+
"txt",
62+
"groovy",
63+
"cs",
64+
"mdo",
65+
"aj",
66+
"jsp",
67+
"gsp",
68+
"vm",
69+
"html",
70+
"xhtml",
71+
"properties",
72+
".classpath",
73+
".project");
74+
75+
List<String> DEFAULT_LANGUAGES = Arrays.asList("java", "groovy", "csharp", "aspectj");
7776

7877
String GROUP_ID = "groupId";
7978

@@ -91,12 +90,8 @@ public interface Constants {
9190

9291
String PARENT_ARTIFACT_ID = "parentArtifactId";
9392

94-
String POM_PATH = Constants.ARCHETYPE_RESOURCES + "/" + Constants.ARCHETYPE_POM;
95-
9693
String RESOURCES = "resources";
9794

98-
String SITE = "site";
99-
10095
String SRC = "src";
10196

10297
String TEST = "test";

archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultArchetypeArtifactManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import java.io.File;
2626
import java.io.IOException;
2727
import java.io.InputStream;
28+
import java.io.InputStreamReader;
2829
import java.io.Reader;
2930
import java.net.MalformedURLException;
3031
import java.net.URL;
3132
import java.net.URLClassLoader;
33+
import java.nio.charset.StandardCharsets;
3234
import java.util.ArrayList;
3335
import java.util.Enumeration;
3436
import java.util.List;
@@ -46,7 +48,6 @@
4648
import org.apache.maven.archetype.old.descriptor.ArchetypeDescriptorBuilder;
4749
import org.apache.maven.model.Model;
4850
import org.codehaus.plexus.logging.AbstractLogEnabled;
49-
import org.codehaus.plexus.util.ReaderFactory;
5051
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
5152
import org.eclipse.aether.RepositorySystemSession;
5253
import org.eclipse.aether.repository.RemoteRepository;
@@ -265,7 +266,7 @@ private void setArchetype(
265266
}
266267

267268
private boolean isFileSetArchetype(ZipFile zipFile) throws IOException {
268-
try (Reader reader = getArchetypeDescriptorReader(zipFile); ) {
269+
try (Reader reader = getArchetypeDescriptorReader(zipFile)) {
269270
return (reader != null);
270271
}
271272
}
@@ -330,7 +331,7 @@ private Reader getDescriptorReader(ZipFile zipFile, String descriptor) throws IO
330331
throw new IOException("The " + descriptor + " descriptor cannot be read in " + zipFile.getName() + ".");
331332
}
332333

333-
return ReaderFactory.newReader(is, ReaderFactory.UTF_8);
334+
return new InputStreamReader(is, StandardCharsets.UTF_8);
334335
}
335336

336337
private ZipEntry searchEntry(ZipFile zipFile, String searchString) {

archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultArchetypeFilesResolver.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.File;
2525
import java.io.IOException;
2626
import java.util.ArrayList;
27-
import java.util.Arrays;
2827
import java.util.HashSet;
2928
import java.util.List;
3029
import java.util.Set;
@@ -271,14 +270,14 @@ private String getCommonPackage(String packageName, String templatePackage) {
271270
}
272271

273272
private List<String> resolveFiles(File basedir, List<String> languages) throws IOException {
274-
String[] languagesArray = languages.toArray(new String[languages.size()]);
273+
String[] languagesArray = languages.toArray(new String[0]);
275274
String[] languagesPathesArray = new String[languagesArray.length];
276275
for (int i = 0; i < languagesArray.length; i++) {
277276
languagesPathesArray[i] = "**/src/**/" + languagesArray[i] + "/**";
278277
}
279278

280279
String excludes = "target";
281-
for (String defaultExclude : Arrays.asList(ListScanner.DEFAULTEXCLUDES)) {
280+
for (String defaultExclude : ListScanner.DEFAULTEXCLUDES) {
282281
excludes += "," + defaultExclude + "/**";
283282
}
284283

archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultPomManager.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import javax.xml.transform.TransformerException;
2525

2626
import java.io.File;
27-
import java.io.FileInputStream;
2827
import java.io.FileNotFoundException;
2928
import java.io.FileOutputStream;
3029
import java.io.IOException;
@@ -33,6 +32,7 @@
3332
import java.io.Reader;
3433
import java.io.StringWriter;
3534
import java.io.Writer;
35+
import java.nio.file.Files;
3636
import java.util.ArrayList;
3737
import java.util.HashMap;
3838
import java.util.List;
@@ -56,8 +56,8 @@
5656
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
5757
import org.codehaus.plexus.logging.AbstractLogEnabled;
5858
import org.codehaus.plexus.util.FileUtils;
59-
import org.codehaus.plexus.util.ReaderFactory;
6059
import org.codehaus.plexus.util.StringUtils;
60+
import org.codehaus.plexus.util.xml.XmlStreamReader;
6161
import org.codehaus.plexus.util.xml.Xpp3Dom;
6262
import org.codehaus.plexus.util.xml.Xpp3DomUtils;
6363
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@@ -73,7 +73,7 @@ public void addModule(File pom, String artifactId)
7373
throws IOException, ParserConfigurationException, TransformerException, SAXException, InvalidPackaging,
7474
ArchetypeTemplateProcessingException {
7575
StringWriter out = new StringWriter();
76-
boolean found = PomUtils.addNewModule(artifactId, ReaderFactory.newXmlReader(pom), out);
76+
boolean found = PomUtils.addNewModule(artifactId, new XmlStreamReader(pom), out);
7777
if (found) {
7878
FileUtils.fileWrite(pom.getAbsolutePath(), out.toString());
7979
}
@@ -182,7 +182,7 @@ public void mergePoms(File pom, File temporaryPom) throws IOException, XmlPullPa
182182

183183
@Override
184184
public Model readPom(final File pomFile) throws IOException, XmlPullParserException {
185-
try (Reader pomReader = ReaderFactory.newXmlReader(pomFile)) {
185+
try (Reader pomReader = new XmlStreamReader(pomFile)) {
186186
MavenXpp3Reader reader = new MavenXpp3Reader();
187187

188188
return reader.read(pomReader);
@@ -191,7 +191,7 @@ public Model readPom(final File pomFile) throws IOException, XmlPullParserExcept
191191

192192
@Override
193193
public Model readPom(InputStream pomStream) throws IOException, XmlPullParserException {
194-
try (Reader pomReader = ReaderFactory.newXmlReader(pomStream)) {
194+
try (Reader pomReader = new XmlStreamReader(pomStream)) {
195195
MavenXpp3Reader reader = new MavenXpp3Reader();
196196

197197
return reader.read(pomReader);
@@ -203,7 +203,7 @@ public void writePom(final Model model, final File pomFile, final File initialPo
203203
String fileEncoding = StringUtils.isEmpty(model.getModelEncoding()) ? "UTF-8" : model.getModelEncoding();
204204

205205
org.jdom2.Document doc;
206-
try (InputStream inputStream = new FileInputStream(initialPomFile)) {
206+
try (InputStream inputStream = Files.newInputStream(initialPomFile.toPath())) {
207207
SAXBuilder builder = new SAXBuilder();
208208
doc = builder.build(inputStream);
209209
} catch (JDOMException exc) {
@@ -225,7 +225,7 @@ public void writePom(final Model model, final File pomFile, final File initialPo
225225
} catch (FileNotFoundException e) {
226226
getLogger().debug("Creating pom file " + pomFile);
227227

228-
try (Writer pomWriter = new OutputStreamWriter(new FileOutputStream(pomFile), fileEncoding)) {
228+
try (Writer pomWriter = new OutputStreamWriter(Files.newOutputStream(pomFile.toPath()), fileEncoding)) {
229229
MavenXpp3Writer writer = new MavenXpp3Writer();
230230
writer.write(pomWriter, model);
231231
}
@@ -253,13 +253,13 @@ private void mergeModelBuild(Model model, Model generatedModel) {
253253

254254
private void mergeProfiles(Model model, Model generatedModel) {
255255
List<Profile> generatedProfiles = generatedModel.getProfiles();
256-
if (generatedProfiles != null && generatedProfiles.size() > 0) {
256+
if (generatedProfiles != null && !generatedProfiles.isEmpty()) {
257257
List<Profile> modelProfiles = model.getProfiles();
258258
Map<String, Profile> modelProfileIdMap = new HashMap<>();
259259
if (modelProfiles == null) {
260260
modelProfiles = new ArrayList<>();
261261
model.setProfiles(modelProfiles);
262-
} else if (modelProfiles.size() > 0) {
262+
} else if (!modelProfiles.isEmpty()) {
263263
// add profile ids from the model for later lookups to the modelProfileIds set
264264
for (Profile modelProfile : modelProfiles) {
265265
modelProfileIdMap.put(modelProfile.getId(), modelProfile);

archetype-common/src/main/java/org/apache/maven/archetype/common/util/ListScanner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public void setCaseSensitive(boolean isCaseSensitive) {
334334
* elements must be non-<code>null</code>.
335335
*/
336336
public void setExcludes(List<String> excludesList) {
337-
String[] excludes = excludesList.toArray(new String[excludesList.size()]);
337+
String[] excludes = excludesList.toArray(new String[0]);
338338
if (excludes == null) {
339339
this.excludes = null;
340340
} else {
@@ -362,7 +362,7 @@ public void setExcludes(String excludes) {
362362
* elements must be non-<code>null</code>.
363363
*/
364364
public void setIncludes(List<String> includesList) {
365-
String[] includes = includesList.toArray(new String[includesList.size()]);
365+
String[] includes = includesList.toArray(new String[0]);
366366
if (includes == null) {
367367
this.includes = null;
368368
} else {
@@ -449,7 +449,7 @@ protected boolean matchesPatterns(String name, String[] patterns) {
449449
String path = null;
450450

451451
String baseDir = getBasedir();
452-
if (baseDir.length() > 0) {
452+
if (!baseDir.isEmpty()) {
453453
baseDir = baseDir.concat(File.separator);
454454
}
455455

archetype-common/src/main/java/org/apache/maven/archetype/common/util/NamespaceStack.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ public String getURI(String prefix) {
140140
if (index == -1) {
141141
return null;
142142
}
143-
String uri = uris.elementAt(index);
144-
return uri;
143+
return uris.elementAt(index);
145144
}
146145

147146
/**

archetype-common/src/main/java/org/apache/maven/archetype/common/util/XMLOutputter.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,7 @@ private Writer makeWriter(OutputStream out) throws java.io.UnsupportedEncodingEx
350350

351351
/** Get an OutputStreamWriter, use specified encoding. */
352352
private static Writer makeWriter(OutputStream out, String enc) throws java.io.UnsupportedEncodingException {
353-
Writer writer = new BufferedWriter((new OutputStreamWriter(new BufferedOutputStream(out), enc)));
354-
return writer;
353+
return new BufferedWriter((new OutputStreamWriter(new BufferedOutputStream(out), enc)));
355354
}
356355

357356
// * * * * * * * * * * Output to a Writer * * * * * * * * * *
@@ -1271,12 +1270,12 @@ private boolean isAllWhitespace(Object obj) {
12711270

12721271
// Determine if a string starts with a XML whitespace.
12731272
private boolean startsWithWhite(String str) {
1274-
return ((str != null) && (str.length() > 0) && isWhitespace(str.charAt(0)));
1273+
return ((str != null) && (!str.isEmpty()) && isWhitespace(str.charAt(0)));
12751274
}
12761275

12771276
// Determine if a string ends with a XML whitespace.
12781277
private boolean endsWithWhite(String str) {
1279-
return ((str != null) && (str.length() > 0) && isWhitespace(str.charAt(str.length() - 1)));
1278+
return ((str != null) && (!str.isEmpty()) && isWhitespace(str.charAt(str.length() - 1)));
12801279
}
12811280

12821281
// Determine if a character is a XML whitespace.
@@ -1486,7 +1485,7 @@ public String toString() {
14861485
+ "omitEncoding = " + userFormat.omitEncoding + ", "
14871486
+ "indent = '" + userFormat.indent + "'" + ", "
14881487
+ "expandEmptyElements = " + userFormat.expandEmptyElements + ", "
1489-
+ "lineSeparator = '" + buffer.toString() + "', "
1488+
+ "lineSeparator = '" + buffer + "', "
14901489
+ "textMode = " + userFormat.mode + "]");
14911490
}
14921491

@@ -1508,12 +1507,12 @@ private NamespaceStack createNamespaceStack() {
15081507
* declare a NamespaceStack parameter, but we don't want to
15091508
* declare the parent NamespaceStack class as public.
15101509
*/
1511-
protected class NamespaceStack extends org.apache.maven.archetype.common.util.NamespaceStack {}
1510+
protected static class NamespaceStack extends org.apache.maven.archetype.common.util.NamespaceStack {}
15121511

15131512
// Support method to print a name without using elt.getQualifiedName()
15141513
// and thus avoiding a StringBuilder creation and memory churn
15151514
private void printQualifiedName(Writer out, Element e) throws IOException {
1516-
if (e.getNamespace().getPrefix().length() == 0) {
1515+
if (e.getNamespace().getPrefix().isEmpty()) {
15171516
out.write(e.getName());
15181517
} else {
15191518
out.write(e.getNamespace().getPrefix());

0 commit comments

Comments
 (0)