1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/TryWithResources/InterruptedExceptionTest.java Tue Mar 29 16:41:18 2011 +0100 1.3 @@ -0,0 +1,234 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 7027157 1.30 + * @summary Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException 1.31 + */ 1.32 + 1.33 +import com.sun.source.util.JavacTask; 1.34 +import java.net.URI; 1.35 +import java.util.Arrays; 1.36 +import javax.tools.Diagnostic; 1.37 +import javax.tools.JavaCompiler; 1.38 +import javax.tools.JavaFileObject; 1.39 +import javax.tools.SimpleJavaFileObject; 1.40 +import javax.tools.StandardJavaFileManager; 1.41 +import javax.tools.ToolProvider; 1.42 + 1.43 +public class InterruptedExceptionTest { 1.44 + 1.45 + enum XlintOption { 1.46 + NONE("none"), 1.47 + TRY("try"); 1.48 + 1.49 + String opt; 1.50 + 1.51 + XlintOption(String opt) { 1.52 + this.opt = opt; 1.53 + } 1.54 + 1.55 + String getXlintOption() { 1.56 + return "-Xlint:" + opt; 1.57 + } 1.58 + } 1.59 + 1.60 + enum SuppressLevel { 1.61 + NONE, 1.62 + SUPPRESS; 1.63 + 1.64 + String getSuppressAnno() { 1.65 + return this == SUPPRESS ? 1.66 + "@SuppressWarnings(\"try\")" : 1.67 + ""; 1.68 + } 1.69 + } 1.70 + 1.71 + enum ClassKind { 1.72 + ABSTRACT_CLASS("abstract class", "implements", false), 1.73 + CLASS("class", "implements", true), 1.74 + INTERFACE("interface", "extends", false); 1.75 + 1.76 + String kindName; 1.77 + String extendsClause; 1.78 + boolean hasBody; 1.79 + 1.80 + private ClassKind(String kindName, String extendsClause, boolean hasBody) { 1.81 + this.kindName = kindName; 1.82 + this.extendsClause = extendsClause; 1.83 + this.hasBody = hasBody; 1.84 + } 1.85 + 1.86 + String getBody() { 1.87 + return hasBody ? "{}" : ";"; 1.88 + } 1.89 + } 1.90 + 1.91 + enum ExceptionKind { 1.92 + NONE("", false), 1.93 + EXCEPTION("Exception", true), 1.94 + INTERRUPTED_EXCEPTION("InterruptedException", true), 1.95 + ILLEGAL_ARGUMENT_EXCEPTION("IllegalArgumentException", false), 1.96 + X("X", false); 1.97 + 1.98 + String exName; 1.99 + boolean shouldWarn; 1.100 + 1.101 + private ExceptionKind(String exName, boolean shouldWarn) { 1.102 + this.exName = exName; 1.103 + this.shouldWarn = shouldWarn; 1.104 + } 1.105 + 1.106 + String getThrowsClause() { 1.107 + return this == NONE ? "" : "throws " + exName; 1.108 + } 1.109 + 1.110 + String getTypeArguments(ExceptionKind decl) { 1.111 + return (decl != X || this == NONE) ? "" : "<" + exName + ">"; 1.112 + } 1.113 + 1.114 + String getTypeParameter() { 1.115 + return this == X ? "<X extends Exception>" : ""; 1.116 + } 1.117 + } 1.118 + 1.119 + public static void main(String... args) throws Exception { 1.120 + 1.121 + //create default shared JavaCompiler - reused across multiple compilations 1.122 + JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 1.123 + StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); 1.124 + 1.125 + for (XlintOption xlint : XlintOption.values()) { 1.126 + for (SuppressLevel suppress_decl : SuppressLevel.values()) { 1.127 + for (SuppressLevel suppress_use : SuppressLevel.values()) { 1.128 + for (ClassKind ck : ClassKind.values()) { 1.129 + for (ExceptionKind ek_decl : ExceptionKind.values()) { 1.130 + for (ExceptionKind ek_use : ExceptionKind.values()) { 1.131 + new InterruptedExceptionTest(xlint, suppress_decl, 1.132 + suppress_use, ck, ek_decl, ek_use).run(comp, fm); 1.133 + } 1.134 + } 1.135 + } 1.136 + } 1.137 + } 1.138 + } 1.139 + } 1.140 + 1.141 + XlintOption xlint; 1.142 + SuppressLevel suppress_decl; 1.143 + SuppressLevel suppress_use; 1.144 + ClassKind ck; 1.145 + ExceptionKind ek_decl; 1.146 + ExceptionKind ek_use; 1.147 + JavaSource source; 1.148 + DiagnosticChecker diagChecker; 1.149 + 1.150 + InterruptedExceptionTest(XlintOption xlint, SuppressLevel suppress_decl, SuppressLevel suppress_use, 1.151 + ClassKind ck, ExceptionKind ek_decl, ExceptionKind ek_use) { 1.152 + this.xlint = xlint; 1.153 + this.suppress_decl = suppress_decl; 1.154 + this.suppress_use = suppress_use; 1.155 + this.ck = ck; 1.156 + this.ek_decl = ek_decl; 1.157 + this.ek_use = ek_use; 1.158 + this.source = new JavaSource(); 1.159 + this.diagChecker = new DiagnosticChecker(); 1.160 + } 1.161 + 1.162 + class JavaSource extends SimpleJavaFileObject { 1.163 + 1.164 + String template = "#S1 #CK Resource#G #EC AutoCloseable {\n" + 1.165 + "public void close() #TK #BK\n" + 1.166 + "}\n" + 1.167 + "class Test {\n" + 1.168 + "#S2 <X> void test() {\n" + 1.169 + "try (Resource#PK r = null) { }\n" + 1.170 + "}\n" + 1.171 + "}\n"; 1.172 + 1.173 + String source; 1.174 + 1.175 + public JavaSource() { 1.176 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 1.177 + source = template.replace("#S1", suppress_decl.getSuppressAnno()) 1.178 + .replace("#S2", suppress_use.getSuppressAnno()) 1.179 + .replace("#CK", ck.kindName) 1.180 + .replace("#EC", ck.extendsClause) 1.181 + .replace("#G", ek_decl.getTypeParameter()) 1.182 + .replace("#TK", ek_decl.getThrowsClause()) 1.183 + .replace("#BK", ck.getBody()) 1.184 + .replace("#PK", ek_use.getTypeArguments(ek_decl)); 1.185 + } 1.186 + 1.187 + @Override 1.188 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.189 + return source; 1.190 + } 1.191 + } 1.192 + 1.193 + void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { 1.194 + JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, 1.195 + Arrays.asList(xlint.getXlintOption()), null, Arrays.asList(source)); 1.196 + ct.analyze(); 1.197 + check(); 1.198 + } 1.199 + 1.200 + void check() { 1.201 + 1.202 + boolean shouldWarnDecl = ek_decl.shouldWarn && 1.203 + xlint == XlintOption.TRY && 1.204 + suppress_decl != SuppressLevel.SUPPRESS; 1.205 + 1.206 + boolean shouldWarnUse = (ek_decl.shouldWarn || 1.207 + ((ek_use.shouldWarn || ek_use == ExceptionKind.NONE) && ek_decl == ExceptionKind.X)) && 1.208 + xlint == XlintOption.TRY && 1.209 + suppress_use != SuppressLevel.SUPPRESS; 1.210 + 1.211 + int foundWarnings = 0; 1.212 + 1.213 + if (shouldWarnDecl) foundWarnings++; 1.214 + if (shouldWarnUse) foundWarnings++; 1.215 + 1.216 + if (foundWarnings != diagChecker.tryWarnFound) { 1.217 + throw new Error("invalid diagnostics for source:\n" + 1.218 + source.getCharContent(true) + 1.219 + "\nOptions: " + xlint.getXlintOption() + 1.220 + "\nFound warnings: " + diagChecker.tryWarnFound + 1.221 + "\nExpected decl warning: " + shouldWarnDecl + 1.222 + "\nExpected use warning: " + shouldWarnUse); 1.223 + } 1.224 + } 1.225 + 1.226 + static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { 1.227 + 1.228 + int tryWarnFound; 1.229 + 1.230 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 1.231 + if (diagnostic.getKind() == Diagnostic.Kind.WARNING && 1.232 + diagnostic.getCode().contains("try.resource.throws.interrupted.exc")) { 1.233 + tryWarnFound++; 1.234 + } 1.235 + } 1.236 + } 1.237 +}