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_r383185284
##########
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) {
+ throw new Error("Could not find class '" + className +
"' for unsafe operations.", e);
}
}
+ /** Should not be instantiated. */
+ private MemoryUtils() {}
Review comment:
I would put the constructor at the beginning or the end but not somewhere in
the middle.
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services