StaticInterfaceFieldTest.java revision 12968:4d8a004e5c6d
1/*
2 * Copyright (c) 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 */
23package org.graalvm.compiler.core.test;
24
25import static org.graalvm.compiler.core.test.GraalCompilerTest.getInitialOptions;
26import static org.graalvm.compiler.debug.DelegatingDebugConfig.Feature.INTERCEPT;
27
28import java.lang.reflect.Method;
29
30import org.graalvm.compiler.api.test.Graal;
31import org.graalvm.compiler.debug.Debug;
32import org.graalvm.compiler.debug.DebugConfigScope;
33import org.graalvm.compiler.debug.DelegatingDebugConfig;
34import org.graalvm.compiler.java.GraphBuilderPhase;
35import org.graalvm.compiler.nodes.StructuredGraph;
36import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
37import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
38import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
39import org.graalvm.compiler.phases.OptimisticOptimizations;
40import org.graalvm.compiler.phases.PhaseSuite;
41import org.graalvm.compiler.phases.VerifyPhase;
42import org.graalvm.compiler.phases.tiers.HighTierContext;
43import org.graalvm.compiler.phases.util.Providers;
44import org.graalvm.compiler.runtime.RuntimeProvider;
45import org.graalvm.compiler.test.GraalTest;
46import org.junit.Assume;
47import org.junit.Test;
48
49import jdk.vm.ci.meta.MetaAccessProvider;
50import jdk.vm.ci.meta.ResolvedJavaMethod;
51
52/**
53 * Test that interfaces are correctly initialized by a static field resolution during eager graph
54 * building.
55 */
56public class StaticInterfaceFieldTest extends GraalTest {
57
58    private interface I {
59        Object CONST = new Object() {
60        };
61
62    }
63
64    private static final class C implements I {
65        @SuppressWarnings({"static-method", "unused"})
66        public Object test() {
67            return CONST;
68        }
69    }
70
71    @Test
72    public void test() {
73        eagerlyParseMethod(C.class, "test");
74
75    }
76
77    @SuppressWarnings("try")
78    private void eagerlyParseMethod(Class<C> clazz, String methodName) {
79        RuntimeProvider rt = Graal.getRequiredCapability(RuntimeProvider.class);
80        Providers providers = rt.getHostBackend().getProviders();
81        MetaAccessProvider metaAccess = providers.getMetaAccess();
82
83        PhaseSuite<HighTierContext> graphBuilderSuite = new PhaseSuite<>();
84        Plugins plugins = new Plugins(new InvocationPlugins(metaAccess));
85        GraphBuilderConfiguration config = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true);
86        graphBuilderSuite.appendPhase(new GraphBuilderPhase(config));
87        HighTierContext context = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.NONE);
88
89        Assume.assumeTrue(VerifyPhase.class.desiredAssertionStatus());
90
91        final Method m = getMethod(clazz, methodName);
92        ResolvedJavaMethod method = metaAccess.lookupJavaMethod(m);
93        StructuredGraph graph = new StructuredGraph.Builder(getInitialOptions()).method(method).build();
94        try (DebugConfigScope s = Debug.setConfig(new DelegatingDebugConfig().disable(INTERCEPT)); Debug.Scope ds = Debug.scope("GraphBuilding", graph, method)) {
95            graphBuilderSuite.apply(graph, context);
96        } catch (Throwable e) {
97            throw Debug.handle(e);
98        }
99    }
100}
101