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