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