You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2020/05/29 10:38:21 UTC

[maven-shared-utils] 01/01: deprecate PropertyUtils constructor and clean up docs

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

elharo pushed a commit to branch path
in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git

commit c3181959dc2339ff454b2fa2d0a52e2c1fcd0ecb
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Fri May 29 06:37:50 2020 -0400

    deprecate PropertyUtils constructor and clean up docs
---
 .../apache/maven/shared/utils/PropertyUtils.java   | 44 +++++++++-------------
 .../maven/shared/utils/PropertyUtilsTest.java      | 39 ++++++-------------
 2 files changed, 29 insertions(+), 54 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/utils/PropertyUtils.java b/src/main/java/org/apache/maven/shared/utils/PropertyUtils.java
index ca6b693..487a9a6 100644
--- a/src/main/java/org/apache/maven/shared/utils/PropertyUtils.java
+++ b/src/main/java/org/apache/maven/shared/utils/PropertyUtils.java
@@ -29,26 +29,27 @@ import java.util.Properties;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
-import org.apache.maven.shared.utils.io.IOUtil;
-
 /**
- *
+ * Static utility methods for loading properties.
  */
 public class PropertyUtils
 {
 
     /**
      * The constructor.
+     *
+     * @deprecated This is a utility class with only static methods. Don't create instances of it.
      */
+    @Deprecated
     public PropertyUtils()
     {
         // should throw new IllegalAccessError( "Utility class" );
     }
 
     /**
-     * @param url The URL which should be used to load the properties.
-     * @return The loaded properties.
-     * @deprecated As of 3.1.0, please use method {@link #loadOptionalProperties(java.net.URL)}. This method should not
+     * @param url the URL which should be used to load the properties
+     * @return the loaded properties
+     * @deprecated use {@link #loadOptionalProperties(java.net.URL)} instead. This method should not
      *             be used as it suppresses exceptions silently when loading properties fails and returns {@code null}
      *             instead of an empty {@code Properties} instance when the given {@code URL} is {@code null}.
      */
@@ -67,9 +68,9 @@ public class PropertyUtils
     }
 
     /**
-     * @param file The file from which the properties will be loaded.
-     * @return The loaded properties.
-     * @deprecated As of 3.1.0, please use method {@link #loadOptionalProperties(java.io.File)}. This method should not
+     * @param file the file from which the properties will be loaded
+     * @return the loaded properties
+     * @deprecated use {@link #loadOptionalProperties(java.io.File)} instead. This method should not
      *             be used as it suppresses exceptions silently when loading properties fails and returns {@code null}
      *             instead of an empty {@code Properties} instance when the given {@code File} is {@code null}.
      */
@@ -89,8 +90,8 @@ public class PropertyUtils
 
     /**
      * @param is {@link InputStream}
-     * @return The loaded properties.
-     * @deprecated As of 3.1.0, please use method {@link #loadOptionalProperties(java.io.InputStream)}. This method
+     * @return the loaded properties
+     * @deprecated use {@link #loadOptionalProperties(java.io.InputStream)} instead. This method
      *             should not be used as it suppresses exceptions silently when loading properties fails.
      */
     @Deprecated
@@ -98,13 +99,12 @@ public class PropertyUtils
     {
         try
         {
-            // to make this the same behaviour as the others we should really return null on any error
             Properties result = new Properties();
             if ( is != null )
             {
-                try
+                try ( InputStream in = is )
                 {
-                    result.load( is );
+                    result.load( in );
                 }
                 catch ( IOException e )
                 {
@@ -117,10 +117,6 @@ public class PropertyUtils
         {
             // ignore
         }
-        finally
-        {
-            IOUtil.close( is );
-        }
         return null;
     }
 
@@ -154,7 +150,7 @@ public class PropertyUtils
     }
 
     /**
-     * Loads {@code Properties} from a given {@code File}.
+     * Loads {@code Properties} from a {@code File}.
      * <p>
      * If the given {@code File} is {@code null} or the properties file can't be read, an empty properties object is
      * returned.
@@ -185,7 +181,7 @@ public class PropertyUtils
     }
 
     /**
-     * Loads {@code Properties} from a given {@code InputStream}.
+     * Loads {@code Properties} from an {@code InputStream}.
      * <p>
      * If the given {@code InputStream} is {@code null} or the properties can't be read, an empty properties object is
      * returned.
@@ -203,18 +199,14 @@ public class PropertyUtils
 
         if ( inputStream != null )
         {
-            try
+            try ( InputStream in = inputStream ) // reassign inputStream to autoclose
             {
-                properties.load( inputStream );
+                properties.load( in );
             }
             catch ( IllegalArgumentException | IOException ex )
             {
                 // ignore and return empty properties
             }
-            finally
-            {
-                IOUtil.close( inputStream );
-            }
         }
 
         return properties;
diff --git a/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java
index 41067a4..0c69641 100644
--- a/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java
@@ -33,7 +33,6 @@ import java.lang.annotation.Target;
 import java.net.URL;
 import java.util.Properties;
 
-import org.apache.maven.shared.utils.io.IOUtil;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
@@ -166,23 +165,15 @@ public class PropertyUtilsTest
     @SuppressWarnings( "deprecation" )
     public void loadValidFile() throws IOException
     {
-        OutputStream out = null;
-        try
+        File valid = tempFolder.newFile( "valid" );
+        Properties value = new Properties();
+        value.setProperty( "a", "b" );
+        try ( OutputStream out = new FileOutputStream( valid ) )
         {
-            File valid = tempFolder.newFile( "valid" );
-            Properties value = new Properties();
-            value.setProperty( "a", "b" );
-            out = new FileOutputStream( valid );
             value.store( out, "a test" );
-            out.close();
-            out = null;
             assertThat( PropertyUtils.loadProperties( valid ), is( value ) );
             assertThat( PropertyUtils.loadOptionalProperties( valid ), is( value ) );
         }
-        finally
-        {
-            IOUtil.close( out );
-        }
     }
 
     @Test
@@ -190,22 +181,14 @@ public class PropertyUtilsTest
     @SuppressWarnings( "deprecation" )
     public void loadValidURL() throws IOException
     {
-        OutputStream out = null;
-        try
-        {
-            File valid = tempFolder.newFile( "valid" );
-            Properties value = new Properties();
-            value.setProperty( "a", "b" );
-            out = new FileOutputStream( valid );
-            value.store( out, "a test" );
-            out.close();
-            out = null;
-            assertThat( PropertyUtils.loadProperties( valid.toURI().toURL() ), is( value ) );
-            assertThat( PropertyUtils.loadOptionalProperties( valid.toURI().toURL() ), is( value ) );
-        }
-        finally
+        File valid = tempFolder.newFile( "valid" );
+        Properties value = new Properties();
+        value.setProperty( "a", "b" );
+        try ( OutputStream out = new FileOutputStream( valid ) )
         {
-            IOUtil.close( out );
+          value.store( out, "a test" );
+          assertThat( PropertyUtils.loadProperties( valid.toURI().toURL() ), is( value ) );
+          assertThat( PropertyUtils.loadOptionalProperties( valid.toURI().toURL() ), is( value ) );
         }
     }