001/*
002 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.test;
024
025import java.util.*;
026
027import jdk.internal.jvmci.meta.*;
028import jdk.internal.jvmci.options.*;
029import jdk.internal.jvmci.options.OptionValue.*;
030
031import org.junit.*;
032
033import com.oracle.graal.compiler.common.*;
034import com.oracle.graal.compiler.test.*;
035import com.oracle.graal.nodes.*;
036import com.oracle.graal.phases.tiers.*;
037
038public class LoadJavaMirrorWithKlassTest extends GraalCompilerTest {
039
040    private static class Wrapper {
041        private Class<?> clazz;
042
043        @Override
044        public boolean equals(Object obj) {
045            if (obj instanceof Wrapper) {
046                return Objects.equals(this.clazz, ((Wrapper) obj).clazz);
047            } else {
048                return false;
049            }
050        }
051
052        @Override
053        public int hashCode() {
054            return clazz.hashCode();
055        }
056    }
057
058    @Override
059    protected Suites createSuites() {
060        try (OverrideScope s = OptionValue.override(GraalOptions.ImmutableCode, true)) {
061            return super.createSuites();
062        }
063    }
064
065    @Override
066    protected boolean checkLowTierGraph(StructuredGraph graph) {
067        for (ConstantNode constantNode : graph.getNodes().filter(ConstantNode.class)) {
068            assert constantNode.asJavaConstant() == null || constantNode.asJavaConstant().getKind() != Kind.Object || constantNode.asJavaConstant().isDefaultForKind() : "Found unexpected object constant " +
069                            constantNode + ", this should have been removed by the LoadJavaMirrorWithKlassPhase.";
070        }
071        return true;
072    }
073
074    public static Class<?> classConstant() {
075        return Wrapper.class;
076    }
077
078    @Test
079    public void testClassConstant() {
080        test("classConstant");
081    }
082
083    public static Class<?> primitiveClassConstant() {
084        return int.class;
085    }
086
087    @Test
088    public void testPrimitiveClassConstant() {
089        test("primitiveClassConstant");
090    }
091
092    public static Wrapper compressedClassConstant(Wrapper w) {
093        w.clazz = Wrapper.class;
094        return w;
095    }
096
097    @Test
098    public void testCompressedClassConstant() {
099        ArgSupplier arg = () -> new Wrapper();
100        test("compressedClassConstant", arg);
101    }
102
103    public static Wrapper compressedPrimitiveClassConstant(Wrapper w) {
104        w.clazz = int.class;
105        return w;
106    }
107
108    @Test
109    public void testCompressedPrimitiveClassConstant() {
110        ArgSupplier arg = () -> new Wrapper();
111        test("compressedPrimitiveClassConstant", arg);
112    }
113}