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

Reviewed-by: mchung
This commit is contained in:
Paul Sandoz 2017-01-04 17:20:41 -08:00
parent 33703528b4
commit 94d019e9ba
2 changed files with 23 additions and 17 deletions

View File

@ -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 @@ public abstract class VarHandle {
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 @@ public abstract class VarHandle {
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];

View File

@ -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 @@ public class VarHandleTestAccessModeMethodNames {
toArray(Object[][]::new);
}
@Test(dataProvider = "accessModesProvider")
public void testMethodName(VarHandle.AccessMode am) {
assertEquals(am.methodName(), toMethodName(am.name()));
@ -58,4 +60,22 @@ public class VarHandleTestAccessModeMethodNames {
}
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));
}
}