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