8172201: Replace assert of return type in VarHandle.AccessMode with test

Reviewed-by: mchung
diff --git a/jdk/src/java.base/share/classes/java/lang/invoke/VarHandle.java b/jdk/src/java.base/share/classes/java/lang/invoke/VarHandle.java
index 167de41..57e3a3f 100644
--- a/jdk/src/java.base/share/classes/java/lang/invoke/VarHandle.java
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/VarHandle.java
@@ -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];
diff --git a/jdk/test/java/lang/invoke/VarHandles/VarHandleTestAccessModeMethodNames.java b/jdk/test/java/lang/invoke/VarHandles/VarHandleTestAccessModeMethodNames.java
index dc86cc6..3365bd9 100644
--- a/jdk/test/java/lang/invoke/VarHandles/VarHandleTestAccessModeMethodNames.java
+++ b/jdk/test/java/lang/invoke/VarHandles/VarHandleTestAccessModeMethodNames.java
@@ -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));
+    }
 }