dschneider-pivotal commented on a change in pull request #7484:
URL: https://github.com/apache/geode/pull/7484#discussion_r835502978



##########
File path: 
geode-core/src/main/java/org/apache/geode/internal/size/ReflectionSingleObjectSizer.java
##########
@@ -89,62 +90,89 @@ public static long sizeof(Class<?> clazz) {
     return sizeof(clazz, true);
   }
 
+  public static long sizeof(Class<?> clazz, boolean roundResult) {
+    return sizeof(clazz, roundResult, unsafe);
+  }
+
+  @VisibleForTesting
+  static long sizeof(Class<?> clazz, boolean roundResult, Unsafe myUnsafe) {
+    Assert.assertTrue(!clazz.isArray());
+    long size = unsafeSizeof(clazz, myUnsafe);
+    if (size == -1) {
+      size = safeSizeof(clazz);
+    }
+    if (roundResult) {
+      size = roundUpSize(size);
+    }
+    return size;
+  }
+
   /**
+   * Returns -1 if it was not able to compute the size; otherwise returns the 
size.
    * Since unsafe.fieldOffset(Field) will give us the offset to the first byte 
of that field all we
    * need to do is find which of the non-static declared fields has the 
greatest offset.
    */
-  public static long sizeof(Class<?> clazz, boolean roundResult) {
-    Assert.assertTrue(!clazz.isArray());
+  @VisibleForTesting
+  static long unsafeSizeof(Class<?> clazz, Unsafe myUnsafe) {
+    if (myUnsafe == null) {
+      return -1;
+    }
     long size;
-    if (unsafe != null) {
-      Field lastField = null;
-      long lastFieldOffset = 0;
-      do {
-        Field[] fields = clazz.getDeclaredFields();
-        for (Field field : fields) {
-          if (!Modifier.isStatic(field.getModifiers())) {
-            long offset = unsafe.fieldOffset(field);
+    Field lastField = null;
+    long lastFieldOffset = 0;
+    do {
+      Field[] fields = clazz.getDeclaredFields();
+      for (Field field : fields) {
+        if (!Modifier.isStatic(field.getModifiers())) {
+          try {
+            long offset = myUnsafe.fieldOffset(field);
             if (offset >= lastFieldOffset) {
               lastFieldOffset = offset;
               lastField = field;
             }
+          } catch (UnsupportedOperationException ex) {
+            // This happens on java 17 because hidden classes do not support
+            // unsafe.fieldOffset.
+            return -1;

Review comment:
       the only product caller of this method is 
ReflectionSingleObjectSizer.sizeof(Class, boolean, Unsafe). If it gets -1 back 
from unsafeSizeof then it always calls safeSizeof.




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to