You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by gi...@apache.org on 2018/06/09 18:16:54 UTC

[2/2] ant-ivyde git commit: Use try with resources

Use try with resources

Project: http://git-wip-us.apache.org/repos/asf/ant-ivyde/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant-ivyde/commit/f16c20ba
Tree: http://git-wip-us.apache.org/repos/asf/ant-ivyde/tree/f16c20ba
Diff: http://git-wip-us.apache.org/repos/asf/ant-ivyde/diff/f16c20ba

Branch: refs/heads/master
Commit: f16c20ba410794da729bab36cd42786887de1219
Parents: 4490bc3
Author: Gintas Grigelionis <gi...@apache.org>
Authored: Sat Jun 9 20:16:34 2018 +0200
Committer: Gintas Grigelionis <gi...@apache.org>
Committed: Sat Jun 9 20:16:34 2018 +0200

----------------------------------------------------------------------
 .../cpcontainer/IvyAttachmentManager.java       | 26 ++-----------
 .../IvyClasspathContainerSerializer.java        | 40 ++++++--------------
 .../eclipse/retrieve/RetrieveSetupManager.java  | 23 +++--------
 3 files changed, 20 insertions(+), 69 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant-ivyde/blob/f16c20ba/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/cpcontainer/IvyAttachmentManager.java
----------------------------------------------------------------------
diff --git a/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/cpcontainer/IvyAttachmentManager.java b/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/cpcontainer/IvyAttachmentManager.java
index 0fac246..107d5dd 100644
--- a/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/cpcontainer/IvyAttachmentManager.java
+++ b/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/cpcontainer/IvyAttachmentManager.java
@@ -54,17 +54,8 @@ public class IvyAttachmentManager {
             return;
         }
         IvyDEMessage.verbose("Reading attachment properties");
-        try {
-            FileInputStream in = new FileInputStream(containersAttachmentFile);
-            try {
-                prop.load(in);
-            } finally {
-                try {
-                    in.close();
-                } catch (IOException e) {
-                    // don't care
-                }
-            }
+        try (FileInputStream in = new FileInputStream(containersAttachmentFile)) {
+            prop.load(in);
         } catch (IOException ioe) {
             IvyPlugin.logWarn("IvyDE attachment properties could not be loaded", ioe);
         }
@@ -127,17 +118,8 @@ public class IvyAttachmentManager {
 
         // store the global result
         IvyDEMessage.verbose("Saving attachment properties");
-        try {
-            FileOutputStream out = new FileOutputStream(containersAttachmentFile);
-            try {
-                prop.store(out, "");
-            } finally {
-                try {
-                    out.close();
-                } catch (IOException e) {
-                    // don't care
-                }
-            }
+        try (FileOutputStream out = new FileOutputStream(containersAttachmentFile)) {
+            prop.store(out, "");
         } catch (IOException ioe) {
             IvyPlugin.logWarn("IvyDE attachment properties could not be saved", ioe);
         }

http://git-wip-us.apache.org/repos/asf/ant-ivyde/blob/f16c20ba/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/cpcontainer/IvyClasspathContainerSerializer.java
----------------------------------------------------------------------
diff --git a/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/cpcontainer/IvyClasspathContainerSerializer.java b/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/cpcontainer/IvyClasspathContainerSerializer.java
index 20ab071..62b0575 100644
--- a/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/cpcontainer/IvyClasspathContainerSerializer.java
+++ b/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/cpcontainer/IvyClasspathContainerSerializer.java
@@ -103,21 +103,12 @@ public class IvyClasspathContainerSerializer {
     public void save(IJavaProject project) {
         List<IvyClasspathContainer> containers = IvyClasspathContainerHelper
                 .getContainers(project);
-        try {
-            File file = new File(containersStateDir, project.getProject().getName() + ".xml");
-            IvyDEMessage.verbose("Saving the state of the containers of the project "
-                    + project.getProject().getName() + " into " + file);
-            FileOutputStream out = new FileOutputStream(file);
-            try {
-                write(out, containers);
-            } finally {
-                try {
-                    out.close();
-                } catch (IOException e) {
-                    // don't care
-                }
-            }
-        } catch (Exception ioe) {
+        File file = new File(containersStateDir, project.getProject().getName() + ".xml");
+        IvyDEMessage.verbose("Saving the state of the containers of the project "
+                + project.getProject().getName() + " into " + file);
+        try (FileOutputStream out = new FileOutputStream(file)) {
+            write(out, containers);
+        } catch (IOException ioe) {
             IvyPlugin.logWarn("IvyDE container states of the project "
                     + project.getProject().getName() + " could not be saved", ioe);
         }
@@ -132,18 +123,9 @@ public class IvyClasspathContainerSerializer {
                     + project.getProject().getName() + " doesn't exist.");
             return null;
         }
-        try {
-            FileInputStream in = new FileInputStream(file);
-            try {
-                return read(in);
-            } finally {
-                try {
-                    in.close();
-                } catch (IOException e) {
-                    // don't care
-                }
-            }
-        } catch (Exception ioe) {
+        try (FileInputStream in = new FileInputStream(file)) {
+            return read(in);
+        } catch (IOException | SAXException ioe) {
             IvyPlugin.logWarn("IvyDE container states of the project "
                     + project.getProject().getName() + " could not be read", ioe);
             return null;
@@ -317,8 +299,8 @@ public class IvyClasspathContainerSerializer {
                     }
                 }
 
-                IvyClasspathContainerImpl ivycp = new IvyClasspathContainerImpl(project, path, cpEntries,
-                        cpAttributes);
+                IvyClasspathContainerImpl ivycp = new IvyClasspathContainerImpl(project, path,
+                        cpEntries, cpAttributes);
                 containers.put(path, ivycp);
             }
             return containers;

http://git-wip-us.apache.org/repos/asf/ant-ivyde/blob/f16c20ba/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/retrieve/RetrieveSetupManager.java
----------------------------------------------------------------------
diff --git a/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/retrieve/RetrieveSetupManager.java b/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/retrieve/RetrieveSetupManager.java
index ee54e4c..a393db3 100644
--- a/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/retrieve/RetrieveSetupManager.java
+++ b/org.apache.ivyde.eclipse/src/java/org/apache/ivyde/internal/eclipse/retrieve/RetrieveSetupManager.java
@@ -59,35 +59,22 @@ public class RetrieveSetupManager implements ISaveParticipant {
         List<StandaloneRetrieveSetup> retrieveSetups;
 
         StandaloneRetrieveSerializer serializer = new StandaloneRetrieveSerializer();
-        ByteArrayInputStream in = new ByteArrayInputStream(retrieveSetup.getBytes());
-        try {
+        try (ByteArrayInputStream in = new ByteArrayInputStream(retrieveSetup.getBytes())) {
             retrieveSetups = serializer.read(in, project);
-        } finally {
-            try {
-                in.close();
-            } catch (IOException e) {
-                // we don't care
-            }
         }
+        // we don't care
         return retrieveSetups;
     }
 
     public void save(final IProject project, List<StandaloneRetrieveSetup> retrieveSetups)
             throws IOException {
         StandaloneRetrieveSerializer serializer = new StandaloneRetrieveSerializer();
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        try {
+        final String retrieveSetup;
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
             serializer.write(out, retrieveSetups);
-        } finally {
-            try {
-                out.close();
-            } catch (IOException e) {
-                // we don't care
-            }
+            retrieveSetup = new String(out.toByteArray());
         }
 
-        final String retrieveSetup = new String(out.toByteArray());
-
         synchronized (projectPrefs) {
             IEclipsePreferences pref = projectPrefs.get(project);
             if (pref == null) {