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.common.CompilationIdentifier.INVALID_COMPILATION_ID;
  26 import static org.graalvm.compiler.debug.DelegatingDebugConfig.Feature.INTERCEPT;
  27 
  28 import java.lang.reflect.Method;
  29 
  30 import jdk.vm.ci.meta.MetaAccessProvider;
  31 import jdk.vm.ci.meta.ResolvedJavaMethod;
  32 
  33 import org.junit.Assume;
  34 import org.junit.Test;
  35 
  36 import org.graalvm.compiler.api.test.Graal;
  37 import org.graalvm.compiler.debug.Debug;
  38 import org.graalvm.compiler.debug.DebugConfigScope;
  39 import org.graalvm.compiler.debug.DelegatingDebugConfig;
  40 import org.graalvm.compiler.java.GraphBuilderPhase;
  41 import org.graalvm.compiler.nodes.StructuredGraph;
  42 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  43 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  44 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  45 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  46 import org.graalvm.compiler.phases.OptimisticOptimizations;
  47 import org.graalvm.compiler.phases.PhaseSuite;
  48 import org.graalvm.compiler.phases.VerifyPhase;
  49 import org.graalvm.compiler.phases.tiers.HighTierContext;
  50 import org.graalvm.compiler.phases.util.Providers;
  51 import org.graalvm.compiler.runtime.RuntimeProvider;
  52 import org.graalvm.compiler.test.GraalTest;
  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);
  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         StructuredGraph graph = new StructuredGraph(method, AllowAssumptions.NO, INVALID_COMPILATION_ID);
  96         try (DebugConfigScope s = Debug.setConfig(new DelegatingDebugConfig().disable(INTERCEPT)); Debug.Scope ds = Debug.scope("GraphBuilding", graph, method)) {
  97             graphBuilderSuite.apply(graph, context);
  98         } catch (Throwable e) {
  99             throw Debug.handle(e);
 100         }
 101     }
 102 }