8172201: Replace assert of return type in VarHandle.AccessMode with test
authorpsandoz
Wed, 04 Jan 2017 17:20:41 -0800
changeset 42996 b778c8cf514d
parent 42995 a953f0d20217
child 42997 5f83f2054eca
8172201: Replace assert of return type in VarHandle.AccessMode with test Reviewed-by: mchung
jdk/src/java.base/share/classes/java/lang/invoke/VarHandle.java
jdk/test/java/lang/invoke/VarHandles/VarHandleTestAccessModeMethodNames.java
--- a/jdk/src/java.base/share/classes/java/lang/invoke/VarHandle.java	Wed Jan 04 14:59:18 2017 -0500
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/VarHandle.java	Wed Jan 04 17:20:41 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1786,10 +1786,6 @@
         AccessMode(final String methodName, AccessType at) {
             this.methodName = methodName;
             this.at = at;
-
-            // Assert that return type is correct
-            // Otherwise, when disabled avoid using reflection
-            assert at.returnType == getReturnType(methodName);
         }
 
         /**
@@ -1821,16 +1817,6 @@
             throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
         }
 
-        private static Class<?> getReturnType(String name) {
-            try {
-                Method m = VarHandle.class.getMethod(name, Object[].class);
-                return m.getReturnType();
-            }
-            catch (Exception e) {
-                throw newInternalError(e);
-            }
-        }
-
         @ForceInline
         static MemberName getMemberName(int ordinal, VarForm vform) {
             return vform.memberName_table[ordinal];
--- a/jdk/test/java/lang/invoke/VarHandles/VarHandleTestAccessModeMethodNames.java	Wed Jan 04 14:59:18 2017 -0500
+++ b/jdk/test/java/lang/invoke/VarHandles/VarHandleTestAccessModeMethodNames.java	Wed Jan 04 17:20:41 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,12 +24,15 @@
 /*
  * @test
  * @run testng VarHandleTestAccessModeMethodNames
+ * @modules java.base/java.lang.invoke:open
  */
 
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 import java.lang.invoke.VarHandle;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.util.stream.Stream;
 
 import static org.testng.Assert.assertEquals;
@@ -43,7 +46,6 @@
                 toArray(Object[][]::new);
     }
 
-
     @Test(dataProvider = "accessModesProvider")
     public void testMethodName(VarHandle.AccessMode am) {
         assertEquals(am.methodName(), toMethodName(am.name()));
@@ -58,4 +60,22 @@
         }
         return s.toString();
     }
+
+
+    @Test(dataProvider = "accessModesProvider")
+    public void testReturnType(VarHandle.AccessMode am) throws Exception {
+        assertEquals(getReturnType(am.methodName()), getAccessModeReturnType(am));
+    }
+
+    private static Class<?> getReturnType(String name) throws Exception {
+        return VarHandle.class.getMethod(name, Object[].class).getReturnType();
+    }
+
+    private static Class<?> getAccessModeReturnType(VarHandle.AccessMode am) throws Exception {
+        Field field_am_at = VarHandle.AccessMode.class.getDeclaredField("at");
+        field_am_at.setAccessible(true);
+        Field field_at_returnType = field_am_at.getType().getDeclaredField("returnType");
+        field_at_returnType.setAccessible(true);
+        return (Class<?>) field_at_returnType.get(field_am_at.get(am));
+    }
 }