1 /*
   2  * Copyright (c) 2016, 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.replacements.test;
  24 
  25 import static org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.BytecodeExceptionMode.CheckAll;
  26 
  27 import org.junit.Assert;
  28 import org.junit.Test;
  29 
  30 import org.graalvm.compiler.core.common.CompilationIdentifier;
  31 import org.graalvm.compiler.core.phases.HighTier;
  32 import org.graalvm.compiler.core.test.GraalCompilerTest;
  33 import org.graalvm.compiler.nodes.StructuredGraph;
  34 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  35 import org.graalvm.compiler.nodes.ValueNode;
  36 import org.graalvm.compiler.nodes.extended.BytecodeExceptionNode;
  37 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  39 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo;
  40 import org.graalvm.compiler.options.OptionValue;
  41 import org.graalvm.compiler.options.OptionValue.OverrideScope;
  42 import org.graalvm.compiler.phases.tiers.Suites;
  43 
  44 import jdk.vm.ci.meta.ResolvedJavaMethod;
  45 
  46 /**
  47  * Tests compilation of a hot exception handler.
  48  */
  49 public class CompiledNullPointerExceptionTest extends GraalCompilerTest {
  50 
  51     @Override
  52     @SuppressWarnings("try")
  53     protected Suites createSuites() {
  54         try (OverrideScope scope = OptionValue.override(HighTier.Options.Inline, false)) {
  55             return super.createSuites();
  56         }
  57     }
  58 
  59     @Override
  60     protected InlineInfo bytecodeParserShouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
  61         return InlineInfo.DO_NOT_INLINE_NO_EXCEPTION;
  62     }
  63 
  64     @Override
  65     protected GraphBuilderConfiguration editGraphBuilderConfiguration(GraphBuilderConfiguration conf) {
  66         return super.editGraphBuilderConfiguration(conf).withBytecodeExceptionMode(CheckAll);
  67     }
  68 
  69     @Override
  70     protected StructuredGraph parseEager(ResolvedJavaMethod m, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId) {
  71         StructuredGraph graph = super.parseEager(m, allowAssumptions, compilationId);
  72         int handlers = graph.getNodes().filter(BytecodeExceptionNode.class).count();
  73         Assert.assertEquals(1, handlers);
  74         return graph;
  75     }
  76 
  77     private class TestClass {
  78 
  79         @Override
  80         public String toString() {
  81             return "TestClass";
  82         }
  83     }
  84 
  85     @Test
  86     public void test() {
  87         test("testSnippet", (TestClass) null, "object2");
  88         test("testSnippet", new TestClass(), "object2");
  89     }
  90 
  91     public static String testSnippet(TestClass o, Object o2) {
  92         try {
  93             return o.toString();
  94         } catch (NullPointerException e) {
  95             return String.valueOf(o2);
  96         }
  97     }
  98 }