001/*
002 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.test;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.meta.*;
027
028import org.junit.*;
029
030import com.oracle.graal.compiler.test.*;
031import com.oracle.graal.nodes.extended.*;
032
033public class ExplicitExceptionTest extends GraalCompilerTest {
034
035    private int expectedForeignCallCount;
036
037    @Override
038    protected InstalledCode getCode(ResolvedJavaMethod method) {
039        InstalledCode installedCode = super.getCode(method);
040        assertDeepEquals(expectedForeignCallCount, lastCompiledGraph.getNodes().filter(ForeignCallNode.class).count());
041        return installedCode;
042    }
043
044    public static int testAIOOBESnippet(int[] array) {
045        return array[10];
046    }
047
048    @Test
049    public void testAIOOBE() {
050        int[] array = new int[4];
051        for (int i = 0; i < 10000; i++) {
052            try {
053                testAIOOBESnippet(array);
054            } catch (ArrayIndexOutOfBoundsException e) {
055                // nothing to do
056            }
057        }
058        expectedForeignCallCount = 2;
059        test("testAIOOBESnippet", array);
060    }
061
062    public static int testNPEArraySnippet(int[] array) {
063        return array[10];
064    }
065
066    @Test
067    public void testNPEArray() {
068        int[] array = null;
069        for (int i = 0; i < 10000; i++) {
070            try {
071                testNPEArraySnippet(array);
072            } catch (NullPointerException e) {
073                // nothing to do
074            }
075        }
076        expectedForeignCallCount = 2;
077        test("testNPEArraySnippet", array);
078    }
079
080    private static class TestClass {
081        int field;
082    }
083
084    public static int testNPESnippet(TestClass obj) {
085        return obj.field;
086    }
087
088    @SuppressWarnings("unused")
089    @Test
090    public void testNPE() {
091        new TestClass();
092        TestClass obj = null;
093        for (int i = 0; i < 10000; i++) {
094            try {
095                testNPESnippet(obj);
096            } catch (NullPointerException e) {
097                // nothing to do
098            }
099        }
100        expectedForeignCallCount = 1;
101        test("testNPESnippet", obj);
102    }
103
104}