You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2019/10/02 15:25:18 UTC
[maven-plugin-tools] 01/01: Use Java 7 APIs and language features
This is an automated email from the ASF dual-hosted git repository.
tibordigana pushed a commit to branch MPLUGIN-353
in repository https://gitbox.apache.org/repos/asf/maven-plugin-tools.git
commit f5545908a58b214a42132a4848392fdac0037add
Author: Andrew Gaul <an...@gaul.org>
AuthorDate: Sat Aug 31 10:34:30 2019 -0700
Use Java 7 APIs and language features
Found via error-prone and Modernizer.
---
.../src/main/java/test/MyMojo.java | 19 +------------------
.../org/apache/maven/plugins/plugin/it/MyMojo.java | 19 +------------------
.../org/apache/maven/plugins/plugin/it/MyMojo.java | 19 +------------------
.../apache/maven/plugins/issues/plugin/MyMojo.java | 19 +------------------
.../maven/tools/plugin/generator/GeneratorUtils.java | 4 +++-
.../plugin/generator/PluginDescriptorGenerator.java | 8 ++++----
.../tools/plugin/generator/PluginHelpGenerator.java | 15 +++++++--------
.../tools/plugin/generator/PluginXdocGenerator.java | 8 ++++----
.../src/main/resources/help-class-source.vm | 18 +-----------------
.../beanshell/BeanshellMojoDescriptorExtractor.java | 10 +++-------
.../org/apache/maven/script/ant/AntMojoWrapper.java | 2 +-
.../apache/maven/script/ant/AntMojoWrapperTest.java | 10 +---------
12 files changed, 28 insertions(+), 123 deletions(-)
diff --git a/maven-plugin-plugin/src/it/fix-maven-since-3.x/javasample-maven-plugin/src/main/java/test/MyMojo.java b/maven-plugin-plugin/src/it/fix-maven-since-3.x/javasample-maven-plugin/src/main/java/test/MyMojo.java
index ee28d9e..978e624 100644
--- a/maven-plugin-plugin/src/it/fix-maven-since-3.x/javasample-maven-plugin/src/main/java/test/MyMojo.java
+++ b/maven-plugin-plugin/src/it/fix-maven-since-3.x/javasample-maven-plugin/src/main/java/test/MyMojo.java
@@ -53,30 +53,13 @@ public class MyMojo
File touch = new File( f, "touch.txt" );
- FileWriter w = null;
- try
+ try ( FileWriter w = new FileWriter( touch ) )
{
- w = new FileWriter( touch );
-
w.write( "touch.txt" );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error creating file " + touch, e );
}
- finally
- {
- if ( w != null )
- {
- try
- {
- w.close();
- }
- catch ( IOException e )
- {
- // ignore
- }
- }
- }
}
}
diff --git a/maven-plugin-plugin/src/it/mplugin-191/src/main/java/org/apache/maven/plugins/plugin/it/MyMojo.java b/maven-plugin-plugin/src/it/mplugin-191/src/main/java/org/apache/maven/plugins/plugin/it/MyMojo.java
index 14c7b92..0f75f16 100644
--- a/maven-plugin-plugin/src/it/mplugin-191/src/main/java/org/apache/maven/plugins/plugin/it/MyMojo.java
+++ b/maven-plugin-plugin/src/it/mplugin-191/src/main/java/org/apache/maven/plugins/plugin/it/MyMojo.java
@@ -52,30 +52,13 @@ public class MyMojo
File touch = new File( f, "touch.txt" );
- FileWriter w = null;
- try
+ try ( FileWriter w = new FileWriter( touch ) )
{
- w = new FileWriter( touch );
-
w.write( "touch.txt" );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error creating file " + touch, e );
}
- finally
- {
- if ( w != null )
- {
- try
- {
- w.close();
- }
- catch ( IOException e )
- {
- // ignore
- }
- }
- }
}
}
diff --git a/maven-plugin-plugin/src/it/mplugin-223/src/main/java/org/apache/maven/plugins/plugin/it/MyMojo.java b/maven-plugin-plugin/src/it/mplugin-223/src/main/java/org/apache/maven/plugins/plugin/it/MyMojo.java
index 14c7b92..0f75f16 100644
--- a/maven-plugin-plugin/src/it/mplugin-223/src/main/java/org/apache/maven/plugins/plugin/it/MyMojo.java
+++ b/maven-plugin-plugin/src/it/mplugin-223/src/main/java/org/apache/maven/plugins/plugin/it/MyMojo.java
@@ -52,30 +52,13 @@ public class MyMojo
File touch = new File( f, "touch.txt" );
- FileWriter w = null;
- try
+ try ( FileWriter w = new FileWriter( touch ) )
{
- w = new FileWriter( touch );
-
w.write( "touch.txt" );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error creating file " + touch, e );
}
- finally
- {
- if ( w != null )
- {
- try
- {
- w.close();
- }
- catch ( IOException e )
- {
- // ignore
- }
- }
- }
}
}
diff --git a/maven-plugin-plugin/src/it/plugin-info-jdk-default-version/src/main/java/org/apache/maven/plugins/issues/plugin/MyMojo.java b/maven-plugin-plugin/src/it/plugin-info-jdk-default-version/src/main/java/org/apache/maven/plugins/issues/plugin/MyMojo.java
index 3f2d827..2cabe0c 100644
--- a/maven-plugin-plugin/src/it/plugin-info-jdk-default-version/src/main/java/org/apache/maven/plugins/issues/plugin/MyMojo.java
+++ b/maven-plugin-plugin/src/it/plugin-info-jdk-default-version/src/main/java/org/apache/maven/plugins/issues/plugin/MyMojo.java
@@ -52,30 +52,13 @@ public class MyMojo
File touch = new File( f, "touch.txt" );
- FileWriter w = null;
- try
+ try ( FileWriter w = new FileWriter( touch ) )
{
- w = new FileWriter( touch );
-
w.write( "touch.txt" );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error creating file " + touch, e );
}
- finally
- {
- if ( w != null )
- {
- try
- {
- w.close();
- }
- catch ( IOException e )
- {
- // ignore
- }
- }
- }
}
}
diff --git a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/GeneratorUtils.java b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/GeneratorUtils.java
index 771b08b..f829d60 100644
--- a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/GeneratorUtils.java
+++ b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/GeneratorUtils.java
@@ -19,6 +19,8 @@ package org.apache.maven.tools.plugin.generator;
* under the License.
*/
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -284,7 +286,7 @@ public final class GeneratorUtils
try
{
ByteArrayOutputStream out = new ByteArrayOutputStream( commentCleaned.length() + 256 );
- tidy.parse( new ByteArrayInputStream( commentCleaned.getBytes( "UTF-8" ) ), out );
+ tidy.parse( new ByteArrayInputStream( commentCleaned.getBytes( UTF_8 ) ), out );
commentCleaned = out.toString( "UTF-8" );
}
catch ( UnsupportedEncodingException e )
diff --git a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java
index 1375cae..736d301 100644
--- a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java
+++ b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java
@@ -19,6 +19,8 @@ package org.apache.maven.tools.plugin.generator;
* under the License.
*/
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -117,14 +119,12 @@ public class PluginDescriptorGenerator
}
}
- String encoding = "UTF-8";
-
Writer writer = null;
try
{
- writer = new OutputStreamWriter( new FileOutputStream( destinationFile ), encoding );
+ writer = new OutputStreamWriter( new FileOutputStream( destinationFile ), UTF_8 );
- XMLWriter w = new PrettyPrintXMLWriter( writer, encoding, null );
+ XMLWriter w = new PrettyPrintXMLWriter( writer, UTF_8.name(), null );
w.writeMarkup( "\n<!-- Generated by maven-plugin-tools " + getVersion() + " -->\n\n" );
diff --git a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java
index 27688cf..d86144d 100644
--- a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java
+++ b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java
@@ -19,6 +19,8 @@ package org.apache.maven.tools.plugin.generator;
* under the License.
*/
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.logging.Log;
@@ -50,7 +52,7 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import java.util.List;
import java.util.Properties;
@@ -201,15 +203,11 @@ public class PluginHelpGenerator
// plugin-tools sources are UTF-8 (and even ASCII in this case))
try ( InputStream is = //
Thread.currentThread().getContextClassLoader().getResourceAsStream( "help-class-source.vm" ); //
- InputStreamReader isReader = new InputStreamReader( is, "UTF-8" ) )
+ InputStreamReader isReader = new InputStreamReader( is, UTF_8 ) )
{
//isReader =
velocityComponent.getEngine().evaluate( context, stringWriter, "", isReader );
}
- catch ( UnsupportedEncodingException e )
- {
- // not supposed to happen since UTF-8 is supposed to be supported by any JVM
- }
return stringWriter.toString();
}
@@ -361,11 +359,12 @@ public class PluginHelpGenerator
{
helpSourceFileNew.getParentFile().mkdirs();
}
+ Charset encoding = Charset.forName( request.getEncoding() );
try ( Reader sourceReader = new InputStreamReader( new FileInputStream( helpSourceFile ), //
- request.getEncoding() ); //
+ encoding ); //
PrintWriter sourceWriter = new PrintWriter(
new OutputStreamWriter( new FileOutputStream( helpSourceFileNew ), //
- request.getEncoding() ) ) )
+ encoding ) ) )
{
sourceWriter.println( "package " + destinationPackage + ";" );
IOUtil.copy( sourceReader, sourceWriter );
diff --git a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java
index 77447c2..8d3aefa 100644
--- a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java
+++ b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java
@@ -19,6 +19,8 @@ package org.apache.maven.tools.plugin.generator;
* under the License.
*/
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.project.MavenProject;
@@ -130,11 +132,9 @@ public class PluginXdocGenerator
throws IOException
{
File outputFile = new File( destinationDirectory, getMojoFilename( mojoDescriptor, "xml" ) );
- String encoding = "UTF-8";
- try ( Writer writer = new OutputStreamWriter( new FileOutputStream( outputFile ), encoding ) )
+ try ( Writer writer = new OutputStreamWriter( new FileOutputStream( outputFile ), UTF_8 ) )
{
-
- XMLWriter w = new PrettyPrintXMLWriter( new PrintWriter( writer ), encoding, null );
+ XMLWriter w = new PrettyPrintXMLWriter( new PrintWriter( writer ), UTF_8.name(), null );
writeBody( mojoDescriptor, w );
writer.flush();
diff --git a/maven-plugin-tools-generators/src/main/resources/help-class-source.vm b/maven-plugin-tools-generators/src/main/resources/help-class-source.vm
index eeafb3e..b467873 100644
--- a/maven-plugin-tools-generators/src/main/resources/help-class-source.vm
+++ b/maven-plugin-tools-generators/src/main/resources/help-class-source.vm
@@ -114,10 +114,8 @@ public class HelpMojo
throws MojoExecutionException
{
getLog().debug( "load plugin-help.xml: " + PLUGIN_HELP_PATH );
- InputStream is = null;
- try
+ try ( InputStream is = getClass().getResourceAsStream( PLUGIN_HELP_PATH ) )
{
- is = getClass().getResourceAsStream( PLUGIN_HELP_PATH );
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
return dBuilder.parse( is );
@@ -134,20 +132,6 @@ public class HelpMojo
{
throw new MojoExecutionException( e.getMessage(), e );
}
- finally
- {
- if ( is != null )
- {
- try
- {
- is.close();
- }
- catch ( IOException e )
- {
- throw new MojoExecutionException( e.getMessage(), e );
- }
- }
- }
}
/**
diff --git a/maven-script/maven-plugin-tools-beanshell/src/main/java/org/apache/maven/tools/plugin/extractor/beanshell/BeanshellMojoDescriptorExtractor.java b/maven-script/maven-plugin-tools-beanshell/src/main/java/org/apache/maven/tools/plugin/extractor/beanshell/BeanshellMojoDescriptorExtractor.java
index 5b5f638..9a12df1 100644
--- a/maven-script/maven-plugin-tools-beanshell/src/main/java/org/apache/maven/tools/plugin/extractor/beanshell/BeanshellMojoDescriptorExtractor.java
+++ b/maven-script/maven-plugin-tools-beanshell/src/main/java/org/apache/maven/tools/plugin/extractor/beanshell/BeanshellMojoDescriptorExtractor.java
@@ -19,6 +19,8 @@ package org.apache.maven.tools.plugin.extractor.beanshell;
* under the License.
*/
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import bsh.EvalError;
import bsh.Interpreter;
import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
@@ -31,7 +33,6 @@ import org.codehaus.plexus.component.annotations.Component;
import java.io.File;
import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -118,17 +119,12 @@ public class BeanshellMojoDescriptorExtractor
interpreter.set( "encoding", "UTF-8" );
- interpreter.eval( new InputStreamReader( getClass().getResourceAsStream( "/extractor.bsh" ), "UTF-8" ) );
+ interpreter.eval( new InputStreamReader( getClass().getResourceAsStream( "/extractor.bsh" ), UTF_8 ) );
}
catch ( EvalError evalError )
{
throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError );
}
- catch ( UnsupportedEncodingException uee )
- {
- // should not occur...
- throw new InvalidPluginDescriptorException( "Unsupported encoding while reading beanshell script", uee );
- }
return mojoDescriptor;
}
diff --git a/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java b/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java
index 62223ad..56eba08 100644
--- a/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java
+++ b/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java
@@ -125,7 +125,7 @@ public class AntMojoWrapper
if ( logger.isDebugEnabled() && !unconstructedParts.isEmpty() )
{
- StringBuffer buffer = new StringBuffer();
+ StringBuilder buffer = new StringBuilder();
buffer.append( "The following standard Maven Ant-mojo support objects could not be created:\n\n" );
diff --git a/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java b/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java
index 76c7909..6d833a9 100644
--- a/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java
+++ b/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java
@@ -138,18 +138,10 @@ public class AntMojoWrapperTest
fail( "plugin descriptor not found: '" + pluginXml + "'." );
}
- Reader reader = null;
PluginDescriptor pd;
- try
+ try ( Reader reader = new InputStreamReader( resource.openStream() ) )
{
- reader = new InputStreamReader( resource.openStream() );
pd = new PluginDescriptorBuilder().build( reader, pluginXml );
- reader.close();
- reader = null;
- }
- finally
- {
- IOUtil.close( reader );
}
Map<String, Object> config = new HashMap<String, Object>();