langtools/test/tools/javac/TryWithResources/UnusedResourcesTest.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 27319 030080f03e4f
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8626
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
8626
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
     4
 *
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
     8
 *
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    14
 *
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    18
 *
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    21
 * questions.
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    22
 */
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    23
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    24
/*
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    25
 * @test
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    26
 * @bug 7023233
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    27
 * @summary False positive for -Xlint:try with nested try with resources blocks
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.file
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.util
8626
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    31
 */
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    32
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    33
import com.sun.source.util.JavacTask;
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
    34
import com.sun.tools.javac.api.ClientCodeWrapper;
8626
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.javac.api.JavacTool;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    36
import com.sun.tools.javac.util.JCDiagnostic;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    37
import java.net.URI;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    38
import java.util.Arrays;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    39
import javax.tools.Diagnostic;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    40
import javax.tools.JavaCompiler;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    41
import javax.tools.JavaFileObject;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    42
import javax.tools.SimpleJavaFileObject;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    43
import javax.tools.StandardJavaFileManager;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    44
import javax.tools.ToolProvider;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    45
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    46
public class UnusedResourcesTest {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    47
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    48
    enum XlintOption {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    49
        NONE("none"),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    50
        TRY("try");
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    51
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    52
        String opt;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    53
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    54
        XlintOption(String opt) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    55
            this.opt = opt;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    56
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    57
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    58
        String getXlintOption() {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    59
            return "-Xlint:" + opt;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    60
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    61
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    62
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    63
    enum TwrStmt {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    64
        TWR1("res1"),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    65
        TWR2("res2"),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    66
        TWR3("res3");
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    67
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    68
        final String resourceName;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    69
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    70
        private TwrStmt(String resourceName) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    71
            this.resourceName = resourceName;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    72
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    73
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    74
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    75
    enum SuppressLevel {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    76
        NONE,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    77
        SUPPRESS;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    78
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    79
        String getSuppressAnno() {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    80
            return this == SUPPRESS ?
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    81
                "@SuppressWarnings(\"try\")" :
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    82
                "";
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    83
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    84
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    85
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    86
    enum ResourceUsage {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    87
        NONE(null),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    88
        USE_R1(TwrStmt.TWR1),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    89
        USE_R2(TwrStmt.TWR2),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    90
        USE_R3(TwrStmt.TWR3);
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    91
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    92
        TwrStmt stmt;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    93
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    94
        private ResourceUsage(TwrStmt stmt) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    95
            this.stmt = stmt;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    96
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    97
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    98
        String usedResourceName() {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    99
            return stmt != null ? stmt.resourceName : null;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   100
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   101
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   102
        boolean isUsedIn(TwrStmt res, TwrStmt stmt) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   103
            return this.stmt == res &&
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   104
                    stmt.ordinal() >= this.stmt.ordinal();
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   105
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   106
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   107
        String getUsage(TwrStmt stmt) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   108
            return this != NONE && stmt.ordinal() >= this.stmt.ordinal() ?
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   109
                "use(" + usedResourceName() + ");" :
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   110
                "";
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   111
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   112
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   113
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   114
    static class JavaSource extends SimpleJavaFileObject {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   115
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   116
        String template = "class Resource implements AutoCloseable {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   117
                              "public void close() {}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   118
                          "}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   119
                          "class Test {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   120
                              "void use(Resource r) {}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   121
                              "#S void test() {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   122
                                 "try (Resource #R1 = new Resource()) {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   123
                                    "#U1_R1\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   124
                                    "#U1_R2\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   125
                                    "#U1_R3\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   126
                                    "try (Resource #R2 = new Resource()) {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   127
                                       "#U2_R1\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   128
                                       "#U2_R2\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   129
                                       "#U2_R3\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   130
                                       "try (Resource #R3 = new Resource()) {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   131
                                           "#U3_R1\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   132
                                           "#U3_R2\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   133
                                           "#U3_R3\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   134
                                       "}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   135
                                    "}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   136
                                 "}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   137
                              "}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   138
                          "}\n";
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   139
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   140
        String source;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   141
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   142
        public JavaSource(SuppressLevel suppressLevel, ResourceUsage usage1,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   143
                ResourceUsage usage2, ResourceUsage usage3) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   144
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   145
            source = template.replace("#S", suppressLevel.getSuppressAnno()).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   146
                    replace("#R1", TwrStmt.TWR1.resourceName).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   147
                    replace("#R2", TwrStmt.TWR2.resourceName).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   148
                    replace("#R3", TwrStmt.TWR3.resourceName).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   149
                    replace("#U1_R1", usage1.getUsage(TwrStmt.TWR1)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   150
                    replace("#U1_R2", usage2.getUsage(TwrStmt.TWR1)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   151
                    replace("#U1_R3", usage3.getUsage(TwrStmt.TWR1)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   152
                    replace("#U2_R1", usage1.getUsage(TwrStmt.TWR2)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   153
                    replace("#U2_R2", usage2.getUsage(TwrStmt.TWR2)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   154
                    replace("#U2_R3", usage3.getUsage(TwrStmt.TWR2)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   155
                    replace("#U3_R1", usage1.getUsage(TwrStmt.TWR3)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   156
                    replace("#U3_R2", usage2.getUsage(TwrStmt.TWR3)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   157
                    replace("#U3_R3", usage3.getUsage(TwrStmt.TWR3));
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   158
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   159
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   160
        @Override
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   161
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   162
            return source;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   163
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   164
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   165
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   166
    public static void main(String... args) throws Exception {
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   167
        try {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   168
            for (XlintOption xlint : XlintOption.values()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   169
                for (SuppressLevel suppressLevel : SuppressLevel.values()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   170
                    for (ResourceUsage usage1 : ResourceUsage.values()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   171
                        for (ResourceUsage usage2 : ResourceUsage.values()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   172
                            for (ResourceUsage usage3 : ResourceUsage.values()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   173
                                    test(xlint,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   174
                                            suppressLevel,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   175
                                            usage1,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   176
                                            usage2,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   177
                                            usage3);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   178
                            }
8626
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   179
                        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   180
                    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   181
                }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   182
            }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   183
        } finally {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 10454
diff changeset
   184
            fm.close();
8626
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   185
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   186
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   187
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   188
    // Create a single file manager and reuse it for each compile to save time.
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   189
    static StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   190
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   191
    static void test(XlintOption xlint, SuppressLevel suppressLevel, ResourceUsage usage1,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   192
                ResourceUsage usage2, ResourceUsage usage3) throws Exception {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   193
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   194
        JavaSource source = new JavaSource(suppressLevel, usage1, usage2, usage3);
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   195
        DiagnosticChecker dc = new DiagnosticChecker();
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   196
        JavacTask ct = (JavacTask)tool.getTask(null, fm, dc,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   197
                Arrays.asList(xlint.getXlintOption()), null, Arrays.asList(source));
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   198
        ct.analyze();
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   199
        check(source, xlint, suppressLevel, usage1, usage2, usage3, dc);
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   200
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   201
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   202
    static void check(JavaSource source, XlintOption xlint, SuppressLevel suppressLevel,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   203
                ResourceUsage usage1, ResourceUsage usage2, ResourceUsage usage3, DiagnosticChecker dc) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   204
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   205
        ResourceUsage[] usages = { usage1, usage2, usage3 };
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   206
        boolean[] unusedFound = { dc.unused_r1, dc.unused_r2, dc.unused_r3 };
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   207
        boolean[] usedResources = { false, false, false };
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   208
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   209
        for (TwrStmt res : TwrStmt.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   210
            outer: for (TwrStmt stmt : TwrStmt.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   211
                for (ResourceUsage usage : usages) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   212
                    if (usage.isUsedIn(res, stmt)) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   213
                        usedResources[res.ordinal()] = true;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   214
                        break outer;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   215
                    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   216
                }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   217
            }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   218
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   219
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   220
        for (TwrStmt stmt : TwrStmt.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   221
            boolean unused = !usedResources[stmt.ordinal()] &&
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   222
                    xlint == XlintOption.TRY &&
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   223
                    suppressLevel != SuppressLevel.SUPPRESS;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   224
            if (unused != unusedFound[stmt.ordinal()]) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   225
                throw new Error("invalid diagnostics for source:\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   226
                    source.getCharContent(true) +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   227
                    "\nOptions: " + xlint.getXlintOption() +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   228
                    "\nFound unused res1: " + unusedFound[0] +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   229
                    "\nFound unused res2: " + unusedFound[1] +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   230
                    "\nFound unused res3: " + unusedFound[2] +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   231
                    "\nExpected unused res1: " + !usedResources[0] +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   232
                    "\nExpected unused res2: " + !usedResources[1] +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   233
                    "\nExpected unused res3: " + !usedResources[2]);
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   234
            }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   235
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   236
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   237
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   238
    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   239
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   240
        boolean unused_r1 = false;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   241
        boolean unused_r2 = false;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   242
        boolean unused_r3 = false;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   243
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   244
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   245
            if (diagnostic.getKind() == Diagnostic.Kind.WARNING &&
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   246
                    diagnostic.getCode().contains("try.resource.not.referenced")) {
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   247
                String varName = unwrap(diagnostic).getArgs()[0].toString();
8626
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   248
                if (varName.equals(TwrStmt.TWR1.resourceName)) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   249
                    unused_r1 = true;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   250
                } else if (varName.equals(TwrStmt.TWR2.resourceName)) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   251
                    unused_r2 = true;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   252
                } else if (varName.equals(TwrStmt.TWR3.resourceName)) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   253
                    unused_r3 = true;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   254
                }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   255
            }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   256
        }
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   257
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   258
        private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   259
            if (diagnostic instanceof JCDiagnostic)
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   260
                return (JCDiagnostic) diagnostic;
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   261
            if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   262
                return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   263
            throw new IllegalArgumentException();
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   264
        }
8626
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   265
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   266
}