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  */
  23 package org.graalvm.compiler.core.test;
  24 
  25 import static org.graalvm.compiler.core.test.GraalCompilerTest.getInitialOptions;
  26 
  27 import java.lang.reflect.Method;
  28 
  29 import org.graalvm.compiler.api.test.Graal;
  30 import org.graalvm.compiler.debug.DebugCloseable;
  31 import org.graalvm.compiler.debug.DebugHandlersFactory;
  32 import org.graalvm.compiler.debug.DebugContext;
  33 import org.graalvm.compiler.java.GraphBuilderPhase;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  36 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  37 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  38 import org.graalvm.compiler.options.OptionValues;
  39 import org.graalvm.compiler.phases.OptimisticOptimizations;
  40 import org.graalvm.compiler.phases.PhaseSuite;
  41 import org.graalvm.compiler.phases.VerifyPhase;
  42 import org.graalvm.compiler.phases.tiers.HighTierContext;
  43 import org.graalvm.compiler.phases.util.Providers;
  44 import org.graalvm.compiler.runtime.RuntimeProvider;
  45 import org.graalvm.compiler.test.GraalTest;
  46 import org.junit.Assume;
  47 import org.junit.Test;
  48 
  49 import jdk.vm.ci.meta.MetaAccessProvider;
  50 import 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  */
  56 public 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());
  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         OptionValues options = getInitialOptions();
  94         DebugContext debug = DebugContext.create(options, DebugHandlersFactory.LOADER);
  95         StructuredGraph graph = new StructuredGraph.Builder(options, debug).method(method).build();
  96         try (DebugCloseable s = debug.disableIntercept(); DebugContext.Scope ds = debug.scope("GraphBuilding", graph, method)) {
  97             graphBuilderSuite.apply(graph, context);
  98         } catch (Throwable e) {
  99             throw debug.handle(e);
 100         }
 101     }
 102 }