8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
authorredestad
Thu, 26 Apr 2018 17:14:04 +0200
changeset 49900 770679787db5
parent 49899 b99fcb855107
child 49901 794325b73468
child 56489 016b77c3734a
8202324: Avoid loading FileInput-/OutputStream$AltFinalizer Reviewed-by: alanb
src/java.base/share/classes/java/io/FileInputStream.java
src/java.base/share/classes/java/io/FileOutputStream.java
--- a/src/java.base/share/classes/java/io/FileInputStream.java	Thu Apr 26 09:04:09 2018 -0400
+++ b/src/java.base/share/classes/java/io/FileInputStream.java	Thu Apr 26 17:14:04 2018 +0200
@@ -79,7 +79,7 @@
 
     private volatile boolean closed;
 
-    private final AltFinalizer altFinalizer;
+    private final Object altFinalizer;
 
     /**
      * Creates a <code>FileInputStream</code> by
@@ -155,7 +155,7 @@
         fd.attach(this);
         path = name;
         open(name);
-        altFinalizer = AltFinalizer.get(this);
+        altFinalizer = getFinalizer(this);
         if (altFinalizer == null) {
             FileCleanable.register(fd);       // open set the fd, register the cleanup
         }
@@ -471,6 +471,23 @@
     protected void finalize() throws IOException {
     }
 
+    /*
+     * Returns a finalizer object if the FIS needs a finalizer; otherwise null.
+     * If the FIS has a close method; it needs an AltFinalizer.
+     */
+    private static Object getFinalizer(FileInputStream fis) {
+        Class<?> clazz = fis.getClass();
+        while (clazz != FileInputStream.class) {
+            try {
+                clazz.getDeclaredMethod("close");
+                return new AltFinalizer(fis);
+            } catch (NoSuchMethodException nsme) {
+                // ignore
+            }
+            clazz = clazz.getSuperclass();
+        }
+        return null;
+    }
     /**
      * Class to call {@code FileInputStream.close} when finalized.
      * If finalization of the stream is needed, an instance is created
@@ -481,25 +498,7 @@
     static class AltFinalizer {
         private final FileInputStream fis;
 
-        /*
-         * Returns a finalizer object if the FIS needs a finalizer; otherwise null.
-         * If the FIS has a close method; it needs an AltFinalizer.
-         */
-        static AltFinalizer get(FileInputStream fis) {
-            Class<?> clazz = fis.getClass();
-            while (clazz != FileInputStream.class) {
-                try {
-                    clazz.getDeclaredMethod("close");
-                    return new AltFinalizer(fis);
-                } catch (NoSuchMethodException nsme) {
-                    // ignore
-                }
-                clazz = clazz.getSuperclass();
-            }
-            return null;
-        }
-
-        private AltFinalizer(FileInputStream fis) {
+        AltFinalizer(FileInputStream fis) {
             this.fis = fis;
         }
 
--- a/src/java.base/share/classes/java/io/FileOutputStream.java	Thu Apr 26 09:04:09 2018 -0400
+++ b/src/java.base/share/classes/java/io/FileOutputStream.java	Thu Apr 26 17:14:04 2018 +0200
@@ -95,7 +95,7 @@
 
     private volatile boolean closed;
 
-    private final AltFinalizer altFinalizer;
+    private final Object altFinalizer;
 
     /**
      * Creates a file output stream to write to the file with the
@@ -235,7 +235,7 @@
         this.path = name;
 
         open(name, append);
-        altFinalizer = AltFinalizer.get(this);
+        altFinalizer = getFinalizer(this);
         if (altFinalizer == null) {
             FileCleanable.register(fd);   // open sets the fd, register the cleanup
         }
@@ -496,6 +496,24 @@
         initIDs();
     }
 
+    /*
+     * Returns a finalizer object if the FOS needs a finalizer; otherwise null.
+     * If the FOS has a close method; it needs an AltFinalizer.
+     */
+    private static Object getFinalizer(FileOutputStream fos) {
+        Class<?> clazz = fos.getClass();
+        while (clazz != FileOutputStream.class) {
+            try {
+                clazz.getDeclaredMethod("close");
+                return new AltFinalizer(fos);
+            } catch (NoSuchMethodException nsme) {
+                // ignore
+            }
+            clazz = clazz.getSuperclass();
+        }
+        return null;
+    }
+
     /**
      * Class to call {@code FileOutputStream.close} when finalized.
      * If finalization of the stream is needed, an instance is created
@@ -506,25 +524,7 @@
     static class AltFinalizer {
         private final FileOutputStream fos;
 
-        /*
-         * Returns a finalizer object if the FOS needs a finalizer; otherwise null.
-         * If the FOS has a close method; it needs an AltFinalizer.
-         */
-        static AltFinalizer get(FileOutputStream fos) {
-            Class<?> clazz = fos.getClass();
-            while (clazz != FileOutputStream.class) {
-                try {
-                    clazz.getDeclaredMethod("close");
-                    return new AltFinalizer(fos);
-                } catch (NoSuchMethodException nsme) {
-                    // ignore
-                }
-                clazz = clazz.getSuperclass();
-            }
-            return null;
-        }
-
-        private AltFinalizer(FileOutputStream fos) {
+        AltFinalizer(FileOutputStream fos) {
             this.fos = fos;
         }