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 14:44:36 UTC
[maven-shared-utils] branch master updated: [MSHARED-860] deprecate
PropertyUtils constructor and clean up docs (#41)
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git
The following commit(s) were added to refs/heads/master by this push:
new a51b3eb [MSHARED-860] deprecate PropertyUtils constructor and clean up docs (#41)
a51b3eb is described below
commit a51b3eb7fa7fce0b635444f24b401de235f818f6
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Fri May 29 10:43:08 2020 -0400
[MSHARED-860] deprecate PropertyUtils constructor and clean up docs (#41)
* deprecate PropertyUtils constructor and clean up docs
* deprecate PathUtils constructor
* warn about closing streams
* close streams we create
---
.../org/apache/maven/shared/utils/PathTool.java | 11 ++++
.../apache/maven/shared/utils/PropertyUtils.java | 62 ++++++++++------------
.../maven/shared/utils/PropertyUtilsTest.java | 39 ++++----------
3 files changed, 50 insertions(+), 62 deletions(-)
diff --git a/src/main/java/org/apache/maven/shared/utils/PathTool.java b/src/main/java/org/apache/maven/shared/utils/PathTool.java
index b616211..28bc308 100644
--- a/src/main/java/org/apache/maven/shared/utils/PathTool.java
+++ b/src/main/java/org/apache/maven/shared/utils/PathTool.java
@@ -35,6 +35,17 @@ import javax.annotation.Nullable;
*/
public class PathTool
{
+
+ /**
+ * The constructor.
+ *
+ * @deprecated This is a utility class with only static methods. Don't create instances of it.
+ */
+ @Deprecated
+ public PathTool()
+ {
+ }
+
/**
* Determines the relative path of a filename from a base directory.
* This method is useful in building relative links within pages of
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..70e21ed 100644
--- a/src/main/java/org/apache/maven/shared/utils/PropertyUtils.java
+++ b/src/main/java/org/apache/maven/shared/utils/PropertyUtils.java
@@ -29,35 +29,35 @@ 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}.
*/
@Deprecated
public static java.util.Properties loadProperties( @Nonnull URL url )
{
- try
+ try ( InputStream in = url.openStream() )
{
- return loadProperties( url.openStream() );
+ return loadProperties( in );
}
catch ( Exception e )
{
@@ -67,18 +67,18 @@ 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}.
*/
@Deprecated
public static Properties loadProperties( @Nonnull File file )
{
- try
+ try ( InputStream in = new FileInputStream( file ) )
{
- return loadProperties( new FileInputStream( file ) );
+ return loadProperties( in );
}
catch ( Exception e )
{
@@ -88,9 +88,13 @@ public class PropertyUtils
}
/**
+ * Loads {@code Properties} from an {@code InputStream} and closes the stream.
+ * In a future release, this will no longer close the stream, so callers
+ * should close the stream themselves.
+ *
* @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 +102,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 +120,6 @@ public class PropertyUtils
{
// ignore
}
- finally
- {
- IOUtil.close( is );
- }
return null;
}
@@ -154,7 +153,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,11 +184,10 @@ public class PropertyUtils
}
/**
- * Loads {@code Properties} from a given {@code InputStream}.
- * <p>
+ * Loads {@code Properties} from an {@code InputStream} and closes the stream.
* If the given {@code InputStream} is {@code null} or the properties can't be read, an empty properties object is
- * returned.
- * </p>
+ * returned. In a future release, this will no longer close the stream, so callers
+ * should close the stream themselves.
*
* @param inputStream the properties resource to load or {@code null}
* @return the loaded properties or an empty {@code Properties} instance if properties fail to load
@@ -203,18 +201,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 ) );
}
}