jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject [jmeter] branch master updated: Use try-with-resource to silence sonar and make code simpler
Date Tue, 01 Sep 2020 18:15:49 GMT
This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new b34ba26  Use try-with-resource to silence sonar and make code simpler
b34ba26 is described below

commit b34ba267bc2262c529ee6b1fa04c50e85fbc065f
Author: Felix Schumacher <felix.schumacher@internetallee.de>
AuthorDate: Tue Sep 1 20:15:30 2020 +0200

    Use try-with-resource to silence sonar and make code simpler
---
 .../java/org/apache/jmeter/util/JMeterUtils.java   | 32 +++++++---------------
 1 file changed, 10 insertions(+), 22 deletions(-)

diff --git a/src/core/src/main/java/org/apache/jmeter/util/JMeterUtils.java b/src/core/src/main/java/org/apache/jmeter/util/JMeterUtils.java
index 0e350a2..f9de829 100644
--- a/src/core/src/main/java/org/apache/jmeter/util/JMeterUtils.java
+++ b/src/core/src/main/java/org/apache/jmeter/util/JMeterUtils.java
@@ -189,15 +189,11 @@ public class JMeterUtils implements UnitTestManager {
      */
     public static void loadJMeterProperties(String file) {
         Properties p = new Properties(System.getProperties());
-        InputStream is = null;
-        try {
-            File f = new File(file);
-            is = new FileInputStream(f);
+        try (InputStream is = new FileInputStream(new File(file))) {
             p.load(is);
         } catch (IOException e) {
-            try {
-                is = ClassLoader.getSystemResourceAsStream(
-                        "org/apache/jmeter/jmeter.properties"); // $NON-NLS-1$
+            try (InputStream is = ClassLoader.getSystemResourceAsStream(
+                        "org/apache/jmeter/jmeter.properties")) { // $NON-NLS-1$
                 if (is == null) {
                     throw new RuntimeException("Could not read JMeter properties file:" +
file);
                 }
@@ -205,8 +201,6 @@ public class JMeterUtils implements UnitTestManager {
             } catch (IOException ex) {
                 throw new RuntimeException("Could not read JMeter properties file:" + file);
             }
-        } finally {
-            JOrphanUtils.closeQuietly(is);
         }
         appProperties = p;
     }
@@ -234,19 +228,15 @@ public class JMeterUtils implements UnitTestManager {
      */
     public static Properties loadProperties(String file, Properties defaultProps) {
         Properties p = new Properties(defaultProps);
-        InputStream is = null;
-        try {
-            File f = new File(file);
-            is = new FileInputStream(f);
+        try (InputStream is = new FileInputStream(new File(file))) {
             p.load(is);
         } catch (IOException e) {
-            try {
-                final URL resource = JMeterUtils.class.getClassLoader().getResource(file);
-                if (resource == null) {
-                    log.warn("Cannot find {}", file);
-                    return defaultProps;
-                }
-                is = resource.openStream();
+            final URL resource = JMeterUtils.class.getClassLoader().getResource(file);
+            if (resource == null) {
+                log.warn("Cannot find {}", file);
+                return defaultProps;
+            }
+            try (InputStream is = resource.openStream()) {
                 if (is == null) {
                     log.warn("Cannot open {}", file);
                     return defaultProps;
@@ -256,8 +246,6 @@ public class JMeterUtils implements UnitTestManager {
                 log.warn("Error reading {} {}", file, ex.toString());
                 return defaultProps;
             }
-        } finally {
-            JOrphanUtils.closeQuietly(is);
         }
         return p;
     }


Mime
View raw message