1/*
2 * Copyright (c) 2011, 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 */
23package org.graalvm.compiler.hotspot.test;
24
25import org.junit.Test;
26
27import org.graalvm.compiler.core.test.GraalCompilerTest;
28import org.graalvm.compiler.nodes.StructuredGraph;
29import org.graalvm.compiler.nodes.extended.ForeignCallNode;
30import org.graalvm.compiler.options.OptionValues;
31
32import jdk.vm.ci.code.InstalledCode;
33import jdk.vm.ci.meta.ResolvedJavaMethod;
34
35public class ExplicitExceptionTest extends GraalCompilerTest {
36
37    private int expectedForeignCallCount;
38
39    @Override
40    protected InstalledCode getCode(ResolvedJavaMethod method, StructuredGraph graph, boolean forceCompile, boolean installAsDefault, OptionValues options) {
41        InstalledCode installedCode = super.getCode(method, graph, forceCompile, installAsDefault, options);
42        assertDeepEquals(expectedForeignCallCount, lastCompiledGraph.getNodes().filter(ForeignCallNode.class).count());
43        return installedCode;
44    }
45
46    public static int testAIOOBESnippet(int[] array) {
47        return array[10];
48    }
49
50    @Test
51    public void testAIOOBE() {
52        int[] array = new int[4];
53        for (int i = 0; i < 10000; i++) {
54            try {
55                testAIOOBESnippet(array);
56            } catch (ArrayIndexOutOfBoundsException e) {
57                // nothing to do
58            }
59        }
60        expectedForeignCallCount = 2;
61        test("testAIOOBESnippet", array);
62    }
63
64    public static int testNPEArraySnippet(int[] array) {
65        return array[10];
66    }
67
68    @Test
69    public void testNPEArray() {
70        int[] array = null;
71        for (int i = 0; i < 10000; i++) {
72            try {
73                testNPEArraySnippet(array);
74            } catch (NullPointerException e) {
75                // nothing to do
76            }
77        }
78        expectedForeignCallCount = 2;
79        test("testNPEArraySnippet", array);
80    }
81
82    private static class TestClass {
83        int field;
84    }
85
86    public static int testNPESnippet(TestClass obj) {
87        return obj.field;
88    }
89
90    @SuppressWarnings("unused")
91    @Test
92    public void testNPE() {
93        new TestClass();
94        TestClass obj = null;
95        for (int i = 0; i < 10000; i++) {
96            try {
97                testNPESnippet(obj);
98            } catch (NullPointerException e) {
99                // nothing to do
100            }
101        }
102        expectedForeignCallCount = 1;
103        test("testNPESnippet", obj);
104    }
105
106}
107