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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1dcab22  Replace try/finally with try-with-resources. (#1546)
1dcab22 is described below

commit 1dcab224dff2ed38ce5875c36a6a4689d75d5b7c
Author: Christopher Tubbs <ctubb...@apache.org>
AuthorDate: Thu Mar 5 11:22:56 2020 -0500

    Replace try/finally with try-with-resources. (#1546)
    
    Implement the try-with-resources construct available in Java 7+ where 
applicable.
---
 .../java/org/apache/accumulo/core/cli/ClientOpts.java    |  8 +-------
 .../accumulo/core/clientImpl/InstanceOperationsImpl.java | 12 +++---------
 .../core/clientImpl/TabletServerBatchDeleter.java        |  7 +------
 .../java/org/apache/accumulo/core/file/rfile/RFile.java  |  5 +----
 .../accumulo/core/file/rfile/bcfile/PrintInfo.java       | 10 ++--------
 .../apache/accumulo/core/util/cleaner/CleanerUtil.java   |  4 +---
 .../hadoop/its/mapred/AccumuloRowInputFormatIT.java      |  8 +-------
 .../hadoop/its/mapreduce/AccumuloRowInputFormatIT.java   |  8 +-------
 .../miniclusterImpl/MiniAccumuloClusterImpl.java         |  8 +-------
 .../apache/accumulo/server/master/LiveTServerSet.java    |  7 +------
 .../test/ThriftServerBindsBeforeZooKeeperLockIT.java     | 16 ++--------------
 .../test/mapreduce/AccumuloRowInputFormatIT.java         |  8 +-------
 12 files changed, 16 insertions(+), 85 deletions(-)

diff --git a/core/src/main/java/org/apache/accumulo/core/cli/ClientOpts.java 
b/core/src/main/java/org/apache/accumulo/core/cli/ClientOpts.java
index ce20f77..338da79 100644
--- a/core/src/main/java/org/apache/accumulo/core/cli/ClientOpts.java
+++ b/core/src/main/java/org/apache/accumulo/core/cli/ClientOpts.java
@@ -85,16 +85,10 @@ public class ClientOpts extends Help {
             justification = "app is run in same security context as user 
providing the filename")
         @Override
         String process(String value) {
-          Scanner scanner = null;
-          try {
-            scanner = new Scanner(new File(value), UTF_8);
+          try (Scanner scanner = new Scanner(new File(value), UTF_8)) {
             return scanner.nextLine();
           } catch (IOException e) {
             throw new ParameterException(e);
-          } finally {
-            if (scanner != null) {
-              scanner.close();
-            }
           }
         }
       },
diff --git 
a/core/src/main/java/org/apache/accumulo/core/clientImpl/InstanceOperationsImpl.java
 
b/core/src/main/java/org/apache/accumulo/core/clientImpl/InstanceOperationsImpl.java
index 28e7811..ac3713b 100644
--- 
a/core/src/main/java/org/apache/accumulo/core/clientImpl/InstanceOperationsImpl.java
+++ 
b/core/src/main/java/org/apache/accumulo/core/clientImpl/InstanceOperationsImpl.java
@@ -40,7 +40,6 @@ import org.apache.accumulo.core.client.admin.ActiveScan;
 import org.apache.accumulo.core.client.admin.InstanceOperations;
 import org.apache.accumulo.core.clientImpl.thrift.ConfigurationType;
 import org.apache.accumulo.core.clientImpl.thrift.ThriftSecurityException;
-import org.apache.accumulo.core.tabletserver.thrift.TabletClientService;
 import org.apache.accumulo.core.tabletserver.thrift.TabletClientService.Client;
 import org.apache.accumulo.core.trace.TraceUtil;
 import org.apache.accumulo.core.util.AddressUtil;
@@ -188,17 +187,12 @@ public class InstanceOperationsImpl implements 
InstanceOperations {
 
   @Override
   public void ping(String tserver) throws AccumuloException {
-    TTransport transport = null;
-    try {
-      transport = createTransport(AddressUtil.parseAddress(tserver, false), 
context);
-      var client = createClient(new TabletClientService.Client.Factory(), 
transport);
+    try (
+        TTransport transport = 
createTransport(AddressUtil.parseAddress(tserver, false), context)) {
+      var client = createClient(new Client.Factory(), transport);
       client.getTabletServerStatus(TraceUtil.traceInfo(), context.rpcCreds());
     } catch (TException e) {
       throw new AccumuloException(e);
-    } finally {
-      if (transport != null) {
-        transport.close();
-      }
     }
   }
 
diff --git 
a/core/src/main/java/org/apache/accumulo/core/clientImpl/TabletServerBatchDeleter.java
 
b/core/src/main/java/org/apache/accumulo/core/clientImpl/TabletServerBatchDeleter.java
index d644cf6..37606e8 100644
--- 
a/core/src/main/java/org/apache/accumulo/core/clientImpl/TabletServerBatchDeleter.java
+++ 
b/core/src/main/java/org/apache/accumulo/core/clientImpl/TabletServerBatchDeleter.java
@@ -52,9 +52,7 @@ public class TabletServerBatchDeleter extends 
TabletServerBatchReader implements
 
   @Override
   public void delete() throws MutationsRejectedException {
-    BatchWriter bw = null;
-    try {
-      bw = new BatchWriterImpl(context, tableId, bwConfig);
+    try (BatchWriter bw = new BatchWriterImpl(context, tableId, bwConfig)) {
       Iterator<Entry<Key,Value>> iter = super.iterator();
       while (iter.hasNext()) {
         Entry<Key,Value> next = iter.next();
@@ -64,9 +62,6 @@ public class TabletServerBatchDeleter extends 
TabletServerBatchReader implements
             new ColumnVisibility(k.getColumnVisibility()), k.getTimestamp());
         bw.addMutation(m);
       }
-    } finally {
-      if (bw != null)
-        bw.close();
     }
   }
 
diff --git a/core/src/main/java/org/apache/accumulo/core/file/rfile/RFile.java 
b/core/src/main/java/org/apache/accumulo/core/file/rfile/RFile.java
index 38d4c87..43770b4 100644
--- a/core/src/main/java/org/apache/accumulo/core/file/rfile/RFile.java
+++ b/core/src/main/java/org/apache/accumulo/core/file/rfile/RFile.java
@@ -1161,8 +1161,7 @@ public class RFile {
     public Reader(CachableBlockFile.Reader rdr) throws IOException {
       this.reader = rdr;
 
-      CachableBlockFile.CachedBlockRead mb = 
reader.getMetaBlock("RFile.index");
-      try {
+      try (CachableBlockFile.CachedBlockRead mb = 
reader.getMetaBlock("RFile.index")) {
         int magic = mb.readInt();
         int ver = mb.readInt();
         rfileVersion = ver;
@@ -1205,8 +1204,6 @@ public class RFile {
           samplerConfig = null;
         }
 
-      } finally {
-        mb.close();
       }
 
       lgContext = new LocalityGroupContext(currentReaders);
diff --git 
a/core/src/main/java/org/apache/accumulo/core/file/rfile/bcfile/PrintInfo.java 
b/core/src/main/java/org/apache/accumulo/core/file/rfile/bcfile/PrintInfo.java
index 59b933e..695cf33 100644
--- 
a/core/src/main/java/org/apache/accumulo/core/file/rfile/bcfile/PrintInfo.java
+++ 
b/core/src/main/java/org/apache/accumulo/core/file/rfile/bcfile/PrintInfo.java
@@ -39,10 +39,8 @@ public class PrintInfo {
   public static void printMetaBlockInfo(SiteConfiguration siteConfig, 
Configuration conf,
       FileSystem fs, Path path) throws IOException {
     FSDataInputStream fsin = fs.open(path);
-    BCFile.Reader bcfr = null;
-    try {
-      bcfr = new BCFile.Reader(fsin, fs.getFileStatus(path).getLen(), conf,
-          CryptoServiceFactory.newInstance(siteConfig, 
ClassloaderType.ACCUMULO));
+    try (BCFile.Reader bcfr = new BCFile.Reader(fsin, 
fs.getFileStatus(path).getLen(), conf,
+        CryptoServiceFactory.newInstance(siteConfig, 
ClassloaderType.ACCUMULO))) {
 
       Set<Entry<String,MetaIndexEntry>> es = bcfr.metaIndex.index.entrySet();
 
@@ -57,10 +55,6 @@ public class PrintInfo {
             "      Compression type     : " + 
entry.getValue().getCompressionAlgorithm().getName());
         out.println();
       }
-    } finally {
-      if (bcfr != null) {
-        bcfr.close();
-      }
     }
   }
 
diff --git 
a/core/src/main/java/org/apache/accumulo/core/util/cleaner/CleanerUtil.java 
b/core/src/main/java/org/apache/accumulo/core/util/cleaner/CleanerUtil.java
index b811ac8..4e79ec8 100644
--- a/core/src/main/java/org/apache/accumulo/core/util/cleaner/CleanerUtil.java
+++ b/core/src/main/java/org/apache/accumulo/core/util/cleaner/CleanerUtil.java
@@ -111,12 +111,10 @@ public class CleanerUtil {
     requireNonNull(bw);
     requireNonNull(client);
     return CLEANER.register(o, () -> {
-      try {
+      try (client) {
         bw.close();
       } catch (MutationsRejectedException e) {
         log.error("Failed to close BatchWriter; some mutations may not be 
applied", e);
-      } finally {
-        client.close();
       }
     });
   }
diff --git 
a/hadoop-mapreduce/src/test/java/org/apache/accumulo/hadoop/its/mapred/AccumuloRowInputFormatIT.java
 
b/hadoop-mapreduce/src/test/java/org/apache/accumulo/hadoop/its/mapred/AccumuloRowInputFormatIT.java
index 6a3757e..f6efc1a 100644
--- 
a/hadoop-mapreduce/src/test/java/org/apache/accumulo/hadoop/its/mapred/AccumuloRowInputFormatIT.java
+++ 
b/hadoop-mapreduce/src/test/java/org/apache/accumulo/hadoop/its/mapred/AccumuloRowInputFormatIT.java
@@ -194,16 +194,10 @@ public class AccumuloRowInputFormatIT extends 
AccumuloClusterHarness {
     try (AccumuloClient client = 
Accumulo.newClient().from(getClientProps()).build()) {
       String tableName = getUniqueNames(1)[0];
       client.tableOperations().create(tableName);
-      BatchWriter writer = null;
-      try {
-        writer = client.createBatchWriter(tableName, new BatchWriterConfig());
+      try (BatchWriter writer = client.createBatchWriter(tableName, new 
BatchWriterConfig())) {
         insertList(writer, row1);
         insertList(writer, row2);
         insertList(writer, row3);
-      } finally {
-        if (writer != null) {
-          writer.close();
-        }
       }
       MRTester.main(new String[] {tableName});
       assertNull(e1);
diff --git 
a/hadoop-mapreduce/src/test/java/org/apache/accumulo/hadoop/its/mapreduce/AccumuloRowInputFormatIT.java
 
b/hadoop-mapreduce/src/test/java/org/apache/accumulo/hadoop/its/mapreduce/AccumuloRowInputFormatIT.java
index f8e86a7..10245b2 100644
--- 
a/hadoop-mapreduce/src/test/java/org/apache/accumulo/hadoop/its/mapreduce/AccumuloRowInputFormatIT.java
+++ 
b/hadoop-mapreduce/src/test/java/org/apache/accumulo/hadoop/its/mapreduce/AccumuloRowInputFormatIT.java
@@ -189,16 +189,10 @@ public class AccumuloRowInputFormatIT extends 
AccumuloClusterHarness {
     try (AccumuloClient client = 
Accumulo.newClient().from(getClientProps()).build()) {
       String tableName = getUniqueNames(1)[0];
       client.tableOperations().create(tableName);
-      BatchWriter writer = null;
-      try {
-        writer = client.createBatchWriter(tableName, new BatchWriterConfig());
+      try (BatchWriter writer = client.createBatchWriter(tableName, new 
BatchWriterConfig())) {
         insertList(writer, row1);
         insertList(writer, row2);
         insertList(writer, row3);
-      } finally {
-        if (writer != null) {
-          writer.close();
-        }
       }
       MRTester.main(new String[] {tableName});
       assertNull(e1);
diff --git 
a/minicluster/src/main/java/org/apache/accumulo/miniclusterImpl/MiniAccumuloClusterImpl.java
 
b/minicluster/src/main/java/org/apache/accumulo/miniclusterImpl/MiniAccumuloClusterImpl.java
index 061cfea..9a6a2c7 100644
--- 
a/minicluster/src/main/java/org/apache/accumulo/miniclusterImpl/MiniAccumuloClusterImpl.java
+++ 
b/minicluster/src/main/java/org/apache/accumulo/miniclusterImpl/MiniAccumuloClusterImpl.java
@@ -519,9 +519,7 @@ public class MiniAccumuloClusterImpl implements 
AccumuloCluster {
           // sleep a little bit to let zookeeper come up before calling init, 
seems to work better
           long startTime = System.currentTimeMillis();
           while (true) {
-            Socket s = null;
-            try {
-              s = new Socket("localhost", config.getZooKeeperPort());
+            try (Socket s = new Socket("localhost", 
config.getZooKeeperPort())) {
               s.setReuseAddress(true);
               s.getOutputStream().write("ruok\n".getBytes());
               s.getOutputStream().flush();
@@ -538,10 +536,6 @@ public class MiniAccumuloClusterImpl implements 
AccumuloCluster {
               }
               // Don't spin absurdly fast
               sleepUninterruptibly(250, TimeUnit.MILLISECONDS);
-            } finally {
-              if (s != null) {
-                s.close();
-              }
             }
           }
         }
diff --git 
a/server/base/src/main/java/org/apache/accumulo/server/master/LiveTServerSet.java
 
b/server/base/src/main/java/org/apache/accumulo/server/master/LiveTServerSet.java
index 0272e87..59a4475 100644
--- 
a/server/base/src/main/java/org/apache/accumulo/server/master/LiveTServerSet.java
+++ 
b/server/base/src/main/java/org/apache/accumulo/server/master/LiveTServerSet.java
@@ -127,9 +127,7 @@ public class LiveTServerSet implements Watcher {
 
       long start = System.currentTimeMillis();
 
-      TTransport transport = ThriftUtil.createTransport(address, context);
-
-      try {
+      try (TTransport transport = ThriftUtil.createTransport(address, 
context)) {
         TabletClientService.Client client =
             ThriftUtil.createClient(new TabletClientService.Client.Factory(), 
transport);
         TabletServerStatus status =
@@ -138,9 +136,6 @@ public class LiveTServerSet implements Watcher {
           status.setResponseTime(System.currentTimeMillis() - start);
         }
         return status;
-      } finally {
-        if (transport != null)
-          transport.close();
       }
     }
 
diff --git 
a/test/src/main/java/org/apache/accumulo/test/ThriftServerBindsBeforeZooKeeperLockIT.java
 
b/test/src/main/java/org/apache/accumulo/test/ThriftServerBindsBeforeZooKeeperLockIT.java
index dfb749c..9246cc1 100644
--- 
a/test/src/main/java/org/apache/accumulo/test/ThriftServerBindsBeforeZooKeeperLockIT.java
+++ 
b/test/src/main/java/org/apache/accumulo/test/ThriftServerBindsBeforeZooKeeperLockIT.java
@@ -163,19 +163,13 @@ public class ThriftServerBindsBeforeZooKeeperLockIT 
extends AccumuloClusterHarne
           master = startProcess(cluster, ServerType.MASTER, freePort);
 
           while (true) {
-            Socket s = null;
-            try {
-              s = new Socket("localhost", freePort);
+            try (Socket s = new Socket("localhost", freePort)) {
               if (s.isConnected()) {
                 // Pass
                 return;
               }
             } catch (Exception e) {
               LOG.debug("Caught exception trying to connect to Master", e);
-            } finally {
-              if (s != null) {
-                s.close();
-              }
             }
             // Wait before trying again
             Thread.sleep(1000);
@@ -231,19 +225,13 @@ public class ThriftServerBindsBeforeZooKeeperLockIT 
extends AccumuloClusterHarne
           master = startProcess(cluster, ServerType.GARBAGE_COLLECTOR, 
freePort);
 
           while (true) {
-            Socket s = null;
-            try {
-              s = new Socket("localhost", freePort);
+            try (Socket s = new Socket("localhost", freePort)) {
               if (s.isConnected()) {
                 // Pass
                 return;
               }
             } catch (Exception e) {
               LOG.debug("Caught exception trying to connect to GC", e);
-            } finally {
-              if (s != null) {
-                s.close();
-              }
             }
             // Wait before trying again
             Thread.sleep(1000);
diff --git 
a/test/src/main/java/org/apache/accumulo/test/mapreduce/AccumuloRowInputFormatIT.java
 
b/test/src/main/java/org/apache/accumulo/test/mapreduce/AccumuloRowInputFormatIT.java
index 51fa82a..3f5ad07 100644
--- 
a/test/src/main/java/org/apache/accumulo/test/mapreduce/AccumuloRowInputFormatIT.java
+++ 
b/test/src/main/java/org/apache/accumulo/test/mapreduce/AccumuloRowInputFormatIT.java
@@ -193,16 +193,10 @@ public class AccumuloRowInputFormatIT extends 
AccumuloClusterHarness {
     try (AccumuloClient client = 
Accumulo.newClient().from(getClientProps()).build()) {
       String tableName = getUniqueNames(1)[0];
       client.tableOperations().create(tableName);
-      BatchWriter writer = null;
-      try {
-        writer = client.createBatchWriter(tableName, new BatchWriterConfig());
+      try (BatchWriter writer = client.createBatchWriter(tableName, new 
BatchWriterConfig())) {
         insertList(writer, row1);
         insertList(writer, row2);
         insertList(writer, row3);
-      } finally {
-        if (writer != null) {
-          writer.close();
-        }
       }
       MRTester.main(new String[] {tableName});
       assertNull(e1);

Reply via email to