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