hotspot/test/compiler/6478991/NullCheckTest.java
author jlahoda
Tue, 26 Nov 2013 15:27:19 +0100
changeset 21896 f39003ba351f
parent 10015 ddfb84d0d0c1
permissions -rw-r--r--
8026374: javac accepts void as a method parameter Summary: Changing Check.validate to reject void types. Reviewed-by: jjg, vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     1
/*
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     4
 *
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     8
 *
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    13
 * accompanied this code).
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    14
 *
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    18
 *
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    21
 * questions.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    22
 *
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    23
 */
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    24
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    25
/**
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    26
 * @test
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    27
 * @bug 6478991
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    28
 * @summary C1 NullCheckEliminator yields incorrect exceptions
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    29
 *
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    30
 * @run main/othervm -XX:CompileOnly=NullCheckTest.test,NullCheckTest.inlined  -Xcomp NullCheckTest
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    31
 */
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    32
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    33
public class NullCheckTest {
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    34
        static class A {
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    35
                int f;
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    36
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    37
                public final void inlined(A a) {
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    38
                        // This cast is intended to fail.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    39
                        B b = ((B) a);
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    40
                }
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    41
        }
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    42
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    43
        static class B extends A {
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    44
        }
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    45
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    46
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    47
        private static void test(A a1, A a2) {
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    48
                // Inlined call must do a null check on a1.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    49
                // However, the exlipcit NullCheck instruction is eliminated and
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    50
                // the null check is folded into the field load below, so the
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    51
                // exception in the inlined method is thrown before the null check
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    52
                // and the NullPointerException is not thrown.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    53
                a1.inlined(a2);
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    54
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    55
                int x = a1.f;
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    56
        }
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    57
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    58
        public static void main(String[] args) {
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    59
                // load classes
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    60
                new B();
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    61
                try {
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    62
                        test(null, new A());
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    63
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    64
                        throw new InternalError("FAILURE: no exception");
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    65
                } catch (NullPointerException ex) {
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    66
                        System.out.println("CORRECT: NullPointerException");
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    67
                } catch (ClassCastException ex) {
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    68
                        System.out.println("FAILURE: ClassCastException");
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    69
                        throw ex;
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    70
                }
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    71
        }
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    72
}