904N/A/*
904N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
904N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
904N/A *
904N/A * This code is free software; you can redistribute it and/or modify it
904N/A * under the terms of the GNU General Public License version 2 only, as
904N/A * published by the Free Software Foundation.
904N/A *
904N/A * This code is distributed in the hope that it will be useful, but WITHOUT
904N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
904N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
904N/A * version 2 for more details (a copy is included in the LICENSE file that
904N/A * accompanied this code).
904N/A *
904N/A * You should have received a copy of the GNU General Public License version
904N/A * 2 along with this work; if not, write to the Free Software Foundation,
904N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
904N/A *
904N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
904N/A * or visit www.oracle.com if you need additional information or have any
904N/A * questions.
904N/A */
904N/A
904N/A/*
904N/A * @test
904N/A * @bug 7023233
904N/A * @summary False positive for -Xlint:try with nested try with resources blocks
904N/A */
904N/A
904N/Aimport com.sun.source.util.JavacTask;
1058N/Aimport com.sun.tools.javac.api.ClientCodeWrapper;
904N/Aimport com.sun.tools.javac.api.JavacTool;
904N/Aimport com.sun.tools.javac.util.JCDiagnostic;
904N/Aimport java.net.URI;
904N/Aimport java.util.Arrays;
904N/Aimport javax.tools.Diagnostic;
904N/Aimport javax.tools.JavaCompiler;
904N/Aimport javax.tools.JavaFileObject;
904N/Aimport javax.tools.SimpleJavaFileObject;
904N/Aimport javax.tools.StandardJavaFileManager;
904N/Aimport javax.tools.ToolProvider;
904N/A
904N/Apublic class UnusedResourcesTest {
904N/A
904N/A enum XlintOption {
904N/A NONE("none"),
904N/A TRY("try");
904N/A
904N/A String opt;
904N/A
904N/A XlintOption(String opt) {
904N/A this.opt = opt;
904N/A }
904N/A
904N/A String getXlintOption() {
904N/A return "-Xlint:" + opt;
904N/A }
904N/A }
904N/A
904N/A enum TwrStmt {
904N/A TWR1("res1"),
904N/A TWR2("res2"),
904N/A TWR3("res3");
904N/A
904N/A final String resourceName;
904N/A
904N/A private TwrStmt(String resourceName) {
904N/A this.resourceName = resourceName;
904N/A }
904N/A }
904N/A
904N/A enum SuppressLevel {
904N/A NONE,
904N/A SUPPRESS;
904N/A
904N/A String getSuppressAnno() {
904N/A return this == SUPPRESS ?
904N/A "@SuppressWarnings(\"try\")" :
904N/A "";
904N/A }
904N/A }
904N/A
904N/A enum ResourceUsage {
904N/A NONE(null),
904N/A USE_R1(TwrStmt.TWR1),
904N/A USE_R2(TwrStmt.TWR2),
904N/A USE_R3(TwrStmt.TWR3);
904N/A
904N/A TwrStmt stmt;
904N/A
904N/A private ResourceUsage(TwrStmt stmt) {
904N/A this.stmt = stmt;
904N/A }
904N/A
904N/A String usedResourceName() {
904N/A return stmt != null ? stmt.resourceName : null;
904N/A }
904N/A
904N/A boolean isUsedIn(TwrStmt res, TwrStmt stmt) {
904N/A return this.stmt == res &&
904N/A stmt.ordinal() >= this.stmt.ordinal();
904N/A }
904N/A
904N/A String getUsage(TwrStmt stmt) {
904N/A return this != NONE && stmt.ordinal() >= this.stmt.ordinal() ?
904N/A "use(" + usedResourceName() + ");" :
904N/A "";
904N/A }
904N/A }
904N/A
904N/A static class JavaSource extends SimpleJavaFileObject {
904N/A
904N/A String template = "class Resource implements AutoCloseable {\n" +
904N/A "public void close() {}\n" +
904N/A "}\n" +
904N/A "class Test {\n" +
904N/A "void use(Resource r) {}\n" +
904N/A "#S void test() {\n" +
904N/A "try (Resource #R1 = new Resource()) {\n" +
904N/A "#U1_R1\n" +
904N/A "#U1_R2\n" +
904N/A "#U1_R3\n" +
904N/A "try (Resource #R2 = new Resource()) {\n" +
904N/A "#U2_R1\n" +
904N/A "#U2_R2\n" +
904N/A "#U2_R3\n" +
904N/A "try (Resource #R3 = new Resource()) {\n" +
904N/A "#U3_R1\n" +
904N/A "#U3_R2\n" +
904N/A "#U3_R3\n" +
904N/A "}\n" +
904N/A "}\n" +
904N/A "}\n" +
904N/A "}\n" +
904N/A "}\n";
904N/A
904N/A String source;
904N/A
904N/A public JavaSource(SuppressLevel suppressLevel, ResourceUsage usage1,
904N/A ResourceUsage usage2, ResourceUsage usage3) {
904N/A super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
904N/A source = template.replace("#S", suppressLevel.getSuppressAnno()).
904N/A replace("#R1", TwrStmt.TWR1.resourceName).
904N/A replace("#R2", TwrStmt.TWR2.resourceName).
904N/A replace("#R3", TwrStmt.TWR3.resourceName).
904N/A replace("#U1_R1", usage1.getUsage(TwrStmt.TWR1)).
904N/A replace("#U1_R2", usage2.getUsage(TwrStmt.TWR1)).
904N/A replace("#U1_R3", usage3.getUsage(TwrStmt.TWR1)).
904N/A replace("#U2_R1", usage1.getUsage(TwrStmt.TWR2)).
904N/A replace("#U2_R2", usage2.getUsage(TwrStmt.TWR2)).
904N/A replace("#U2_R3", usage3.getUsage(TwrStmt.TWR2)).
904N/A replace("#U3_R1", usage1.getUsage(TwrStmt.TWR3)).
904N/A replace("#U3_R2", usage2.getUsage(TwrStmt.TWR3)).
904N/A replace("#U3_R3", usage3.getUsage(TwrStmt.TWR3));
904N/A }
904N/A
904N/A @Override
904N/A public CharSequence getCharContent(boolean ignoreEncodingErrors) {
904N/A return source;
904N/A }
904N/A }
904N/A
904N/A public static void main(String... args) throws Exception {
904N/A for (XlintOption xlint : XlintOption.values()) {
904N/A for (SuppressLevel suppressLevel : SuppressLevel.values()) {
904N/A for (ResourceUsage usage1 : ResourceUsage.values()) {
904N/A for (ResourceUsage usage2 : ResourceUsage.values()) {
904N/A for (ResourceUsage usage3 : ResourceUsage.values()) {
904N/A test(xlint,
904N/A suppressLevel,
904N/A usage1,
904N/A usage2,
904N/A usage3);
904N/A }
904N/A }
904N/A }
904N/A }
904N/A }
904N/A }
904N/A
904N/A // Create a single file manager and reuse it for each compile to save time.
904N/A static StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
904N/A
904N/A static void test(XlintOption xlint, SuppressLevel suppressLevel, ResourceUsage usage1,
904N/A ResourceUsage usage2, ResourceUsage usage3) throws Exception {
904N/A final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
904N/A JavaSource source = new JavaSource(suppressLevel, usage1, usage2, usage3);
904N/A DiagnosticChecker dc = new DiagnosticChecker();
904N/A JavacTask ct = (JavacTask)tool.getTask(null, fm, dc,
904N/A Arrays.asList(xlint.getXlintOption()), null, Arrays.asList(source));
904N/A ct.analyze();
904N/A check(source, xlint, suppressLevel, usage1, usage2, usage3, dc);
904N/A }
904N/A
904N/A static void check(JavaSource source, XlintOption xlint, SuppressLevel suppressLevel,
904N/A ResourceUsage usage1, ResourceUsage usage2, ResourceUsage usage3, DiagnosticChecker dc) {
904N/A
904N/A ResourceUsage[] usages = { usage1, usage2, usage3 };
904N/A boolean[] unusedFound = { dc.unused_r1, dc.unused_r2, dc.unused_r3 };
904N/A boolean[] usedResources = { false, false, false };
904N/A
904N/A for (TwrStmt res : TwrStmt.values()) {
904N/A outer: for (TwrStmt stmt : TwrStmt.values()) {
904N/A for (ResourceUsage usage : usages) {
904N/A if (usage.isUsedIn(res, stmt)) {
904N/A usedResources[res.ordinal()] = true;
904N/A break outer;
904N/A }
904N/A }
904N/A }
904N/A }
904N/A
904N/A for (TwrStmt stmt : TwrStmt.values()) {
904N/A boolean unused = !usedResources[stmt.ordinal()] &&
904N/A xlint == XlintOption.TRY &&
904N/A suppressLevel != SuppressLevel.SUPPRESS;
904N/A if (unused != unusedFound[stmt.ordinal()]) {
904N/A throw new Error("invalid diagnostics for source:\n" +
904N/A source.getCharContent(true) +
904N/A "\nOptions: " + xlint.getXlintOption() +
904N/A "\nFound unused res1: " + unusedFound[0] +
904N/A "\nFound unused res2: " + unusedFound[1] +
904N/A "\nFound unused res3: " + unusedFound[2] +
904N/A "\nExpected unused res1: " + !usedResources[0] +
904N/A "\nExpected unused res2: " + !usedResources[1] +
904N/A "\nExpected unused res3: " + !usedResources[2]);
904N/A }
904N/A }
904N/A }
904N/A
904N/A static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
904N/A
904N/A boolean unused_r1 = false;
904N/A boolean unused_r2 = false;
904N/A boolean unused_r3 = false;
904N/A
904N/A public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
904N/A if (diagnostic.getKind() == Diagnostic.Kind.WARNING &&
904N/A diagnostic.getCode().contains("try.resource.not.referenced")) {
1058N/A String varName = unwrap(diagnostic).getArgs()[0].toString();
904N/A if (varName.equals(TwrStmt.TWR1.resourceName)) {
904N/A unused_r1 = true;
904N/A } else if (varName.equals(TwrStmt.TWR2.resourceName)) {
904N/A unused_r2 = true;
904N/A } else if (varName.equals(TwrStmt.TWR3.resourceName)) {
904N/A unused_r3 = true;
904N/A }
904N/A }
904N/A }
1058N/A
1058N/A private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
1058N/A if (diagnostic instanceof JCDiagnostic)
1058N/A return (JCDiagnostic) diagnostic;
1058N/A if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
1058N/A return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
1058N/A throw new IllegalArgumentException();
1058N/A }
904N/A }
904N/A}