commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1748108 - in /commons/proper/bcel/trunk/src/main/java/org/apache/bcel: classfile/ generic/ util/
Date Mon, 13 Jun 2016 07:41:48 GMT
Author: ggregory
Date: Mon Jun 13 07:41:48 2016
New Revision: 1748108

URL: http://svn.apache.org/viewvc?rev=1748108&view=rev
Log:
Use try-with-resources.

Modified:
    commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/JavaClass.java
    commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationEntryGen.java
    commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/Class2HTML.java
    commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassLoaderRepository.java
    commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassPath.java
    commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassPathRepository.java
    commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/MemorySensitiveClassPathRepository.java

Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/JavaClass.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/JavaClass.java?rev=1748108&r1=1748107&r2=1748108&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/JavaClass.java (original)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/JavaClass.java Mon Jun
13 07:41:48 2016
@@ -229,7 +229,7 @@ public class JavaClass extends AccessFla
      * @param file Output file
      * @throws IOException
      */
-    public void dump( final File file ) throws IOException {
+    public void dump(final File file) throws IOException {
         String parent = file.getParent();
         if (parent != null) {
             File dir = new File(parent);
@@ -239,14 +239,8 @@ public class JavaClass extends AccessFla
                 }
             }
         }
-        DataOutputStream dos = null;
-        try {
-            dos = new DataOutputStream(new FileOutputStream(file));
+        try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(file))) {
             dump(dos);
-        } finally {
-            if (dos != null) {
-                dos.close();
-            }
         }
     }
 

Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationEntryGen.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationEntryGen.java?rev=1748108&r1=1748107&r2=1748108&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationEntryGen.java
(original)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationEntryGen.java
Mon Jun 13 07:41:48 2016
@@ -216,24 +216,22 @@ public class AnnotationEntryGen {
 
             ByteArrayOutputStream rvaBytes = new ByteArrayOutputStream();
             ByteArrayOutputStream riaBytes = new ByteArrayOutputStream();
-            DataOutputStream rvaDos = new DataOutputStream(rvaBytes);
-            DataOutputStream riaDos = new DataOutputStream(riaBytes);
+            try (DataOutputStream rvaDos = new DataOutputStream(rvaBytes);
+                    DataOutputStream riaDos = new DataOutputStream(riaBytes)) {
 
-            rvaDos.writeShort(countVisible);
-            riaDos.writeShort(countInvisible);
+                rvaDos.writeShort(countVisible);
+                riaDos.writeShort(countInvisible);
 
-            // put the annotations in the right output stream
-            for (AnnotationEntryGen a : annotationEntryGens) {
-                if (a.isRuntimeVisible()) {
-                    a.dump(rvaDos);
-                } else {
-                    a.dump(riaDos);
+                // put the annotations in the right output stream
+                for (AnnotationEntryGen a : annotationEntryGens) {
+                    if (a.isRuntimeVisible()) {
+                        a.dump(rvaDos);
+                    } else {
+                        a.dump(riaDos);
+                    }
                 }
             }
 
-            rvaDos.close();
-            riaDos.close();
-
             byte[] rvaData = rvaBytes.toByteArray();
             byte[] riaData = riaBytes.toByteArray();
 
@@ -296,34 +294,34 @@ public class AnnotationEntryGen {
             }
             // Lets do the visible ones
             ByteArrayOutputStream rvaBytes = new ByteArrayOutputStream();
-            DataOutputStream rvaDos = new DataOutputStream(rvaBytes);
-            rvaDos.writeByte(vec.length); // First goes number of parameters
-            for (int i = 0; i < vec.length; i++) {
-                rvaDos.writeShort(visCount[i]);
-                if (visCount[i] > 0) {
-                    for (AnnotationEntryGen element : vec[i]) {
-                        if (element.isRuntimeVisible()) {
-                            element.dump(rvaDos);
+            try (DataOutputStream rvaDos = new DataOutputStream(rvaBytes)) {
+                rvaDos.writeByte(vec.length); // First goes number of parameters
+                for (int i = 0; i < vec.length; i++) {
+                    rvaDos.writeShort(visCount[i]);
+                    if (visCount[i] > 0) {
+                        for (AnnotationEntryGen element : vec[i]) {
+                            if (element.isRuntimeVisible()) {
+                                element.dump(rvaDos);
+                            }
                         }
                     }
                 }
             }
-            rvaDos.close();
             // Lets do the invisible ones
             ByteArrayOutputStream riaBytes = new ByteArrayOutputStream();
-            DataOutputStream riaDos = new DataOutputStream(riaBytes);
-            riaDos.writeByte(vec.length); // First goes number of parameters
-            for (int i = 0; i < vec.length; i++) {
-                riaDos.writeShort(invisCount[i]);
-                if (invisCount[i] > 0) {
-                    for (AnnotationEntryGen element : vec[i]) {
-                        if (!element.isRuntimeVisible()) {
-                            element.dump(riaDos);
+            try (DataOutputStream riaDos = new DataOutputStream(riaBytes)) {
+                riaDos.writeByte(vec.length); // First goes number of parameters
+                for (int i = 0; i < vec.length; i++) {
+                    riaDos.writeShort(invisCount[i]);
+                    if (invisCount[i] > 0) {
+                        for (AnnotationEntryGen element : vec[i]) {
+                            if (!element.isRuntimeVisible()) {
+                                element.dump(riaDos);
+                            }
                         }
                     }
                 }
             }
-            riaDos.close();
             byte[] rvaData = rvaBytes.toByteArray();
             byte[] riaData = riaBytes.toByteArray();
             int rvaIndex = -1;

Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/Class2HTML.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/Class2HTML.java?rev=1748108&r1=1748107&r2=1748108&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/Class2HTML.java (original)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/Class2HTML.java Mon Jun 13
07:41:48 2016
@@ -217,24 +217,19 @@ public class Class2HTML {
 
 
     private void writeMainHTML( final AttributeHTML attribute_html ) throws IOException {
-        PrintWriter file = new PrintWriter(new FileOutputStream(dir + class_name + ".html"));
+        try (PrintWriter file = new PrintWriter(new FileOutputStream(dir + class_name + ".html")))
{
+            file.println("<HTML>\n" + "<HEAD><TITLE>Documentation for "
+ class_name + "</TITLE>" + "</HEAD>\n"
+                    + "<FRAMESET BORDER=1 cols=\"30%,*\">\n" + "<FRAMESET BORDER=1
rows=\"80%,*\">\n"
+                    + "<FRAME NAME=\"ConstantPool\" SRC=\"" + class_name + "_cp.html"
+ "\"\n MARGINWIDTH=\"0\" "
+                    + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + "<FRAME
NAME=\"Attributes\" SRC=\""
+                    + class_name + "_attributes.html" + "\"\n MARGINWIDTH=\"0\" "
+                    + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + "</FRAMESET>\n"
+                    + "<FRAMESET BORDER=1 rows=\"80%,*\">\n" + "<FRAME NAME=\"Code\"
SRC=\"" + class_name
+                    + "_code.html\"\n MARGINWIDTH=0 " + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n"
+                    + "<FRAME NAME=\"Methods\" SRC=\"" + class_name + "_methods.html\"\n
MARGINWIDTH=0 "
+                    + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" + "</FRAMESET></FRAMESET></HTML>");
+        }
         Attribute[] attributes = java_class.getAttributes();
-        file.println("<HTML>\n" + "<HEAD><TITLE>Documentation for " + class_name
+ "</TITLE>"
-                + "</HEAD>\n" + "<FRAMESET BORDER=1 cols=\"30%,*\">\n"
-                + "<FRAMESET BORDER=1 rows=\"80%,*\">\n" + "<FRAME NAME=\"ConstantPool\"
SRC=\""
-                + class_name + "_cp.html" + "\"\n MARGINWIDTH=\"0\" "
-                + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n"
-                + "<FRAME NAME=\"Attributes\" SRC=\"" + class_name + "_attributes.html"
-                + "\"\n MARGINWIDTH=\"0\" "
-                + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + "</FRAMESET>\n"
-                + "<FRAMESET BORDER=1 rows=\"80%,*\">\n" + "<FRAME NAME=\"Code\"
SRC=\""
-                + class_name + "_code.html\"\n MARGINWIDTH=0 "
-                + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n"
-                + "<FRAME NAME=\"Methods\" SRC=\"" + class_name
-                + "_methods.html\"\n MARGINWIDTH=0 "
-                + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n"
-                + "</FRAMESET></FRAMESET></HTML>");
-        file.close();
         for (int i = 0; i < attributes.length; i++) {
             attribute_html.writeAttribute(attributes[i], "class" + i);
         }

Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassLoaderRepository.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassLoaderRepository.java?rev=1748108&r1=1748107&r2=1748108&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassLoaderRepository.java
(original)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassLoaderRepository.java
Mon Jun 13 07:41:48 2016
@@ -79,25 +79,20 @@ public class ClassLoaderRepository imple
      * Lookup a JavaClass object from the Class Name provided.
      */
     @Override
-    public JavaClass loadClass( final String className ) throws ClassNotFoundException {
+    public JavaClass loadClass(final String className) throws ClassNotFoundException {
         String classFile = className.replace('.', '/');
         JavaClass RC = findClass(className);
         if (RC != null) {
             return RC;
         }
-        try {
-            InputStream is = loader.getResourceAsStream(classFile + ".class");
+        try (InputStream is = loader.getResourceAsStream(classFile + ".class")) {
             if (is == null) {
                 throw new ClassNotFoundException(className + " not found.");
             }
-            try {
-                ClassParser parser = new ClassParser(is, className);
-                RC = parser.parse();
-                storeClass(RC);
-                return RC;
-            } finally {
-                is.close();
-            }
+            ClassParser parser = new ClassParser(is, className);
+            RC = parser.parse();
+            storeClass(RC);
+            return RC;
         } catch (IOException e) {
             throw new ClassNotFoundException(className + " not found: " + e, e);
         }

Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassPath.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassPath.java?rev=1748108&r1=1748107&r2=1748108&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassPath.java (original)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassPath.java Mon Jun 13
07:41:48 2016
@@ -307,10 +307,9 @@ public class ClassPath {
      * @param suffix file name ends with suffix, e.g. .java
      * @return byte array for file on class path
      */
-    public byte[] getBytes( final String name, final String suffix ) throws IOException {
+    public byte[] getBytes(final String name, final String suffix) throws IOException {
         DataInputStream dis = null;
-        try {
-            InputStream is = getInputStream(name, suffix);
+        try (InputStream is = getInputStream(name, suffix)) {
             if (is == null) {
                 throw new IOException("Couldn't find: " + name + suffix);
             }

Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassPathRepository.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassPathRepository.java?rev=1748108&r1=1748107&r2=1748108&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassPathRepository.java
(original)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/ClassPathRepository.java
Mon Jun 13 07:41:48 2016
@@ -106,28 +106,21 @@ public class ClassPathRepository impleme
      */
     @Override
     public JavaClass loadClass(final Class<?> clazz) throws ClassNotFoundException
{
-        InputStream clsStream = null;
-        try {
-            String className = clazz.getName();
-            JavaClass repositoryClass = findClass(className);
-            if (repositoryClass != null) {
-                return repositoryClass;
-            }
-            String name = className;
-            int i = name.lastIndexOf('.');
-            if (i > 0) {
-                name = name.substring(i + 1);
-            }
-            clsStream = clazz.getResourceAsStream(name + ".class");
-            return loadClass(clsStream, className);
-        } finally {
-            try {
-                if (clsStream != null) {
-                    clsStream.close();
-                }
-            } catch (IOException ioe) {
-                // don't care
-            }
+        String className = clazz.getName();
+        JavaClass repositoryClass = findClass(className);
+        if (repositoryClass != null) {
+            return repositoryClass;
+        }
+        String name = className;
+        int i = name.lastIndexOf('.');
+        if (i > 0) {
+            name = name.substring(i + 1);
+        }
+        JavaClass cls = null;
+        try (InputStream clsStream = clazz.getResourceAsStream(name + ".class")) {
+            return cls = loadClass(clsStream, className);
+        } catch (IOException e) {
+            return cls;
         }
     }
 

Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/MemorySensitiveClassPathRepository.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/MemorySensitiveClassPathRepository.java?rev=1748108&r1=1748107&r2=1748108&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/MemorySensitiveClassPathRepository.java
(original)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/util/MemorySensitiveClassPathRepository.java
Mon Jun 13 07:41:48 2016
@@ -112,29 +112,23 @@ public class MemorySensitiveClassPathRep
      */
     @Override
     public JavaClass loadClass(final Class<?> clazz) throws ClassNotFoundException
{
-        InputStream clsStream = null;
-        try {
-            String className = clazz.getName();
-            JavaClass repositoryClass = findClass(className);
-            if (repositoryClass != null) {
-                return repositoryClass;
-            }
-            String name = className;
-            int i = name.lastIndexOf('.');
-            if (i > 0) {
-                name = name.substring(i + 1);
-            }
-            clsStream = clazz.getResourceAsStream(name + ".class");
-            return loadClass(clsStream, className);
-        } finally {
-            try {
-                if (clsStream != null) {
-                    clsStream.close();
-                }
-            } catch (IOException ioe) {
-                // don't care
-            }
+        String className = clazz.getName();
+        JavaClass repositoryClass = findClass(className);
+        if (repositoryClass != null) {
+            return repositoryClass;
         }
+        String name = className;
+        int i = name.lastIndexOf('.');
+        if (i > 0) {
+            name = name.substring(i + 1);
+        }
+        JavaClass cls = null;
+        try (InputStream clsStream = clazz.getResourceAsStream(name + ".class")) {
+            return cls = loadClass(clsStream, className);
+        } catch (IOException e) {
+            return cls;
+        }
+
     }
 
     private JavaClass loadClass(final InputStream is, final String className) throws ClassNotFoundException
{



Mime
View raw message