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