langtools/test/tools/javac/TryWithResources/UnusedResourcesTest.java
author ksrini
Thu, 01 Sep 2011 09:14:25 -0700
changeset 10455 3d070be0fff8
parent 10454 9d5584396849
child 27319 030080f03e4f
permissions -rw-r--r--
7073631: (javac) javac parser improvements for error position reporting Summary: JavacParser improvements for NetBeans, improved by LangTools. Reviewed-by: mcimadamore, jjg Contributed-by: jan.lahoda@oracle.com
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
/*
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
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
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    28
 */
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    29
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    30
import com.sun.source.util.JavacTask;
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
    31
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
    32
import com.sun.tools.javac.api.JavacTool;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.javac.util.JCDiagnostic;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    34
import java.net.URI;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    35
import java.util.Arrays;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    36
import javax.tools.Diagnostic;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    37
import javax.tools.JavaCompiler;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    38
import javax.tools.JavaFileObject;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    39
import javax.tools.SimpleJavaFileObject;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    40
import javax.tools.StandardJavaFileManager;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    41
import javax.tools.ToolProvider;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    42
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    43
public class UnusedResourcesTest {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    44
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    45
    enum XlintOption {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    46
        NONE("none"),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    47
        TRY("try");
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    48
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    49
        String opt;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    50
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    51
        XlintOption(String opt) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    52
            this.opt = 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
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    55
        String getXlintOption() {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    56
            return "-Xlint:" + opt;
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
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    59
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    60
    enum TwrStmt {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    61
        TWR1("res1"),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    62
        TWR2("res2"),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    63
        TWR3("res3");
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    64
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    65
        final String resourceName;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    66
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    67
        private TwrStmt(String resourceName) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    68
            this.resourceName = 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
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    71
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    72
    enum SuppressLevel {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    73
        NONE,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    74
        SUPPRESS;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    75
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    76
        String getSuppressAnno() {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    77
            return this == SUPPRESS ?
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    78
                "@SuppressWarnings(\"try\")" :
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    79
                "";
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    80
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    81
    }
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
    enum ResourceUsage {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    84
        NONE(null),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    85
        USE_R1(TwrStmt.TWR1),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    86
        USE_R2(TwrStmt.TWR2),
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    87
        USE_R3(TwrStmt.TWR3);
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    88
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    89
        TwrStmt stmt;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    90
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    91
        private ResourceUsage(TwrStmt stmt) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    92
            this.stmt = 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
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    95
        String usedResourceName() {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    96
            return stmt != null ? stmt.resourceName : null;
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
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
    99
        boolean isUsedIn(TwrStmt res, TwrStmt stmt) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   100
            return this.stmt == res &&
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   101
                    stmt.ordinal() >= this.stmt.ordinal();
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   102
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   103
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   104
        String getUsage(TwrStmt stmt) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   105
            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
   106
                "use(" + usedResourceName() + ");" :
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   107
                "";
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   108
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   109
    }
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
    static class JavaSource extends SimpleJavaFileObject {
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
        String template = "class Resource implements AutoCloseable {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   114
                              "public void close() {}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   115
                          "}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   116
                          "class Test {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   117
                              "void use(Resource r) {}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   118
                              "#S void test() {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   119
                                 "try (Resource #R1 = new Resource()) {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   120
                                    "#U1_R1\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   121
                                    "#U1_R2\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   122
                                    "#U1_R3\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   123
                                    "try (Resource #R2 = new Resource()) {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   124
                                       "#U2_R1\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   125
                                       "#U2_R2\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   126
                                       "#U2_R3\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   127
                                       "try (Resource #R3 = new Resource()) {\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   128
                                           "#U3_R1\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   129
                                           "#U3_R2\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   130
                                           "#U3_R3\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   131
                                       "}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   132
                                    "}\n" +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   133
                                 "}\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
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   137
        String source;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   138
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   139
        public JavaSource(SuppressLevel suppressLevel, ResourceUsage usage1,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   140
                ResourceUsage usage2, ResourceUsage usage3) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   141
            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
   142
            source = template.replace("#S", suppressLevel.getSuppressAnno()).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   143
                    replace("#R1", TwrStmt.TWR1.resourceName).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   144
                    replace("#R2", TwrStmt.TWR2.resourceName).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   145
                    replace("#R3", TwrStmt.TWR3.resourceName).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   146
                    replace("#U1_R1", usage1.getUsage(TwrStmt.TWR1)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   147
                    replace("#U1_R2", usage2.getUsage(TwrStmt.TWR1)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   148
                    replace("#U1_R3", usage3.getUsage(TwrStmt.TWR1)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   149
                    replace("#U2_R1", usage1.getUsage(TwrStmt.TWR2)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   150
                    replace("#U2_R2", usage2.getUsage(TwrStmt.TWR2)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   151
                    replace("#U2_R3", usage3.getUsage(TwrStmt.TWR2)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   152
                    replace("#U3_R1", usage1.getUsage(TwrStmt.TWR3)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   153
                    replace("#U3_R2", usage2.getUsage(TwrStmt.TWR3)).
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   154
                    replace("#U3_R3", usage3.getUsage(TwrStmt.TWR3));
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   155
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   156
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   157
        @Override
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   158
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   159
            return source;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   160
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   161
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   162
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   163
    public static void main(String... args) throws Exception {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   164
        for (XlintOption xlint : XlintOption.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   165
            for (SuppressLevel suppressLevel : SuppressLevel.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   166
                for (ResourceUsage usage1 : ResourceUsage.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   167
                    for (ResourceUsage usage2 : ResourceUsage.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   168
                        for (ResourceUsage usage3 : ResourceUsage.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   169
                                test(xlint,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   170
                                        suppressLevel,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   171
                                        usage1,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   172
                                        usage2,
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   173
                                        usage3);
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   174
                        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   175
                    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   176
                }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   177
            }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   178
        }
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
    // 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
   182
    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
   183
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   184
    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
   185
                ResourceUsage usage2, ResourceUsage usage3) throws Exception {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   186
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   187
        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
   188
        DiagnosticChecker dc = new DiagnosticChecker();
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   189
        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
   190
                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
   191
        ct.analyze();
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   192
        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
   193
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   194
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   195
    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
   196
                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
   197
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   198
        ResourceUsage[] usages = { usage1, usage2, usage3 };
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   199
        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
   200
        boolean[] usedResources = { false, false, false };
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
        for (TwrStmt res : TwrStmt.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   203
            outer: for (TwrStmt stmt : TwrStmt.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   204
                for (ResourceUsage usage : usages) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   205
                    if (usage.isUsedIn(res, stmt)) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   206
                        usedResources[res.ordinal()] = true;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   207
                        break outer;
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
                }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   210
            }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   211
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   212
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   213
        for (TwrStmt stmt : TwrStmt.values()) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   214
            boolean unused = !usedResources[stmt.ordinal()] &&
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   215
                    xlint == XlintOption.TRY &&
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   216
                    suppressLevel != SuppressLevel.SUPPRESS;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   217
            if (unused != unusedFound[stmt.ordinal()]) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   218
                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
   219
                    source.getCharContent(true) +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   220
                    "\nOptions: " + xlint.getXlintOption() +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   221
                    "\nFound unused res1: " + unusedFound[0] +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   222
                    "\nFound unused res2: " + unusedFound[1] +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   223
                    "\nFound unused res3: " + unusedFound[2] +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   224
                    "\nExpected unused res1: " + !usedResources[0] +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   225
                    "\nExpected unused res2: " + !usedResources[1] +
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   226
                    "\nExpected unused res3: " + !usedResources[2]);
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   227
            }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   228
        }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   229
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   230
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   231
    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
   232
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   233
        boolean unused_r1 = false;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   234
        boolean unused_r2 = false;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   235
        boolean unused_r3 = false;
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
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   238
            if (diagnostic.getKind() == Diagnostic.Kind.WARNING &&
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   239
                    diagnostic.getCode().contains("try.resource.not.referenced")) {
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   240
                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
   241
                if (varName.equals(TwrStmt.TWR1.resourceName)) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   242
                    unused_r1 = true;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   243
                } else if (varName.equals(TwrStmt.TWR2.resourceName)) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   244
                    unused_r2 = true;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   245
                } else if (varName.equals(TwrStmt.TWR3.resourceName)) {
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   246
                    unused_r3 = true;
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   247
                }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   248
            }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   249
        }
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   250
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   251
        private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   252
            if (diagnostic instanceof JCDiagnostic)
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   253
                return (JCDiagnostic) diagnostic;
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   254
            if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   255
                return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   256
            throw new IllegalArgumentException();
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8626
diff changeset
   257
        }
8626
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   258
    }
38b24af530bc 7023233: False positive for -Xlint:try with nested try with resources blocks
mcimadamore
parents:
diff changeset
   259
}