19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.hotspot.test; 24 25 import static java.lang.reflect.Modifier.FINAL; 26 import static java.lang.reflect.Modifier.PRIVATE; 27 import static java.lang.reflect.Modifier.PROTECTED; 28 import static java.lang.reflect.Modifier.PUBLIC; 29 import static java.lang.reflect.Modifier.STATIC; 30 import static java.lang.reflect.Modifier.TRANSIENT; 31 import static java.lang.reflect.Modifier.VOLATILE; 32 33 import java.lang.reflect.Field; 34 import java.lang.reflect.InvocationTargetException; 35 import java.lang.reflect.Method; 36 37 import org.junit.Assert; 38 import org.junit.Test; 39 40 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 41 42 import jdk.vm.ci.hotspot.HotSpotResolvedJavaField; 43 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 44 import jdk.vm.ci.meta.JavaType; 45 import jdk.vm.ci.meta.ResolvedJavaField; 46 47 /** 48 * Tests {@link HotSpotResolvedJavaField} functionality. 49 */ 50 public class HotSpotResolvedJavaFieldTest extends HotSpotGraalCompilerTest { 51 52 private static final Class<?>[] classesWithInternalFields = {Class.class, ClassLoader.class}; 53 54 private static final Method createFieldMethod; 55 56 static { 57 Method ret = null; 58 try { 59 Class<?> typeImpl = Class.forName("jdk.vm.ci.hotspot.HotSpotResolvedObjectTypeImpl"); 60 ret = typeImpl.getDeclaredMethod("createField", String.class, JavaType.class, long.class, int.class); 61 ret.setAccessible(true); 62 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) { 63 e.printStackTrace(); 64 } 65 66 createFieldMethod = ret; 67 } 68 69 /** 70 * Same as {@code HotSpotModifiers.jvmFieldModifiers()} but works when using a JVMCI version 71 * prior to the introduction of that method. 72 */ 73 private int jvmFieldModifiers() { 74 GraalHotSpotVMConfig config = runtime().getVMConfig(); 75 int accEnum = config.getConstant("JVM_ACC_ENUM", Integer.class, 0x4000); 76 int accSynthetic = config.getConstant("JVM_ACC_SYNTHETIC", Integer.class, 0x1000); 77 return PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | VOLATILE | TRANSIENT | accEnum | accSynthetic; 78 } 79 80 /** 81 * Tests that {@link HotSpotResolvedJavaField#getModifiers()} only includes the modifiers 82 * returned by {@link Field#getModifiers()}. Namely, it must not include 83 * {@code HotSpotResolvedJavaField#FIELD_INTERNAL_FLAG}. 84 */ 85 @Test 86 public void testModifiersForInternal() { 87 for (Class<?> c : classesWithInternalFields) { 88 HotSpotResolvedObjectType type = HotSpotResolvedObjectType.fromObjectClass(c); 89 for (ResolvedJavaField field : type.getInstanceFields(false)) { 90 if (field.isInternal()) { 91 Assert.assertEquals(0, ~jvmFieldModifiers() & field.getModifiers()); 92 } 93 } 94 } 95 } 96 97 /** 98 * Tests that {@code HotSpotResolvedObjectType#createField(String, JavaType, long, int)} always 99 * returns the same object for an internal field. 100 * 101 * @throws InvocationTargetException 102 * @throws IllegalArgumentException 103 * @throws IllegalAccessException 104 */ 105 @Test 106 public void testCachingForInternalFields() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { 107 Assert.assertTrue("HotSpotResolvedObjectTypeImpl.createField method not found", createFieldMethod != null); 108 for (Class<?> c : classesWithInternalFields) { 109 HotSpotResolvedObjectType type = HotSpotResolvedObjectType.fromObjectClass(c); 110 for (ResolvedJavaField field : type.getInstanceFields(false)) { 111 if (field.isInternal()) { 112 HotSpotResolvedJavaField expected = (HotSpotResolvedJavaField) field; 113 ResolvedJavaField actual = (ResolvedJavaField) createFieldMethod.invoke(type, expected.getName(), expected.getType(), expected.offset(), expected.getModifiers()); 114 Assert.assertEquals(expected, actual); 115 } 116 } 117 } 118 } 119 120 @Test 121 public void testIsInObject() { 122 for (Field f : String.class.getDeclaredFields()) { 123 HotSpotResolvedJavaField rf = (HotSpotResolvedJavaField) getMetaAccess().lookupJavaField(f); 124 Assert.assertEquals(rf.toString(), rf.isInObject("a string"), !rf.isStatic()); 125 } 126 } 127 } | 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.hotspot.test; 24 25 import static java.lang.reflect.Modifier.FINAL; 26 import static java.lang.reflect.Modifier.PRIVATE; 27 import static java.lang.reflect.Modifier.PROTECTED; 28 import static java.lang.reflect.Modifier.PUBLIC; 29 import static java.lang.reflect.Modifier.STATIC; 30 import static java.lang.reflect.Modifier.TRANSIENT; 31 import static java.lang.reflect.Modifier.VOLATILE; 32 33 import java.lang.reflect.Field; 34 import java.lang.reflect.InvocationTargetException; 35 import java.lang.reflect.Method; 36 37 import org.junit.Assert; 38 import org.junit.Test; 39 import org.graalvm.compiler.core.common.util.Util; 40 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 41 42 import jdk.vm.ci.hotspot.HotSpotResolvedJavaField; 43 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 44 import jdk.vm.ci.meta.JavaType; 45 import jdk.vm.ci.meta.ResolvedJavaField; 46 47 /** 48 * Tests {@link HotSpotResolvedJavaField} functionality. 49 */ 50 public class HotSpotResolvedJavaFieldTest extends HotSpotGraalCompilerTest { 51 52 private static final Class<?>[] classesWithInternalFields = {Class.class, ClassLoader.class}; 53 54 private static final Method createFieldMethod; 55 private static final Field indexField; 56 57 static { 58 Method m = null; 59 Field f = null; 60 try { 61 Class<?> typeImpl = Class.forName("jdk.vm.ci.hotspot.HotSpotResolvedObjectTypeImpl"); 62 m = typeImpl.getDeclaredMethod("createField", JavaType.class, long.class, int.class, int.class); 63 Util.setAccessible(m, true); 64 Class<?> fieldImpl = Class.forName("jdk.vm.ci.hotspot.HotSpotResolvedJavaFieldImpl"); 65 f = fieldImpl.getDeclaredField("index"); 66 Util.setAccessible(f, true); 67 } catch (Exception e) { 68 throw new AssertionError(e); 69 } 70 71 createFieldMethod = m; 72 indexField = f; 73 } 74 75 /** 76 * Same as {@code HotSpotModifiers.jvmFieldModifiers()} but works when using a JVMCI version 77 * prior to the introduction of that method. 78 */ 79 private int jvmFieldModifiers() { 80 GraalHotSpotVMConfig config = runtime().getVMConfig(); 81 int accEnum = config.getConstant("JVM_ACC_ENUM", Integer.class, 0x4000); 82 int accSynthetic = config.getConstant("JVM_ACC_SYNTHETIC", Integer.class, 0x1000); 83 return PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | VOLATILE | TRANSIENT | accEnum | accSynthetic; 84 } 85 86 /** 87 * Tests that {@link HotSpotResolvedJavaField#getModifiers()} only includes the modifiers 88 * returned by {@link Field#getModifiers()}. Namely, it must not include 89 * {@code HotSpotResolvedJavaField#FIELD_INTERNAL_FLAG}. 90 */ 91 @Test 92 public void testModifiersForInternal() { 93 for (Class<?> c : classesWithInternalFields) { 94 HotSpotResolvedObjectType type = HotSpotResolvedObjectType.fromObjectClass(c); 95 for (ResolvedJavaField field : type.getInstanceFields(false)) { 96 if (field.isInternal()) { 97 Assert.assertEquals(0, ~jvmFieldModifiers() & field.getModifiers()); 98 } 99 } 100 } 101 } 102 103 /** 104 * Tests that {@code HotSpotResolvedObjectType#createField(String, JavaType, long, int)} always 105 * returns an {@linkplain ResolvedJavaField#equals(Object) equivalent} object for an internal 106 * field. 107 * 108 * @throws InvocationTargetException 109 * @throws IllegalArgumentException 110 * @throws IllegalAccessException 111 */ 112 @Test 113 public void testEquivalenceForInternalFields() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { 114 for (Class<?> c : classesWithInternalFields) { 115 HotSpotResolvedObjectType type = HotSpotResolvedObjectType.fromObjectClass(c); 116 for (ResolvedJavaField field : type.getInstanceFields(false)) { 117 if (field.isInternal()) { 118 HotSpotResolvedJavaField expected = (HotSpotResolvedJavaField) field; 119 int index = indexField.getInt(expected); 120 ResolvedJavaField actual = (ResolvedJavaField) createFieldMethod.invoke(type, expected.getType(), expected.offset(), expected.getModifiers(), index); 121 Assert.assertEquals(expected, actual); 122 } 123 } 124 } 125 } 126 127 @Test 128 public void testIsInObject() { 129 for (Field f : String.class.getDeclaredFields()) { 130 HotSpotResolvedJavaField rf = (HotSpotResolvedJavaField) getMetaAccess().lookupJavaField(f); 131 Assert.assertEquals(rf.toString(), rf.isInObject("a string"), !rf.isStatic()); 132 } 133 } 134 } |