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