1 /*
   2  * Copyright (c) 2012, 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 
  24 /**
  25  * @test
  26  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  27  * @library ../../../../../
  28  * @modules jdk.vm.ci/jdk.vm.ci.meta
  29  *          jdk.vm.ci/jdk.vm.ci.runtime
  30  *          java.base/jdk.internal.misc
  31  * @build jdk.vm.ci.runtime.test.TestMetaAccessProvider
  32  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.runtime.test.TestMetaAccessProvider
  33  */
  34 
  35 package jdk.vm.ci.runtime.test;
  36 
  37 import static jdk.vm.ci.meta.MetaUtil.toInternalName;
  38 import static org.junit.Assert.assertEquals;
  39 import static org.junit.Assert.assertNotNull;
  40 import static org.junit.Assert.assertNull;
  41 import static org.junit.Assert.assertTrue;
  42 
  43 import java.lang.reflect.Field;
  44 import java.lang.reflect.Method;
  45 import jdk.vm.ci.meta.DeoptimizationAction;
  46 import jdk.vm.ci.meta.DeoptimizationReason;
  47 
  48 import jdk.vm.ci.meta.JavaConstant;
  49 import jdk.vm.ci.meta.JavaKind;
  50 import jdk.vm.ci.meta.MetaAccessProvider;
  51 import jdk.vm.ci.meta.ResolvedJavaField;
  52 import jdk.vm.ci.meta.ResolvedJavaMethod;
  53 import jdk.vm.ci.meta.ResolvedJavaType;
  54 import jdk.vm.ci.meta.Signature;
  55 
  56 import org.junit.Test;
  57 
  58 /**
  59  * Tests for {@link MetaAccessProvider}.
  60  */
  61 public class TestMetaAccessProvider extends TypeUniverse {
  62     private static final DeoptimizationAction DEOPT_ACTION = DeoptimizationAction.InvalidateRecompile;
  63     private static final DeoptimizationReason DEOPT_REASON = DeoptimizationReason.Aliasing;
  64     private static final int INT_23BITS_SET = 0x7FFFFF;
  65     private static final int[] DEBUG_IDS = new int[]{0, 1, 42, INT_23BITS_SET};
  66     private static final int[] VALID_ENCODED_VALUES = new int[]{
  67                     metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[0]).asInt(),
  68                     metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[1]).asInt(),
  69                     metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[2]).asInt(),
  70                     metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[3]).asInt()
  71     };
  72 
  73     @Test
  74     public void lookupJavaTypeTest() {
  75         for (Class<?> c : classes) {
  76             ResolvedJavaType type = metaAccess.lookupJavaType(c);
  77             assertNotNull(c.toString(), type);
  78             assertEquals(c.toString(), type.getName(), toInternalName(c.getName()));
  79             assertEquals(c.toString(), type.getName(), toInternalName(type.toJavaName()));
  80             assertEquals(c.toString(), c.getName(), type.toClassName());
  81             if (!type.isArray()) {
  82                 assertEquals(c.toString(), c.getName(), type.toJavaName());
  83             }
  84         }
  85     }
  86 
  87     @Test(expected = IllegalArgumentException.class)
  88     public void lookupJavaTypeNegativeTest() {
  89         metaAccess.lookupJavaType((Class<?>) null);
  90     }
  91 
  92     @Test
  93     public void lookupJavaTypesTest() {
  94         ResolvedJavaType[] result = metaAccess.lookupJavaTypes(classes.toArray(new Class<?>[classes.size()]));
  95         int counter = 0;
  96         for (Class<?> aClass : classes) {
  97             assertEquals("Unexpected javaType: " + result[counter] + " while expecting of class: " + aClass, result[counter].toClassName(), aClass.getName());
  98             counter++;
  99         }
 100     }
 101 
 102     @Test(expected = NullPointerException.class)
 103     public void lookupJavaTypesNegative1Test() {
 104         assertNull("Expected null", metaAccess.lookupJavaTypes(null));
 105     }
 106 
 107     @Test(expected = IllegalArgumentException.class)
 108     public void lookupJavaTypesNegative2Test() {
 109         ResolvedJavaType[] result = metaAccess.lookupJavaTypes(new Class<?>[]{null, null, null});
 110         for (ResolvedJavaType aType : result) {
 111             assertNull("Expected null javaType", aType);
 112         }
 113         result = metaAccess.lookupJavaTypes(new Class<?>[]{String.class, String.class});
 114         assertEquals("Results not equals", result[0].getClass(), result[1].getClass());
 115         assertEquals("Result is not String.class", result[0].getClass(), String.class);
 116     }
 117 
 118     @Test
 119     public void lookupJavaMethodTest() {
 120         for (Class<?> c : classes) {
 121             for (Method reflect : c.getDeclaredMethods()) {
 122                 ResolvedJavaMethod method = metaAccess.lookupJavaMethod(reflect);
 123                 assertNotNull(method);
 124                 assertTrue(method.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass())));
 125             }
 126         }
 127     }
 128 
 129     @Test(expected = NullPointerException.class)
 130     public void lookupJavaMethodNegativeTest() {
 131         metaAccess.lookupJavaMethod(null);
 132     }
 133 
 134     @Test
 135     public void lookupJavaFieldTest() {
 136         for (Class<?> c : classes) {
 137             for (Field reflect : c.getDeclaredFields()) {
 138                 ResolvedJavaField field = metaAccess.lookupJavaField(reflect);
 139                 assertNotNull(field);
 140                 assertTrue(field.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass())));
 141             }
 142         }
 143     }
 144 
 145     @Test
 146     public void lookupJavaTypeConstantTest() {
 147         for (ConstantValue cv : constants()) {
 148             JavaConstant c = cv.value;
 149             if (c.getJavaKind() == JavaKind.Object && !c.isNull()) {
 150                 Object o = cv.boxed;
 151                 ResolvedJavaType type = metaAccess.lookupJavaType(c);
 152                 assertNotNull(type);
 153                 assertTrue(type.equals(metaAccess.lookupJavaType(o.getClass())));
 154             } else {
 155                 assertEquals(metaAccess.lookupJavaType(c), null);
 156             }
 157         }
 158     }
 159 
 160     @Test(expected = NullPointerException.class)
 161     public void lookupJavaTypeConstantNegativeTest() {
 162         metaAccess.lookupJavaType((JavaConstant) null);
 163     }
 164 
 165     @Test
 166     public void getMemorySizeTest() {
 167         for (ConstantValue cv : constants()) {
 168             JavaConstant c = cv.value;
 169             long memSize = metaAccess.getMemorySize(c);
 170             if (c.isNull()) {
 171                 assertEquals("Expected size = 0 for null", memSize, 0L);
 172             } else {
 173                 assertTrue("Expected size != 0 for " + cv, memSize != 0L);
 174             }
 175         }
 176     }
 177 
 178     @Test(expected = NullPointerException.class)
 179     public void getMemorySizeNegativeTest() {
 180         metaAccess.getMemorySize(null);
 181     }
 182 
 183     @Test
 184     public void parseMethodDescriptorTest() {
 185         for (String retType : new String[]{"V", "Z", "Ljava/lang/String;"}) {
 186             for (String paramTypes : new String[]{"", "B",
 187                             "Ljava/lang/String;", "JLjava/lang/String;",
 188                             "Ljava/lang/String;F",
 189                             "[Ljava/lang/String;ZBCDFIJLS[ILjava/lang/Object;"}) {
 190                 String signature = "(" + paramTypes + ")" + retType;
 191                 Signature result = metaAccess.parseMethodDescriptor(signature);
 192                 assertEquals("Expected signatures to be equal", result.toMethodDescriptor(), signature);
 193             }
 194         }
 195     }
 196 
 197     @Test(expected = NullPointerException.class)
 198     public void parseMethodDescriptorNegativeNullTest() {
 199         metaAccess.parseMethodDescriptor(null);
 200     }
 201 
 202     @Test(expected = NullPointerException.class)
 203     public void encodeDeoptActionAndReasonNegative1Test() {
 204         metaAccess.encodeDeoptActionAndReason(null, DeoptimizationReason.Aliasing, 0);
 205 
 206     }
 207 
 208     @Test(expected = NullPointerException.class)
 209     public void encodeDeoptActionAndReasonNegative2Test() {
 210         metaAccess.encodeDeoptActionAndReason(DeoptimizationAction.InvalidateRecompile, null, 0);
 211     }
 212 
 213     @Test
 214     public void decodeDeoptReasonTest() {
 215         for (int encoded : VALID_ENCODED_VALUES) {
 216             JavaConstant value = JavaConstant.forInt(encoded);
 217             DeoptimizationReason reason = metaAccess.decodeDeoptReason(value);
 218             assertEquals("Expected equal reasons", reason, DEOPT_REASON);
 219         }
 220     }
 221 
 222     @Test
 223     public void decodeDeoptReasonNegative1Test() {
 224         int encoded = 42;
 225         JavaConstant value = JavaConstant.forInt(encoded);
 226         metaAccess.decodeDeoptReason(value);
 227     }
 228 
 229     @Test(expected = NullPointerException.class)
 230     public void decodeDeoptReasonNegative2Test() {
 231         metaAccess.decodeDeoptReason(null);
 232     }
 233 
 234     @Test
 235     public void decodeDeoptActionTest() {
 236         for (int encoded : VALID_ENCODED_VALUES) {
 237             JavaConstant value = JavaConstant.forInt(encoded);
 238             DeoptimizationAction action = metaAccess.decodeDeoptAction(value);
 239             assertEquals("Expected equal actions", action, DEOPT_ACTION);
 240         }
 241     }
 242 
 243     @Test
 244     public void decodeDeoptActionNegative1Test() {
 245         int encoded = 123456789;
 246         JavaConstant value = JavaConstant.forInt(encoded);
 247         metaAccess.decodeDeoptAction(value);
 248     }
 249 
 250     @Test(expected = NullPointerException.class)
 251     public void decodeDeoptActionNegative2Test() {
 252         metaAccess.decodeDeoptAction(null);
 253     }
 254 
 255     @Test
 256     public void decodeDebugIdTest() {
 257         for (int i = 0; i < VALID_ENCODED_VALUES.length; i++) {
 258             JavaConstant value = JavaConstant.forInt(VALID_ENCODED_VALUES[i]);
 259             assertEquals("Unexpected debugId", metaAccess.decodeDebugId(value), DEBUG_IDS[i]);
 260         }
 261     }
 262 }