changeset 51762:c36ca9d88f54

8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE Reviewed-by: alanb
author redestad
date Fri, 13 Jul 2018 18:39:11 +0200
parents 73c769e0486a
children 4447720200f2
files src/java.base/share/classes/java/nio/Bits.java src/java.base/share/classes/java/nio/Buffer.java src/java.base/share/classes/jdk/internal/misc/SharedSecrets.java
diffstat 3 files changed, 41 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/nio/Bits.java	Thu Jul 12 22:53:35 2018 -0700
+++ b/src/java.base/share/classes/java/nio/Bits.java	Fri Jul 13 18:39:11 2018 +0200
@@ -207,43 +207,24 @@
         assert cnt >= 0 && reservedMem >= 0 && totalCap >= 0;
     }
 
-    // -- Monitoring of direct buffer usage --
-
-    static {
-        // setup access to this package in SharedSecrets
-        SharedSecrets.setJavaNioAccess(
-            new JavaNioAccess() {
-                @Override
-                public JavaNioAccess.BufferPool getDirectBufferPool() {
-                    return new JavaNioAccess.BufferPool() {
-                        @Override
-                        public String getName() {
-                            return "direct";
-                        }
-                        @Override
-                        public long getCount() {
-                            return Bits.COUNT.get();
-                        }
-                        @Override
-                        public long getTotalCapacity() {
-                            return Bits.TOTAL_CAPACITY.get();
-                        }
-                        @Override
-                        public long getMemoryUsed() {
-                            return Bits.RESERVED_MEMORY.get();
-                        }
-                    };
-                }
-                @Override
-                public ByteBuffer newDirectByteBuffer(long addr, int cap, Object ob) {
-                    return new DirectByteBuffer(addr, cap, ob);
-                }
-                @Override
-                public void truncate(Buffer buf) {
-                    buf.truncate();
-                }
-        });
-    }
+    static final JavaNioAccess.BufferPool BUFFER_POOL = new JavaNioAccess.BufferPool() {
+        @Override
+        public String getName() {
+            return "direct";
+        }
+        @Override
+        public long getCount() {
+            return Bits.COUNT.get();
+        }
+        @Override
+        public long getTotalCapacity() {
+            return Bits.TOTAL_CAPACITY.get();
+        }
+        @Override
+        public long getMemoryUsed() {
+            return Bits.RESERVED_MEMORY.get();
+        }
+    };
 
     // These numbers represent the point at which we have empirically
     // determined that the average cost of a JNI call exceeds the expense
--- a/src/java.base/share/classes/java/nio/Buffer.java	Thu Jul 12 22:53:35 2018 -0700
+++ b/src/java.base/share/classes/java/nio/Buffer.java	Fri Jul 13 18:39:11 2018 +0200
@@ -26,6 +26,8 @@
 package java.nio;
 
 import jdk.internal.HotSpotIntrinsicCandidate;
+import jdk.internal.misc.JavaNioAccess;
+import jdk.internal.misc.SharedSecrets;
 import jdk.internal.misc.Unsafe;
 
 import java.util.Spliterator;
@@ -707,4 +709,23 @@
             throw new IndexOutOfBoundsException();
     }
 
+    static {
+        // setup access to this package in SharedSecrets
+        SharedSecrets.setJavaNioAccess(
+            new JavaNioAccess() {
+                @Override
+                public JavaNioAccess.BufferPool getDirectBufferPool() {
+                    return Bits.BUFFER_POOL;
+                }
+                @Override
+                public ByteBuffer newDirectByteBuffer(long addr, int cap, Object ob) {
+                    return new DirectByteBuffer(addr, cap, ob);
+                }
+                @Override
+                public void truncate(Buffer buf) {
+                    buf.truncate();
+                }
+            });
+    }
+
 }
--- a/src/java.base/share/classes/jdk/internal/misc/SharedSecrets.java	Thu Jul 12 22:53:35 2018 -0700
+++ b/src/java.base/share/classes/jdk/internal/misc/SharedSecrets.java	Fri Jul 13 18:39:11 2018 +0200
@@ -194,10 +194,9 @@
 
     public static JavaNioAccess getJavaNioAccess() {
         if (javaNioAccess == null) {
-            // Ensure java.nio.ByteOrder is initialized; we know that
-            // this class initializes java.nio.Bits that provides the
+            // Ensure java.nio.Buffer is initialized, which provides the
             // shared secret.
-            unsafe.ensureClassInitialized(java.nio.ByteOrder.class);
+            unsafe.ensureClassInitialized(java.nio.Buffer.class);
         }
         return javaNioAccess;
     }