< prev index next >

test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestResolvedJavaType.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 2016, 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  */


  32  *          java.base/jdk.internal.misc
  33  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -Djvmci.Compiler=null jdk.vm.ci.runtime.test.TestResolvedJavaType
  34  */
  35 
  36 package jdk.vm.ci.runtime.test;
  37 
  38 import static java.lang.reflect.Modifier.isAbstract;
  39 import static java.lang.reflect.Modifier.isFinal;
  40 import static java.lang.reflect.Modifier.isPrivate;
  41 import static java.lang.reflect.Modifier.isProtected;
  42 import static java.lang.reflect.Modifier.isPublic;
  43 import static java.lang.reflect.Modifier.isStatic;
  44 import static org.junit.Assert.assertArrayEquals;
  45 import static org.junit.Assert.assertEquals;
  46 import static org.junit.Assert.assertFalse;
  47 import static org.junit.Assert.assertNotNull;
  48 import static org.junit.Assert.assertNull;
  49 import static org.junit.Assert.assertTrue;
  50 
  51 import java.lang.annotation.Annotation;


  52 import java.lang.reflect.Field;
  53 import java.lang.reflect.Method;
  54 import java.lang.reflect.Modifier;
  55 import java.util.Arrays;
  56 import java.util.Collections;
  57 import java.util.function.Supplier;
  58 import java.util.HashMap;
  59 import java.util.HashSet;
  60 import java.util.Map;
  61 import java.util.Set;
  62 
  63 import org.junit.Test;
  64 
  65 import jdk.internal.reflect.ConstantPool;
  66 import jdk.vm.ci.common.JVMCIError;
  67 import jdk.vm.ci.meta.Assumptions.AssumptionResult;
  68 import jdk.vm.ci.meta.JavaConstant;
  69 import jdk.vm.ci.meta.JavaKind;
  70 import jdk.vm.ci.meta.ResolvedJavaField;
  71 import jdk.vm.ci.meta.ResolvedJavaMethod;


 745 
 746     public Field lookupField(Set<Field> fields, ResolvedJavaField key) {
 747         for (Field f : fields) {
 748             if (fieldsEqual(f, key)) {
 749                 return f;
 750             }
 751         }
 752         return null;
 753     }
 754 
 755     private static boolean isHiddenFromReflection(ResolvedJavaField f) {
 756         if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(Throwable.class)) && f.getName().equals("backtrace")) {
 757             return true;
 758         }
 759         if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(ConstantPool.class)) && f.getName().equals("constantPoolOop")) {
 760             return true;
 761         }
 762         if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(Class.class)) && f.getName().equals("classLoader")) {
 763             return true;
 764         }







 765         return false;
 766     }
 767 
 768     @Test
 769     public void getInstanceFieldsTest() {
 770         for (Class<?> c : classes) {
 771             ResolvedJavaType type = metaAccess.lookupJavaType(c);
 772             for (boolean includeSuperclasses : new boolean[]{true, false}) {
 773                 Set<Field> expected = getInstanceFields(c, includeSuperclasses);
 774                 ResolvedJavaField[] actual = type.getInstanceFields(includeSuperclasses);
 775                 for (Field f : expected) {
 776                     assertNotNull(lookupField(actual, f));
 777                 }
 778                 for (ResolvedJavaField rf : actual) {
 779                     if (!isHiddenFromReflection(rf)) {
 780                         assertEquals(rf.toString(), lookupField(expected, rf) != null, !rf.isInternal());
 781                     }
 782                 }
 783 
 784                 // Test stability of getInstanceFields


   1 /*
   2  * Copyright (c) 2012, 2018, 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  */


  32  *          java.base/jdk.internal.misc
  33  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -Djvmci.Compiler=null jdk.vm.ci.runtime.test.TestResolvedJavaType
  34  */
  35 
  36 package jdk.vm.ci.runtime.test;
  37 
  38 import static java.lang.reflect.Modifier.isAbstract;
  39 import static java.lang.reflect.Modifier.isFinal;
  40 import static java.lang.reflect.Modifier.isPrivate;
  41 import static java.lang.reflect.Modifier.isProtected;
  42 import static java.lang.reflect.Modifier.isPublic;
  43 import static java.lang.reflect.Modifier.isStatic;
  44 import static org.junit.Assert.assertArrayEquals;
  45 import static org.junit.Assert.assertEquals;
  46 import static org.junit.Assert.assertFalse;
  47 import static org.junit.Assert.assertNotNull;
  48 import static org.junit.Assert.assertNull;
  49 import static org.junit.Assert.assertTrue;
  50 
  51 import java.lang.annotation.Annotation;
  52 import java.lang.reflect.AccessibleObject;
  53 import java.lang.reflect.Constructor;
  54 import java.lang.reflect.Field;
  55 import java.lang.reflect.Method;
  56 import java.lang.reflect.Modifier;
  57 import java.util.Arrays;
  58 import java.util.Collections;
  59 import java.util.function.Supplier;
  60 import java.util.HashMap;
  61 import java.util.HashSet;
  62 import java.util.Map;
  63 import java.util.Set;
  64 
  65 import org.junit.Test;
  66 
  67 import jdk.internal.reflect.ConstantPool;
  68 import jdk.vm.ci.common.JVMCIError;
  69 import jdk.vm.ci.meta.Assumptions.AssumptionResult;
  70 import jdk.vm.ci.meta.JavaConstant;
  71 import jdk.vm.ci.meta.JavaKind;
  72 import jdk.vm.ci.meta.ResolvedJavaField;
  73 import jdk.vm.ci.meta.ResolvedJavaMethod;


 747 
 748     public Field lookupField(Set<Field> fields, ResolvedJavaField key) {
 749         for (Field f : fields) {
 750             if (fieldsEqual(f, key)) {
 751                 return f;
 752             }
 753         }
 754         return null;
 755     }
 756 
 757     private static boolean isHiddenFromReflection(ResolvedJavaField f) {
 758         if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(Throwable.class)) && f.getName().equals("backtrace")) {
 759             return true;
 760         }
 761         if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(ConstantPool.class)) && f.getName().equals("constantPoolOop")) {
 762             return true;
 763         }
 764         if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(Class.class)) && f.getName().equals("classLoader")) {
 765             return true;
 766         }
 767         if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(ClassLoader.class)) ||
 768             f.getDeclaringClass().equals(metaAccess.lookupJavaType(AccessibleObject.class)) ||
 769             f.getDeclaringClass().equals(metaAccess.lookupJavaType(Constructor.class)) ||
 770             f.getDeclaringClass().equals(metaAccess.lookupJavaType(Field.class)) ||
 771             f.getDeclaringClass().equals(metaAccess.lookupJavaType(Method.class))) {
 772             return true;
 773         }
 774         return false;
 775     }
 776 
 777     @Test
 778     public void getInstanceFieldsTest() {
 779         for (Class<?> c : classes) {
 780             ResolvedJavaType type = metaAccess.lookupJavaType(c);
 781             for (boolean includeSuperclasses : new boolean[]{true, false}) {
 782                 Set<Field> expected = getInstanceFields(c, includeSuperclasses);
 783                 ResolvedJavaField[] actual = type.getInstanceFields(includeSuperclasses);
 784                 for (Field f : expected) {
 785                     assertNotNull(lookupField(actual, f));
 786                 }
 787                 for (ResolvedJavaField rf : actual) {
 788                     if (!isHiddenFromReflection(rf)) {
 789                         assertEquals(rf.toString(), lookupField(expected, rf) != null, !rf.isInternal());
 790                     }
 791                 }
 792 
 793                 // Test stability of getInstanceFields


< prev index next >