changeset 14475:6c9b3dc5bf6b

8141536: MethodType field offset calculation could be lazy Reviewed-by: vlivanov
author redestad
date Thu, 05 Nov 2015 16:36:55 +0100
parents a16ce5acb643
children 5b710994aafb
files src/java.base/share/classes/java/lang/invoke/MethodType.java
diffstat 1 files changed, 15 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/invoke/MethodType.java	Thu Nov 05 16:29:16 2015 +0100
+++ b/src/java.base/share/classes/java/lang/invoke/MethodType.java	Thu Nov 05 16:36:55 2015 +0100
@@ -1187,20 +1187,23 @@
         // store them into the implementation-specific final fields.
         checkRtype(rtype);
         checkPtypes(ptypes);
-        UNSAFE.putObject(this, rtypeOffset, rtype);
-        UNSAFE.putObject(this, ptypesOffset, ptypes);
+        UNSAFE.putObject(this, OffsetHolder.rtypeOffset, rtype);
+        UNSAFE.putObject(this, OffsetHolder.ptypesOffset, ptypes);
     }
 
-    // Support for resetting final fields while deserializing
-    private static final long rtypeOffset, ptypesOffset;
-    static {
-        try {
-            rtypeOffset = UNSAFE.objectFieldOffset
-                (MethodType.class.getDeclaredField("rtype"));
-            ptypesOffset = UNSAFE.objectFieldOffset
-                (MethodType.class.getDeclaredField("ptypes"));
-        } catch (Exception ex) {
-            throw new Error(ex);
+    // Support for resetting final fields while deserializing. Implement Holder
+    // pattern to make the rarely needed offset calculation lazy.
+    private static class OffsetHolder {
+        private static final long rtypeOffset, ptypesOffset;
+        static {
+            try {
+                rtypeOffset = UNSAFE.objectFieldOffset
+                    (MethodType.class.getDeclaredField("rtype"));
+                ptypesOffset = UNSAFE.objectFieldOffset
+                    (MethodType.class.getDeclaredField("ptypes"));
+            } catch (Exception ex) {
+                throw new Error(ex);
+            }
         }
     }