001/* 002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.hotspot.test; 024 025import java.lang.reflect.*; 026 027import jdk.internal.jvmci.hotspot.*; 028import jdk.internal.jvmci.meta.*; 029 030import org.junit.*; 031 032/** 033 * Tests {@link HotSpotResolvedJavaField} functionality. 034 */ 035public class HotSpotResolvedJavaFieldTest extends HotSpotGraalCompilerTest { 036 037 private static final Class<?>[] classesWithInternalFields = {Class.class, ClassLoader.class}; 038 039 /** 040 * Tests that {@link HotSpotResolvedJavaField#getModifiers()} only includes the modifiers 041 * returned by {@link Field#getModifiers()}. Namely, it must not include 042 * {@code HotSpotResolvedJavaField#FIELD_INTERNAL_FLAG}. 043 */ 044 @Test 045 public void testModifiersForInternal() { 046 for (Class<?> c : classesWithInternalFields) { 047 HotSpotResolvedObjectType type = HotSpotResolvedObjectTypeImpl.fromObjectClass(c); 048 for (ResolvedJavaField field : type.getInstanceFields(false)) { 049 if (field.isInternal()) { 050 Assert.assertEquals(0, ~ModifiersProvider.jvmFieldModifiers() & field.getModifiers()); 051 } 052 } 053 } 054 } 055 056 /** 057 * Tests that {@link HotSpotResolvedObjectTypeImpl#createField(String, JavaType, long, int)} 058 * always returns the same object for an internal field. 059 */ 060 @Test 061 public void testCachingForInternalFields() { 062 for (Class<?> c : classesWithInternalFields) { 063 HotSpotResolvedObjectType type = HotSpotResolvedObjectTypeImpl.fromObjectClass(c); 064 for (ResolvedJavaField field : type.getInstanceFields(false)) { 065 if (field.isInternal()) { 066 HotSpotResolvedJavaField expected = (HotSpotResolvedJavaField) field; 067 ResolvedJavaField actual = type.createField(expected.getName(), expected.getType(), expected.offset(), expected.getModifiers()); 068 Assert.assertEquals(expected, actual); 069 } 070 } 071 } 072 } 073 074 @Test 075 public void testIsInObject() { 076 for (Field f : String.class.getDeclaredFields()) { 077 HotSpotResolvedJavaField rf = (HotSpotResolvedJavaField) getMetaAccess().lookupJavaField(f); 078 Assert.assertEquals(rf.toString(), rf.isInObject("a string"), !rf.isStatic()); 079 } 080 } 081}