001/*
002 * Copyright (c) 2014, 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 */
023
024package com.oracle.graal.hotspot.test;
025
026import com.oracle.graal.debug.*;
027import com.oracle.graal.debug.Debug.*;
028
029import org.junit.*;
030
031import com.oracle.graal.compiler.test.*;
032import com.oracle.graal.graph.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
035
036public class ClassSubstitutionsTests extends GraalCompilerTest {
037
038    public Number instanceField;
039
040    public Object[] arrayField;
041
042    public String[] stringArrayField;
043
044    protected StructuredGraph test(final String snippet) {
045        try (Scope s = Debug.scope("ClassSubstitutionsTest", getMetaAccess().lookupJavaMethod(getMethod(snippet)))) {
046            StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
047            compile(graph.method(), graph);
048            assertNotInGraph(graph, Invoke.class);
049            Debug.dump(graph, snippet);
050            return graph;
051        } catch (Throwable e) {
052            throw Debug.handle(e);
053        }
054    }
055
056    protected static StructuredGraph assertNotInGraph(StructuredGraph graph, Class<?> clazz) {
057        for (Node node : graph.getNodes()) {
058            if (clazz.isInstance(node)) {
059                fail(node.toString());
060            }
061        }
062        return graph;
063    }
064
065    public boolean constantIsArray() {
066        return "".getClass().isArray();
067    }
068
069    public boolean constantIsInterface() {
070        return "".getClass().isInterface();
071    }
072
073    public boolean constantIsPrimitive() {
074        return "".getClass().isPrimitive();
075    }
076
077    @Test
078    public void testIsArray() {
079        testConstantReturn("constantIsArray", 0);
080    }
081
082    @Test
083    public void testIsInterface() {
084        testConstantReturn("constantIsInterface", 0);
085    }
086
087    @Test
088    public void testIsPrimitive() {
089        testConstantReturn("constantIsPrimitive", 0);
090    }
091
092    public boolean fieldIsNotArray() {
093        if (instanceField != null) {
094            // The base type of instanceField is not Object or an Interface, so it's provably an
095            // instance type, so isArray will always return false.
096            return instanceField.getClass().isArray();
097        }
098        return false;
099    }
100
101    @Test
102    public void testFieldIsNotArray() {
103        testConstantReturn("fieldIsNotArray", 0);
104    }
105
106    public boolean foldComponentType() {
107        return stringArrayField.getClass().getComponentType() == String.class;
108    }
109
110    @Test
111    public void testFoldComponentType() {
112        testConstantReturn("foldComponentType", 1);
113    }
114
115    @Test
116    public void testFieldIsArray() {
117        testConstantReturn("fieldIsArray", 1);
118    }
119
120    public boolean fieldIsArray() {
121        if (arrayField != null) {
122            // The base type of arrayField is an array of some sort so isArray will always return
123            // true.
124            return arrayField.getClass().isArray();
125        }
126        return true;
127    }
128
129    private static class A {
130    }
131
132    private static class B extends A {
133    }
134
135    private static class C {
136    }
137
138    private static final A a = new A();
139    private static final B b = new B();
140    private static final C c = new C();
141
142    public boolean classIsAssignable1() {
143        return a.getClass().isAssignableFrom(a.getClass());
144    }
145
146    public boolean classIsAssignable2() {
147        return a.getClass().isAssignableFrom(b.getClass());
148    }
149
150    public boolean classIsAssignable3() {
151        return a.getClass().isAssignableFrom(c.getClass());
152    }
153
154    public boolean classIsAssignable4() {
155        return b.getClass().isAssignableFrom(a.getClass());
156    }
157
158    public boolean classIsAssignable5() {
159        return c.getClass().isAssignableFrom(b.getClass());
160    }
161
162    public boolean classIsAssignable6() {
163        return int.class.isAssignableFrom(b.getClass());
164    }
165
166    public boolean classIsAssignable7() {
167        return int.class.isAssignableFrom(int.class);
168    }
169
170    @Test
171    public void testClassIsAssignable() {
172        testConstantReturn("classIsAssignable1", 1);
173        testConstantReturn("classIsAssignable2", 1);
174        testConstantReturn("classIsAssignable3", 0);
175        testConstantReturn("classIsAssignable4", 0);
176        testConstantReturn("classIsAssignable5", 0);
177        testConstantReturn("classIsAssignable6", 0);
178        testConstantReturn("classIsAssignable7", 1);
179    }
180
181    private void testConstantReturn(String name, Object value) {
182        StructuredGraph result = test(name);
183        ReturnNode ret = result.getNodes(ReturnNode.TYPE).first();
184        assertDeepEquals(1, result.getNodes(ReturnNode.TYPE).count());
185
186        assertDeepEquals(true, ret.result().isConstant());
187        assertDeepEquals(value, ret.result().asJavaConstant().asBoxedPrimitive());
188    }
189}