1 /*
   2  * Copyright (c) 2013, 2015, 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 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 }