|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 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 jdk.vm.ci.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 jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; |
| 41 | +import jdk.vm.ci.hotspot.HotSpotResolvedJavaField; |
| 42 | +import jdk.vm.ci.hotspot.HotSpotVMConfigAccess; |
| 43 | +import jdk.vm.ci.meta.JavaType; |
| 44 | +import jdk.vm.ci.meta.MetaAccessProvider; |
| 45 | +import jdk.vm.ci.meta.ResolvedJavaField; |
| 46 | +import jdk.vm.ci.meta.ResolvedJavaType; |
| 47 | +import jdk.vm.ci.runtime.JVMCI; |
| 48 | + |
| 49 | +/** |
| 50 | + * Tests {@link HotSpotResolvedJavaField} functionality. |
| 51 | + */ |
| 52 | +public class HotSpotResolvedJavaFieldTest { |
| 53 | + |
| 54 | + private static final Class<?>[] classesWithInternalFields = {Class.class, ClassLoader.class}; |
| 55 | + |
| 56 | + private static final Method createFieldMethod; |
| 57 | + private static final Field indexField; |
| 58 | + |
| 59 | + static { |
| 60 | + Method m = null; |
| 61 | + Field f = null; |
| 62 | + try { |
| 63 | + Class<?> typeImpl = Class.forName("jdk.vm.ci.hotspot.HotSpotResolvedObjectTypeImpl"); |
| 64 | + m = typeImpl.getDeclaredMethod("createField", JavaType.class, long.class, int.class, int.class); |
| 65 | + m.setAccessible(true); |
| 66 | + Class<?> fieldImpl = Class.forName("jdk.vm.ci.hotspot.HotSpotResolvedJavaFieldImpl"); |
| 67 | + f = fieldImpl.getDeclaredField("index"); |
| 68 | + f.setAccessible(true); |
| 69 | + } catch (Exception e) { |
| 70 | + throw new AssertionError(e); |
| 71 | + } |
| 72 | + |
| 73 | + createFieldMethod = m; |
| 74 | + indexField = f; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Same as {@code HotSpotModifiers.jvmFieldModifiers()} but works when using a JVMCI version |
| 79 | + * prior to the introduction of that method. |
| 80 | + */ |
| 81 | + private int jvmFieldModifiers() { |
| 82 | + HotSpotJVMCIRuntime runtime = runtime(); |
| 83 | + HotSpotVMConfigAccess access = new HotSpotVMConfigAccess(runtime.getConfigStore()); |
| 84 | + int accEnum = access.getConstant("JVM_ACC_ENUM", Integer.class, 0x4000); |
| 85 | + int accSynthetic = access.getConstant("JVM_ACC_SYNTHETIC", Integer.class, 0x1000); |
| 86 | + return PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | VOLATILE | TRANSIENT | accEnum | accSynthetic; |
| 87 | + } |
| 88 | + |
| 89 | + HotSpotJVMCIRuntime runtime() { |
| 90 | + return (HotSpotJVMCIRuntime) JVMCI.getRuntime(); |
| 91 | + } |
| 92 | + |
| 93 | + MetaAccessProvider getMetaAccess() { |
| 94 | + return runtime().getHostJVMCIBackend().getMetaAccess(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Tests that {@link HotSpotResolvedJavaField#getModifiers()} only includes the modifiers |
| 99 | + * returned by {@link Field#getModifiers()}. Namely, it must not include |
| 100 | + * {@code HotSpotResolvedJavaField#FIELD_INTERNAL_FLAG}. |
| 101 | + */ |
| 102 | + @Test |
| 103 | + public void testModifiersForInternal() { |
| 104 | + for (Class<?> c : classesWithInternalFields) { |
| 105 | + ResolvedJavaType type = getMetaAccess().lookupJavaType(c); |
| 106 | + for (ResolvedJavaField field : type.getInstanceFields(false)) { |
| 107 | + if (field.isInternal()) { |
| 108 | + Assert.assertEquals(0, ~jvmFieldModifiers() & field.getModifiers()); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Tests that {@code HotSpotResolvedObjectTypeImpl#createField(String, JavaType, long, int)} |
| 116 | + * always returns an {@linkplain ResolvedJavaField#equals(Object) equivalent} object for an |
| 117 | + * internal field. |
| 118 | + * |
| 119 | + * @throws InvocationTargetException |
| 120 | + * @throws IllegalArgumentException |
| 121 | + * @throws IllegalAccessException |
| 122 | + */ |
| 123 | + @Test |
| 124 | + public void testEquivalenceForInternalFields() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { |
| 125 | + for (Class<?> c : classesWithInternalFields) { |
| 126 | + ResolvedJavaType type = getMetaAccess().lookupJavaType(c); |
| 127 | + for (ResolvedJavaField field : type.getInstanceFields(false)) { |
| 128 | + if (field.isInternal()) { |
| 129 | + HotSpotResolvedJavaField expected = (HotSpotResolvedJavaField) field; |
| 130 | + int index = indexField.getInt(expected); |
| 131 | + ResolvedJavaField actual = (ResolvedJavaField) createFieldMethod.invoke(type, expected.getType(), expected.getOffset(), expected.getModifiers(), index); |
| 132 | + Assert.assertEquals(expected, actual); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testIsInObject() { |
| 140 | + for (Field f : String.class.getDeclaredFields()) { |
| 141 | + HotSpotResolvedJavaField rf = (HotSpotResolvedJavaField) getMetaAccess().lookupJavaField(f); |
| 142 | + Assert.assertEquals(rf.toString(), rf.isInObject(runtime().getHostJVMCIBackend().getConstantReflection().forString("a string")), !rf.isStatic()); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments