You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2018/06/30 20:54:06 UTC

[maven-jxr] 02/02: try-with-resources

This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-jxr.git

commit 82ba337608a6ad412317cec5fdbf489f046f0b37
Author: rfscholte <rf...@apache.org>
AuthorDate: Sat Jun 30 22:49:29 2018 +0200

    try-with-resources
---
 .../org/apache/maven/plugin/jxr/JxrReportTest.java | 11 +++---
 .../apache/maven/plugin/jxr/JxrTestReportTest.java | 11 +++---
 .../org/apache/maven/jxr/DirectoryIndexer.java     | 10 +----
 .../org/apache/maven/jxr/pacman/JavaFileImpl.java  | 44 ++++++++++------------
 4 files changed, 33 insertions(+), 43 deletions(-)

diff --git a/maven-jxr-plugin/src/test/java/org/apache/maven/plugin/jxr/JxrReportTest.java b/maven-jxr-plugin/src/test/java/org/apache/maven/plugin/jxr/JxrReportTest.java
index 6f7689e..42c59d1 100644
--- a/maven-jxr-plugin/src/test/java/org/apache/maven/plugin/jxr/JxrReportTest.java
+++ b/maven-jxr-plugin/src/test/java/org/apache/maven/plugin/jxr/JxrReportTest.java
@@ -298,13 +298,14 @@ public class JxrReportTest
         throws IOException
     {
         String str = "", strTmp = "";
-        BufferedReader in = new BufferedReader( new FileReader( file ) );
-
-        while ( ( strTmp = in.readLine() ) != null )
+        
+        try ( BufferedReader in = new BufferedReader( new FileReader( file ) ) )
         {
-            str = str + ' ' + strTmp;
+            while ( ( strTmp = in.readLine() ) != null )
+            {
+                str = str + ' ' + strTmp;
+            }
         }
-        in.close();
 
         return str;
     }
diff --git a/maven-jxr-plugin/src/test/java/org/apache/maven/plugin/jxr/JxrTestReportTest.java b/maven-jxr-plugin/src/test/java/org/apache/maven/plugin/jxr/JxrTestReportTest.java
index 6085e6f..a089758 100644
--- a/maven-jxr-plugin/src/test/java/org/apache/maven/plugin/jxr/JxrTestReportTest.java
+++ b/maven-jxr-plugin/src/test/java/org/apache/maven/plugin/jxr/JxrTestReportTest.java
@@ -90,13 +90,14 @@ public class JxrTestReportTest
         throws IOException
     {
         String str = "", strTmp = "";
-        BufferedReader in = new BufferedReader( new FileReader( file ) );
-
-        while ( ( strTmp = in.readLine() ) != null )
+        
+        try ( BufferedReader in = new BufferedReader( new FileReader( file ) ) )
         {
-            str = str + ' ' + strTmp;
+            while ( ( strTmp = in.readLine() ) != null )
+            {
+                str = str + ' ' + strTmp;
+            }
         }
-        in.close();
 
         return str;
     }
diff --git a/maven-jxr/src/main/java/org/apache/maven/jxr/DirectoryIndexer.java b/maven-jxr/src/main/java/org/apache/maven/jxr/DirectoryIndexer.java
index 7628a59..5a465d2 100644
--- a/maven-jxr/src/main/java/org/apache/maven/jxr/DirectoryIndexer.java
+++ b/maven-jxr/src/main/java/org/apache/maven/jxr/DirectoryIndexer.java
@@ -31,7 +31,6 @@ import java.util.Iterator;
 import java.util.Map;
 import java.util.TreeMap;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.maven.jxr.log.Log;
 import org.apache.maven.jxr.log.VelocityLogger;
 import org.apache.maven.jxr.pacman.ClassType;
@@ -292,12 +291,9 @@ public class DirectoryIndexer
         // output file
         File file = new File( outDir, templateName + ".html" );
         file.getParentFile().mkdirs();
-        Writer writer = null;
 
-        try
+        try ( Writer writer = new OutputStreamWriter( new FileOutputStream( file ), getOutputEncoding() ) )
         {
-            writer = new OutputStreamWriter( new FileOutputStream( file ), getOutputEncoding() );
-
             // template file
             StringBuilder templateFile = new StringBuilder();
             File templateDirFile = new File( getTemplateDir() );
@@ -319,10 +315,6 @@ public class DirectoryIndexer
         {
             throw new JxrException( "Error merging velocity template", e );
         }
-        finally
-        {
-            IOUtils.closeQuietly( writer );
-        }
     }
 
     /*
diff --git a/maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFileImpl.java b/maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFileImpl.java
index b496c86..e1594e7 100644
--- a/maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFileImpl.java
+++ b/maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFileImpl.java
@@ -38,8 +38,6 @@ import java.nio.file.Path;
 public class JavaFileImpl
     extends JavaFile
 {
-    private Reader reader;
-
     /**
      * Create a new JavaFileImpl that points to a given file...
      *
@@ -68,9 +66,9 @@ public class JavaFileImpl
         throws IOException
     {
         StreamTokenizer stok = null;
-        try
+        try ( Reader reader = getReader() )
         {
-            stok = this.getTokenizer();
+            stok = this.getTokenizer( reader );
 
             while ( stok.nextToken() != StreamTokenizer.TT_EOF )
             {
@@ -124,10 +122,6 @@ public class JavaFileImpl
         finally
         {
             stok = null;
-            if ( this.reader != null )
-            {
-                this.reader.close();
-            }
         }
     }
 
@@ -149,24 +143,9 @@ public class JavaFileImpl
     /**
      * Get a StreamTokenizer for this file.
      */
-    private StreamTokenizer getTokenizer()
+    private StreamTokenizer getTokenizer( Reader reader )
         throws IOException
     {
-
-        if ( Files.notExists( this.getPath() ) )
-        {
-            throw new IOException( this.getPath() + " does not exist!" );
-        }
-
-        if ( this.getEncoding() != null )
-        {
-            this.reader = new InputStreamReader( new FileInputStream( this.getPath().toFile() ), this.getEncoding() );
-        }
-        else
-        {
-            this.reader = new FileReader( this.getPath().toFile() );
-        }
-
         StreamTokenizer stok = new StreamTokenizer( reader );
         //int tok;
 
@@ -179,5 +158,22 @@ public class JavaFileImpl
 
         return stok;
     }
+    
+    private Reader getReader()
+        throws IOException
+    {
+        if ( Files.notExists( this.getPath() ) )
+        {
+            throw new IOException( this.getPath() + " does not exist!" );
+        }
 
+        if ( this.getEncoding() != null )
+        {
+            return new InputStreamReader( new FileInputStream( this.getPath().toFile() ), this.getEncoding() );
+        }
+        else
+        {
+            return new FileReader( this.getPath().toFile() );
+        }
+    }    
 }