You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/02/24 15:23:20 UTC

[GitHub] [flink] tillrohrmann commented on a change in pull request #11160: [FLINK-15094] Use Unsafe to instantiate and construct DirectByteBuffer

tillrohrmann commented on a change in pull request #11160: [FLINK-15094] Use Unsafe to instantiate and construct DirectByteBuffer
URL: https://github.com/apache/flink/pull/11160#discussion_r383184923
 
 

 ##########
 File path: flink-core/src/main/java/org/apache/flink/core/memory/MemoryUtils.java
 ##########
 @@ -60,32 +63,31 @@
 		}
 	}
 
-	/** Should not be instantiated. */
-	private MemoryUtils() {}
-
-	private static Constructor<? extends ByteBuffer> getDirectBufferPrivateConstructor() {
+	private static long getClassFieldOffset(@SuppressWarnings("SameParameterValue") Class<?> cl, String fieldName) {
+		final String errorMessage = "Could not get field '" + fieldName + "' offset in class '" + cl + "' for unsafe operations";
 		try {
-			Constructor<? extends ByteBuffer> constructor =
-				ByteBuffer.allocateDirect(1).getClass().getDeclaredConstructor(long.class, int.class);
-			constructor.setAccessible(true);
-			return constructor;
-		} catch (NoSuchMethodException e) {
-			throw new Error(
-				"The private constructor java.nio.DirectByteBuffer.<init>(long, int) is not available.",
-				e);
+			return UNSAFE.objectFieldOffset(cl.getDeclaredField(fieldName));
 		} catch (SecurityException e) {
-			throw new Error(
-				"The private constructor java.nio.DirectByteBuffer.<init>(long, int) is not available, " +
-					"permission denied by security manager",
-				e);
+			throw new Error(errorMessage + ", permission denied by security manager.", e);
+		} catch (NoSuchFieldException e) {
+			throw new Error(errorMessage, e);
 		} catch (Throwable t) {
-			throw new Error(
-				"Unclassified error while trying to access private constructor " +
-					"java.nio.DirectByteBuffer.<init>(long, int).",
-				t);
+			throw new Error(errorMessage + ", unclassified error", t);
+		}
+	}
+
+	private static Class<?> getClassByName(@SuppressWarnings("SameParameterValue") String className) {
+		//noinspection OverlyBroadCatchBlock
+		try {
+			return Class.forName(className);
+		} catch (Throwable e) {
 
 Review comment:
   Why don't we only catch `ClassNotFoundException` here?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services