sashapolo commented on a change in pull request #172:
URL: https://github.com/apache/ignite-3/pull/172#discussion_r649844012



##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
##########
@@ -100,32 +103,35 @@
     /** {@link java.nio.Buffer#address} field offset. */
     private static final long DIRECT_BUF_ADDR_OFF = bufferAddressOffset();
 
+    /** Null object. */
+    private static final Object NULL_OBJ = null;

Review comment:
       are you sure this constant is really needed? It's only used in one 
place...

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
##########
@@ -191,57 +197,59 @@ private GridUnsafe() {
      * @return Byte buffer wrapping the given memory.
      */
     public static ByteBuffer wrapPointer(long ptr, int len) {
-        if (NEW_DIRECT_BUF_MTD != null && JAVA_NIO_ACCESS_OBJ != null)
-            return wrapPointerJavaNio(ptr, len, NEW_DIRECT_BUF_MTD, 
JAVA_NIO_ACCESS_OBJ);
+        if (NEW_DIRECT_BUF_MH != null && JAVA_NIO_ACCESS_OBJ != null)
+            return wrapPointerJavaNio(ptr, len, NEW_DIRECT_BUF_MH, 
JAVA_NIO_ACCESS_OBJ);
         else if (NEW_DIRECT_BUF_CONSTRUCTOR != null)
             return wrapPointerDirectBufferConstructor(ptr, len, 
NEW_DIRECT_BUF_CONSTRUCTOR);
         else
             throw new RuntimeException("All alternatives for a new 
DirectByteBuffer() creation failed: " + FeatureChecker.JAVA_VER_SPECIFIC_WARN);
     }
 
     /**
-     * Wraps a pointer to unmanaged memory with a direct byte buffer using the 
direct byte buffer's constructor.
+     * Wraps a pointer to unmanaged memory into direct byte buffer. Uses 
constructor of a direct byte buffer.

Review comment:
       ```suggestion
        * Wraps a pointer to unmanaged memory into a direct byte buffer. Uses 
the constructor of the direct byte buffer.
   ```

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
##########
@@ -191,57 +197,59 @@ private GridUnsafe() {
      * @return Byte buffer wrapping the given memory.
      */
     public static ByteBuffer wrapPointer(long ptr, int len) {
-        if (NEW_DIRECT_BUF_MTD != null && JAVA_NIO_ACCESS_OBJ != null)
-            return wrapPointerJavaNio(ptr, len, NEW_DIRECT_BUF_MTD, 
JAVA_NIO_ACCESS_OBJ);
+        if (NEW_DIRECT_BUF_MH != null && JAVA_NIO_ACCESS_OBJ != null)
+            return wrapPointerJavaNio(ptr, len, NEW_DIRECT_BUF_MH, 
JAVA_NIO_ACCESS_OBJ);
         else if (NEW_DIRECT_BUF_CONSTRUCTOR != null)
             return wrapPointerDirectBufferConstructor(ptr, len, 
NEW_DIRECT_BUF_CONSTRUCTOR);
         else
             throw new RuntimeException("All alternatives for a new 
DirectByteBuffer() creation failed: " + FeatureChecker.JAVA_VER_SPECIFIC_WARN);
     }
 
     /**
-     * Wraps a pointer to unmanaged memory with a direct byte buffer using the 
direct byte buffer's constructor.
+     * Wraps a pointer to unmanaged memory into direct byte buffer. Uses 
constructor of a direct byte buffer.
      *
      * @param ptr Pointer to wrap.
      * @param len Memory location length.
      * @param constructor Constructor to use. Should create an instance of a 
direct ByteBuffer.
      * @return Byte buffer wrapping the given memory.
      */
-    @NotNull private static ByteBuffer wrapPointerDirectBufferConstructor(long 
ptr, int len, Constructor<?> constructor) {
+    @NotNull private static ByteBuffer wrapPointerDirectBufferConstructor(long 
ptr, int len, MethodHandle constructor) {
         try {
-            Object newDirectBuf = constructor.newInstance(ptr, len);
+            ByteBuffer newDirectBuf = (ByteBuffer)constructor.invokeExact(ptr, 
len);
 
-            return ((ByteBuffer)newDirectBuf).order(NATIVE_BYTE_ORDER);
+            return newDirectBuf.order(NATIVE_BYTE_ORDER);
         }
-        catch (ReflectiveOperationException e) {
+        catch (Throwable e) {
             throw new RuntimeException("DirectByteBuffer#constructor is 
unavailable."
                 + FeatureChecker.JAVA_VER_SPECIFIC_WARN, e);
         }
     }
 
     /**
-     * Wraps a pointer to unmanaged memory with a direct byte buffer using a 
JavaNioAccess object.
+     * Wraps a pointer to unmanaged memory into direct byte buffer. Uses 
JavaNioAccess object.

Review comment:
       ```suggestion
        * Wraps a pointer to unmanaged memory into a direct byte buffer. Uses 
the JavaNioAccess object.
   ```

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
##########
@@ -1587,17 +1595,31 @@ private static Object javaNioAccessObject() {
      * @return Reference to {@code JavaNioAccess.newDirectByteBuffer} method
      * @throws RuntimeException If getting access to the private API is failed.
      */
-    private static Method newDirectBufferMethod(Object nioAccessObj) {
+    private static MethodHandle newDirectBufferMethodHandle(Object 
nioAccessObj) {
         try {
             Class<?> cls = nioAccessObj.getClass();
 
             Method mtd = cls.getMethod("newDirectByteBuffer", long.class, 
int.class, Object.class);
 
-            mtd.setAccessible(true);
+            AccessController.doPrivileged((PrivilegedExceptionAction<?>)() -> {
+                mtd.setAccessible(true);
+
+                return null;
+            });
 
-            return mtd;
+            MethodType mtdType = MethodType.methodType(
+                ByteBuffer.class,
+                Object.class,
+                long.class,
+                int.class,
+                Object.class
+            );
+
+            return MethodHandles.lookup()
+                .unreflect(mtd)
+                .asType(mtdType);

Review comment:
       why is this line needed?

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
##########
@@ -1587,17 +1595,31 @@ private static Object javaNioAccessObject() {
      * @return Reference to {@code JavaNioAccess.newDirectByteBuffer} method
      * @throws RuntimeException If getting access to the private API is failed.
      */
-    private static Method newDirectBufferMethod(Object nioAccessObj) {
+    private static MethodHandle newDirectBufferMethodHandle(Object 
nioAccessObj) {
         try {
             Class<?> cls = nioAccessObj.getClass();
 
             Method mtd = cls.getMethod("newDirectByteBuffer", long.class, 
int.class, Object.class);
 
-            mtd.setAccessible(true);
+            AccessController.doPrivileged((PrivilegedExceptionAction<?>)() -> {

Review comment:
       why is this needed now?

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
##########
@@ -100,32 +103,35 @@
     /** {@link java.nio.Buffer#address} field offset. */
     private static final long DIRECT_BUF_ADDR_OFF = bufferAddressOffset();
 
+    /** Null object. */
+    private static final Object NULL_OBJ = null;
+
     /** JavaNioAccess object. If {@code null} then {@link 
#NEW_DIRECT_BUF_CONSTRUCTOR} should be available. */
     @Nullable private static final Object JAVA_NIO_ACCESS_OBJ;
 
     /**
-     * JavaNioAccess#newDirectByteBuffer method. Ususally {@code null} if 
{@link #JAVA_NIO_ACCESS_OBJ} is {@code null}.
-     * If {@code null} then {@link #NEW_DIRECT_BUF_CONSTRUCTOR} should be 
available.
+     * JavaNioAccess#newDirectByteBuffer method handle. Ususally {@code null} 
if {@link #JAVA_NIO_ACCESS_OBJ} is

Review comment:
       ```suggestion
        * JavaNioAccess#newDirectByteBuffer method handle. Usually {@code null} 
if {@link #JAVA_NIO_ACCESS_OBJ} is
   ```

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
##########
@@ -100,32 +103,35 @@
     /** {@link java.nio.Buffer#address} field offset. */
     private static final long DIRECT_BUF_ADDR_OFF = bufferAddressOffset();
 
+    /** Null object. */
+    private static final Object NULL_OBJ = null;

Review comment:
       This class is called `GridUnsafe` and uses a ton of cryptic stuff, are 
you sure its the `(Object)null` that will confuse them?)))

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
##########
@@ -100,32 +103,35 @@
     /** {@link java.nio.Buffer#address} field offset. */
     private static final long DIRECT_BUF_ADDR_OFF = bufferAddressOffset();
 
+    /** Null object. */
+    private static final Object NULL_OBJ = null;

Review comment:
       This class is called `GridUnsafe` and uses a ton of cryptic stuff, are 
you sure its the `(Object)null` that will confuse them?))) But ok, this is a 
matter of taste

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
##########
@@ -1587,17 +1595,31 @@ private static Object javaNioAccessObject() {
      * @return Reference to {@code JavaNioAccess.newDirectByteBuffer} method
      * @throws RuntimeException If getting access to the private API is failed.
      */
-    private static Method newDirectBufferMethod(Object nioAccessObj) {
+    private static MethodHandle newDirectBufferMethodHandle(Object 
nioAccessObj) {
         try {
             Class<?> cls = nioAccessObj.getClass();
 
             Method mtd = cls.getMethod("newDirectByteBuffer", long.class, 
int.class, Object.class);
 
-            mtd.setAccessible(true);
+            AccessController.doPrivileged((PrivilegedExceptionAction<?>)() -> {
+                mtd.setAccessible(true);
+
+                return null;
+            });
 
-            return mtd;
+            MethodType mtdType = MethodType.methodType(
+                ByteBuffer.class,
+                Object.class,
+                long.class,
+                int.class,
+                Object.class
+            );
+
+            return MethodHandles.lookup()
+                .unreflect(mtd)
+                .asType(mtdType);

Review comment:
       > You mean "asType"?
   
   Yes
   
   > For "invokeExact" to work properly
   
   Can you clarify please?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to