changeset 50729:d8ea829882ad jep-334

Tidy up VarHandle equals/hashcode impls.
author psandoz
date Wed, 23 May 2018 10:50:48 -0700
parents a064652a444f
children f44721e1c901
files src/java.base/share/classes/java/lang/invoke/VarHandle.java src/java.base/share/classes/java/lang/invoke/X-VarHandle.java.template
diffstat 2 files changed, 19 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/invoke/VarHandle.java	Wed May 23 13:24:31 2018 -0400
+++ b/src/java.base/share/classes/java/lang/invoke/VarHandle.java	Wed May 23 10:50:48 2018 -0700
@@ -1869,22 +1869,19 @@
                internalEquals(that);
     }
 
-    boolean internalEquals(VarHandle vh) {
-        return true;
-    }
+    abstract boolean internalEquals(VarHandle vh);
 
     @Override
     public final int hashCode() {
         return 31 * accessModeType(AccessMode.GET).hashCode() + internalHashCode();
     }
 
-    int internalHashCode() {
-        return 0;
-    }
+    abstract int internalHashCode();
 
     @Override
     public final String toString() {
         // @@@ defer to concrete type for additional description
+        // see https://bugs.openjdk.java.net/browse/JDK-8199149
         return String.format("VarHandle[varType=%s, cooordType=%s]",
                              varType().getName(),
                              coordinateTypes());
--- a/src/java.base/share/classes/java/lang/invoke/X-VarHandle.java.template	Wed May 23 13:24:31 2018 -0400
+++ b/src/java.base/share/classes/java/lang/invoke/X-VarHandle.java.template	Wed May 23 10:50:48 2018 -0700
@@ -81,6 +81,7 @@
             if (!receiverTypeRef.isPresent() || !fieldTypeRef.isPresent())
                 return Optional.empty();
 
+            // Reflect on this VarHandle to extract the field name
             String name = VarHandles.getFieldFromReceiverAndOffset(
                 receiverType, fieldOffset, {#if[Object]?fieldType:$type$.class}).getName();
             return Optional.of(VarHandleDesc.ofField(receiverTypeRef.get(), name, fieldTypeRef.get()));
@@ -365,6 +366,7 @@
             if (!fieldTypeRef.isPresent())
                 return Optional.empty();
 
+            // Reflect on this VarHandle to extract the field name
             var staticField = VarHandles.getStaticFieldFromBaseAndOffset(
                 base, fieldOffset, {#if[Object]?fieldType:$type$.class});
             var receiverTypeRef = staticField.getDeclaringClass().describeConstable();
@@ -638,6 +640,20 @@
         }
 
         @Override
+        final boolean internalEquals(VarHandle vh) {
+            // Equality of access mode types of AccessMode.GET is sufficient for
+            // equality checks
+            return true;
+        }
+
+        @Override
+        final int internalHashCode() {
+            // The hash code of the access mode types of AccessMode.GET is
+            // sufficient for hash code generation
+            return 0;
+        }
+
+        @Override
         public Optional<VarHandleDesc> describeConstable() {
             var arrayTypeRef = {#if[Object]?arrayType:$type$[].class}.describeConstable();
             if (!arrayTypeRef.isPresent())