langtools/test/tools/javac/TryWithResources/TwrNullTests.java
author darcy
Fri, 18 Feb 2011 15:55:20 -0800
changeset 8434 afb406fc66fe
permissions -rw-r--r--
7020047: Project Coin: generate null-check around try-with-resources close call Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8434
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
     1
/*
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
     4
 *
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
     8
 *
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    13
 * accompanied this code).
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    14
 *
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    18
 *
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    21
 * questions.
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    22
 */
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    23
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    24
/*
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    25
 * @test
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    26
 * @bug 7020047
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    27
 * @summary Test null handling of try-with-resources statement
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    28
 */
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    29
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    30
public class TwrNullTests {
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    31
    /*
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    32
     * Each try-with-resources statement generates two calls to the
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    33
     * close method for each resource: one for when there is a primary
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    34
     * exception present and the second for when a primary exception
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    35
     * is absent.  The null handling of both cases needs to be
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    36
     * checked.
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    37
     */
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    38
    public static void main(String... args) {
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    39
        testNormalCompletion();
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    40
        testNoSuppression();
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    41
    }
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    42
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    43
    /*
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    44
     * Verify empty try-with-resources on a null resource completes
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    45
     * normally; no NPE from the generated close call.
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    46
     */
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    47
    private static void testNormalCompletion() {
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    48
        try(AutoCloseable resource = null) {
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    49
            return; // Nothing to see here, move along.
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    50
        } catch (Exception e) {
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    51
            throw new AssertionError("Should not be reached", e);
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    52
        }
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    53
    }
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    54
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    55
    /*
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    56
     * Verify that a NPE on a null resource is <em>not</em> added as a
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    57
     * suppressed exception to an exception from try block.
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    58
     */
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    59
    private static void testNoSuppression() {
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    60
        try(AutoCloseable resource = null) {
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    61
            throw new java.io.IOException();
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    62
        } catch(java.io.IOException ioe) {
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    63
            Throwable[] suppressed = ioe.getSuppressed();
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    64
            if (suppressed.length != 0) {
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    65
                throw new AssertionError("Non-empty suppressed exceptions",
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    66
                                         ioe);
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    67
            }
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    68
        } catch (Exception e) {
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    69
            throw new AssertionError("Should not be reached", e);
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    70
        }
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    71
    }
afb406fc66fe 7020047: Project Coin: generate null-check around try-with-resources close call
darcy
parents:
diff changeset
    72
}