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 
  60     @Override
  61     @SuppressWarnings("try")
  62     protected Suites createSuites(OptionValues options) {
  63         return super.createSuites(getOptions());
  64     }
  65 
  66     private static OptionValues getOptions() {
  67         return new OptionValues(getInitialOptions(), GraalOptions.ImmutableCode, true);
  68     }
  69 
  70     @Override
  71     protected boolean checkLowTierGraph(StructuredGraph graph) {
  72         for (ConstantNode constantNode : graph.getNodes().filter(ConstantNode.class)) {
  73             assert constantNode.asJavaConstant() == null || constantNode.asJavaConstant().getJavaKind() != JavaKind.Object ||
  74                             constantNode.asJavaConstant().isDefaultForKind() : "Found unexpected object constant " +
  75                                             constantNode + ", this should have been removed by the LoadJavaMirrorWithKlassPhase.";
  76         }
  77         return true;
  78     }
  79 
  80     public static Class<?> classConstant() {
  81         return Wrapper.class;
  82     }
  83 
  84     @Test
  85     public void testClassConstant() {
  86         test(getOptions(), "classConstant");
  87     }
  88 
  89     public static Class<?> primitiveClassConstant() {
  90         return int.class;
  91     }
  92 
  93     @Test
  94     public void testPrimitiveClassConstant() {
  95         test(getOptions(), "primitiveClassConstant");
  96     }
  97 
  98     public static Wrapper compressedClassConstant(Wrapper w) {
  99         w.clazz = Wrapper.class;
 100         return w;
 101     }
 102 
 103     @Test
 104     public void testCompressedClassConstant() {
 105         ArgSupplier arg = () -> new Wrapper();
 106         test(getOptions(), "compressedClassConstant", arg);
 107     }
 108 
 109     public static Wrapper compressedPrimitiveClassConstant(Wrapper w) {
 110         w.clazz = int.class;
 111         return w;
 112     }
 113 
 114     @Test
 115     public void testCompressedPrimitiveClassConstant() {
 116         ArgSupplier arg = () -> new Wrapper();
 117         test(getOptions(), "compressedPrimitiveClassConstant", arg);
 118     }
 119 }