1 /*
   2  * Copyright (c) 2015, 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 java.util.Objects;
  26 
  27 import org.junit.Test;
  28 
  29 import org.graalvm.compiler.core.common.GraalOptions;
  30 import org.graalvm.compiler.core.test.GraalCompilerTest;
  31 import org.graalvm.compiler.nodes.ConstantNode;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.options.OptionValue;
  34 import org.graalvm.compiler.options.OptionValue.OverrideScope;
  35 import org.graalvm.compiler.phases.tiers.Suites;
  36 
  37 import jdk.vm.ci.meta.JavaKind;
  38 
  39 public class LoadJavaMirrorWithKlassTest extends GraalCompilerTest {
  40 
  41     private static class Wrapper {
  42         private Class<?> clazz;
  43 
  44         @Override
  45         public boolean equals(Object obj) {
  46             if (obj instanceof Wrapper) {
  47                 return Objects.equals(this.clazz, ((Wrapper) obj).clazz);
  48             } else {
  49                 return false;
  50             }
  51         }
  52 
  53         @Override
  54         public int hashCode() {
  55             return clazz.hashCode();
  56         }
  57     }
  58 
  59     @Override
  60     @SuppressWarnings("try")
  61     protected Suites createSuites() {
  62         try (OverrideScope s = OptionValue.override(GraalOptions.ImmutableCode, true)) {
  63             return super.createSuites();
  64         }
  65     }
  66 
  67     @Override
  68     protected boolean checkLowTierGraph(StructuredGraph graph) {
  69         for (ConstantNode constantNode : graph.getNodes().filter(ConstantNode.class)) {
  70             assert constantNode.asJavaConstant() == null || constantNode.asJavaConstant().getJavaKind() != JavaKind.Object ||
  71                             constantNode.asJavaConstant().isDefaultForKind() : "Found unexpected object constant " +
  72                                             constantNode + ", this should have been removed by the LoadJavaMirrorWithKlassPhase.";
  73         }
  74         return true;
  75     }
  76 
  77     public static Class<?> classConstant() {
  78         return Wrapper.class;
  79     }
  80 
  81     @Test
  82     public void testClassConstant() {
  83         test("classConstant");
  84     }
  85 
  86     public static Class<?> primitiveClassConstant() {
  87         return int.class;
  88     }
  89 
  90     @Test
  91     public void testPrimitiveClassConstant() {
  92         test("primitiveClassConstant");
  93     }
  94 
  95     public static Wrapper compressedClassConstant(Wrapper w) {
  96         w.clazz = Wrapper.class;
  97         return w;
  98     }
  99 
 100     @Test
 101     public void testCompressedClassConstant() {
 102         ArgSupplier arg = () -> new Wrapper();
 103         test("compressedClassConstant", arg);
 104     }
 105 
 106     public static Wrapper compressedPrimitiveClassConstant(Wrapper w) {
 107         w.clazz = int.class;
 108         return w;
 109     }
 110 
 111     @Test
 112     public void testCompressedPrimitiveClassConstant() {
 113         ArgSupplier arg = () -> new Wrapper();
 114         test("compressedPrimitiveClassConstant", arg);
 115     }
 116 }