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.compiler.test;
024
025import static com.oracle.graal.debug.DelegatingDebugConfig.Feature.*;
026
027import java.lang.reflect.*;
028
029import com.oracle.graal.debug.*;
030import jdk.internal.jvmci.meta.*;
031
032import org.junit.*;
033
034import com.oracle.graal.api.runtime.*;
035import com.oracle.graal.graphbuilderconf.*;
036import com.oracle.graal.graphbuilderconf.GraphBuilderConfiguration.Plugins;
037import com.oracle.graal.java.*;
038import com.oracle.graal.nodes.*;
039import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
040import com.oracle.graal.phases.*;
041import com.oracle.graal.phases.tiers.*;
042import com.oracle.graal.phases.util.*;
043import com.oracle.graal.runtime.*;
044import com.oracle.graal.test.*;
045
046/**
047 * Test that interfaces are correctly initialized by a static field resolution during eager graph
048 * building.
049 */
050public class StaticInterfaceFieldTest extends GraalTest {
051
052    private interface I {
053        Object CONST = new Object() {
054        };
055
056    }
057
058    private static final class C implements I {
059        @SuppressWarnings({"static-method", "unused"})
060        public Object test() {
061            return CONST;
062        }
063    }
064
065    @Test
066    public void test() {
067        eagerlyParseMethod(C.class, "test");
068
069    }
070
071    private void eagerlyParseMethod(Class<C> clazz, String methodName) {
072        RuntimeProvider rt = Graal.getRequiredCapability(RuntimeProvider.class);
073        Providers providers = rt.getHostBackend().getProviders();
074        MetaAccessProvider metaAccess = providers.getMetaAccess();
075
076        PhaseSuite<HighTierContext> graphBuilderSuite = new PhaseSuite<>();
077        GraphBuilderConfiguration config = GraphBuilderConfiguration.getEagerDefault(new Plugins(new InvocationPlugins(metaAccess)));
078        graphBuilderSuite.appendPhase(new GraphBuilderPhase(config));
079        HighTierContext context = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.NONE);
080
081        Assume.assumeTrue(VerifyPhase.class.desiredAssertionStatus());
082
083        final Method m = getMethod(clazz, methodName);
084        ResolvedJavaMethod method = metaAccess.lookupJavaMethod(m);
085        StructuredGraph graph = new StructuredGraph(method, AllowAssumptions.NO);
086        try (DebugConfigScope s = Debug.setConfig(new DelegatingDebugConfig().disable(INTERCEPT)); Debug.Scope ds = Debug.scope("GraphBuilding", graph, method)) {
087            graphBuilderSuite.apply(graph, context);
088        } catch (Throwable e) {
089            throw Debug.handle(e);
090        }
091    }
092}