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