From d154aa09b126cd8f8c8f35ed16a1379b00bc5a00 Mon Sep 17 00:00:00 2001 From: redestad Date: Fri, 13 Jul 2018 18:39:11 +0200 Subject: [PATCH] 8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE Reviewed-by: alanb --- .../share/classes/java/nio/Bits.java | 55 ++++++------------- .../share/classes/java/nio/Buffer.java | 21 +++++++ .../jdk/internal/misc/SharedSecrets.java | 5 +- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/src/java.base/share/classes/java/nio/Bits.java b/src/java.base/share/classes/java/nio/Bits.java index f96d2f48ba..1babb85f47 100644 --- a/src/java.base/share/classes/java/nio/Bits.java +++ b/src/java.base/share/classes/java/nio/Bits.java @@ -207,43 +207,24 @@ class Bits { // package-private 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 diff --git a/src/java.base/share/classes/java/nio/Buffer.java b/src/java.base/share/classes/java/nio/Buffer.java index 8741108b96..1e04467b6b 100644 --- a/src/java.base/share/classes/java/nio/Buffer.java +++ b/src/java.base/share/classes/java/nio/Buffer.java @@ -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 @@ public abstract class Buffer { 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(); + } + }); + } + } diff --git a/src/java.base/share/classes/jdk/internal/misc/SharedSecrets.java b/src/java.base/share/classes/jdk/internal/misc/SharedSecrets.java index 04439fba71..e44e9e7007 100644 --- a/src/java.base/share/classes/jdk/internal/misc/SharedSecrets.java +++ b/src/java.base/share/classes/jdk/internal/misc/SharedSecrets.java @@ -194,10 +194,9 @@ public class SharedSecrets { 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; } -- GitLab