test/tools/javac/varargs/warning/Warn5.java

Tue, 08 Jan 2013 13:47:57 +0000

author
vromero
date
Tue, 08 Jan 2013 13:47:57 +0000
changeset 1482
954541f13717
parent 1108
b5d0b8effc85
child 1520
5c956be64b9e
permissions
-rw-r--r--

8005167: execution time of combo tests in javac should be improved
Reviewed-by: jjg, jjh

mcimadamore@795 1 /*
vromero@1482 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
mcimadamore@795 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@795 4 *
mcimadamore@795 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@795 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@795 7 * published by the Free Software Foundation.
mcimadamore@795 8 *
mcimadamore@795 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@795 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@795 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@795 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@795 13 * accompanied this code).
mcimadamore@795 14 *
mcimadamore@795 15 * You should have received a copy of the GNU General Public License version
mcimadamore@795 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@795 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@795 18 *
mcimadamore@795 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@795 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@795 21 * questions.
mcimadamore@795 22 */
mcimadamore@795 23
mcimadamore@795 24 /**
mcimadamore@795 25 * @test
mcimadamore@1108 26 * @bug 6993978 7097436
mcimadamore@795 27 * @summary Project Coin: Annotation to reduce varargs warnings
mcimadamore@795 28 * @author mcimadamore
vromero@1482 29 * @library ../../lib
vromero@1482 30 * @build JavacTestingAbstractThreadedTest
mcimadamore@795 31 * @run main Warn5
mcimadamore@795 32 */
mcimadamore@795 33 import java.net.URI;
mcimadamore@795 34 import java.util.Arrays;
mcimadamore@1108 35 import java.util.EnumSet;
mcimadamore@795 36 import javax.tools.Diagnostic;
mcimadamore@795 37 import javax.tools.JavaCompiler;
mcimadamore@795 38 import javax.tools.JavaFileObject;
mcimadamore@795 39 import javax.tools.SimpleJavaFileObject;
mcimadamore@795 40 import javax.tools.ToolProvider;
vromero@1482 41 import com.sun.source.util.JavacTask;
mcimadamore@795 42
vromero@1482 43 public class Warn5
vromero@1482 44 extends JavacTestingAbstractThreadedTest
vromero@1482 45 implements Runnable {
mcimadamore@795 46
mcimadamore@795 47 enum XlintOption {
mcimadamore@795 48 NONE("none"),
mcimadamore@795 49 ALL("all");
mcimadamore@795 50
mcimadamore@795 51 String opt;
mcimadamore@795 52
mcimadamore@795 53 XlintOption(String opt) {
mcimadamore@795 54 this.opt = opt;
mcimadamore@795 55 }
mcimadamore@795 56
mcimadamore@795 57 String getXlintOption() {
mcimadamore@795 58 return "-Xlint:" + opt;
mcimadamore@795 59 }
mcimadamore@795 60 }
mcimadamore@795 61
mcimadamore@795 62 enum TrustMe {
mcimadamore@795 63 DONT_TRUST(""),
mcimadamore@795 64 TRUST("@java.lang.SafeVarargs");
mcimadamore@795 65
mcimadamore@795 66 String anno;
mcimadamore@795 67
mcimadamore@795 68 TrustMe(String anno) {
mcimadamore@795 69 this.anno = anno;
mcimadamore@795 70 }
mcimadamore@795 71 }
mcimadamore@795 72
mcimadamore@795 73 enum SuppressLevel {
mcimadamore@795 74 NONE,
mcimadamore@795 75 VARARGS;
mcimadamore@795 76
mcimadamore@795 77 String getSuppressAnno() {
mcimadamore@795 78 return this == VARARGS ?
mcimadamore@795 79 "@SuppressWarnings(\"varargs\")" :
mcimadamore@795 80 "";
mcimadamore@795 81 }
mcimadamore@795 82 }
mcimadamore@795 83
mcimadamore@795 84 enum ModifierKind {
mcimadamore@795 85 NONE(""),
mcimadamore@795 86 FINAL("final"),
mcimadamore@795 87 STATIC("static");
mcimadamore@795 88
mcimadamore@795 89 String mod;
mcimadamore@795 90
mcimadamore@795 91 ModifierKind(String mod) {
mcimadamore@795 92 this.mod = mod;
mcimadamore@795 93 }
mcimadamore@795 94 }
mcimadamore@795 95
mcimadamore@795 96 enum MethodKind {
mcimadamore@795 97 METHOD("void m"),
mcimadamore@795 98 CONSTRUCTOR("Test");
mcimadamore@795 99
mcimadamore@795 100 String name;
mcimadamore@795 101
mcimadamore@795 102 MethodKind(String name) {
mcimadamore@795 103 this.name = name;
mcimadamore@795 104 }
mcimadamore@795 105 }
mcimadamore@795 106
mcimadamore@795 107 enum SourceLevel {
mcimadamore@795 108 JDK_6("6"),
mcimadamore@795 109 JDK_7("7");
mcimadamore@795 110
mcimadamore@795 111 String sourceKey;
mcimadamore@795 112
mcimadamore@795 113 SourceLevel(String sourceKey) {
mcimadamore@795 114 this.sourceKey = sourceKey;
mcimadamore@795 115 }
mcimadamore@795 116 }
mcimadamore@795 117
mcimadamore@795 118 enum SignatureKind {
mcimadamore@795 119 VARARGS_X("#K <X>#N(X... x)", false, true),
mcimadamore@795 120 VARARGS_STRING("#K #N(String... x)", true, true),
mcimadamore@795 121 ARRAY_X("#K <X>#N(X[] x)", false, false),
mcimadamore@795 122 ARRAY_STRING("#K #N(String[] x)", true, false);
mcimadamore@795 123
mcimadamore@795 124 String stub;
mcimadamore@795 125 boolean isReifiableArg;
mcimadamore@795 126 boolean isVarargs;
mcimadamore@795 127
mcimadamore@795 128 SignatureKind(String stub, boolean isReifiableArg, boolean isVarargs) {
mcimadamore@795 129 this.stub = stub;
mcimadamore@795 130 this.isReifiableArg = isReifiableArg;
mcimadamore@795 131 this.isVarargs = isVarargs;
mcimadamore@795 132 }
mcimadamore@795 133
mcimadamore@795 134 String getSignature(ModifierKind modKind, MethodKind methKind) {
mcimadamore@795 135 return methKind != MethodKind.CONSTRUCTOR ?
mcimadamore@795 136 stub.replace("#K", modKind.mod).replace("#N", methKind.name) :
mcimadamore@795 137 stub.replace("#K", "").replace("#N", methKind.name);
mcimadamore@795 138 }
mcimadamore@795 139 }
mcimadamore@795 140
mcimadamore@795 141 enum BodyKind {
mcimadamore@795 142 ASSIGN("Object o = x;", true),
mcimadamore@795 143 CAST("Object o = (Object)x;", true),
mcimadamore@795 144 METH("test(x);", true),
mcimadamore@795 145 PRINT("System.out.println(x.toString());", false),
mcimadamore@795 146 ARRAY_ASSIGN("Object[] o = x;", true),
mcimadamore@795 147 ARRAY_CAST("Object[] o = (Object[])x;", true),
mcimadamore@795 148 ARRAY_METH("testArr(x);", true);
mcimadamore@795 149
mcimadamore@795 150 String body;
mcimadamore@795 151 boolean hasAliasing;
mcimadamore@795 152
mcimadamore@795 153 BodyKind(String body, boolean hasAliasing) {
mcimadamore@795 154 this.body = body;
mcimadamore@795 155 this.hasAliasing = hasAliasing;
mcimadamore@795 156 }
mcimadamore@795 157 }
mcimadamore@795 158
mcimadamore@1108 159 enum WarningKind {
mcimadamore@1108 160 UNSAFE_BODY,
mcimadamore@1108 161 UNSAFE_DECL,
mcimadamore@1108 162 MALFORMED_SAFEVARARGS,
mcimadamore@1108 163 REDUNDANT_SAFEVARARGS;
mcimadamore@1108 164 }
mcimadamore@1108 165
mcimadamore@1108 166 public static void main(String... args) throws Exception {
mcimadamore@1108 167 for (SourceLevel sourceLevel : SourceLevel.values()) {
mcimadamore@1108 168 for (XlintOption xlint : XlintOption.values()) {
mcimadamore@1108 169 for (TrustMe trustMe : TrustMe.values()) {
mcimadamore@1108 170 for (SuppressLevel suppressLevel : SuppressLevel.values()) {
mcimadamore@1108 171 for (ModifierKind modKind : ModifierKind.values()) {
mcimadamore@1108 172 for (MethodKind methKind : MethodKind.values()) {
mcimadamore@1108 173 for (SignatureKind sig : SignatureKind.values()) {
mcimadamore@1108 174 for (BodyKind body : BodyKind.values()) {
vromero@1482 175 pool.execute(new Warn5(sourceLevel,
vromero@1482 176 xlint, trustMe, suppressLevel,
vromero@1482 177 modKind, methKind, sig, body));
mcimadamore@1108 178 }
mcimadamore@1108 179 }
mcimadamore@1108 180 }
mcimadamore@1108 181 }
mcimadamore@1108 182 }
mcimadamore@1108 183 }
mcimadamore@1108 184 }
mcimadamore@1108 185 }
vromero@1482 186
vromero@1482 187 checkAfterExec(false);
mcimadamore@1108 188 }
mcimadamore@1108 189
mcimadamore@1108 190 final SourceLevel sourceLevel;
mcimadamore@1108 191 final XlintOption xlint;
mcimadamore@1108 192 final TrustMe trustMe;
mcimadamore@1108 193 final SuppressLevel suppressLevel;
mcimadamore@1108 194 final ModifierKind modKind;
mcimadamore@1108 195 final MethodKind methKind;
mcimadamore@1108 196 final SignatureKind sig;
mcimadamore@1108 197 final BodyKind body;
mcimadamore@1108 198 final JavaSource source;
mcimadamore@1108 199 final DiagnosticChecker dc;
mcimadamore@1108 200
vromero@1482 201 public Warn5(SourceLevel sourceLevel, XlintOption xlint, TrustMe trustMe,
vromero@1482 202 SuppressLevel suppressLevel, ModifierKind modKind,
vromero@1482 203 MethodKind methKind, SignatureKind sig, BodyKind body) {
mcimadamore@1108 204 this.sourceLevel = sourceLevel;
mcimadamore@1108 205 this.xlint = xlint;
mcimadamore@1108 206 this.trustMe = trustMe;
mcimadamore@1108 207 this.suppressLevel = suppressLevel;
mcimadamore@1108 208 this.modKind = modKind;
mcimadamore@1108 209 this.methKind = methKind;
mcimadamore@1108 210 this.sig = sig;
mcimadamore@1108 211 this.body = body;
mcimadamore@1108 212 this.source = new JavaSource();
mcimadamore@1108 213 this.dc = new DiagnosticChecker();
mcimadamore@1108 214 }
mcimadamore@1108 215
vromero@1482 216 @Override
vromero@1482 217 public void run() {
mcimadamore@1108 218 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
vromero@1482 219 JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), dc,
vromero@1482 220 Arrays.asList(xlint.getXlintOption(),
vromero@1482 221 "-source", sourceLevel.sourceKey),
vromero@1482 222 null, Arrays.asList(source));
vromero@1482 223 try {
vromero@1482 224 ct.analyze();
vromero@1482 225 } catch (Throwable t) {
vromero@1482 226 processException(t);
vromero@1482 227 }
mcimadamore@1108 228 check();
mcimadamore@1108 229 }
mcimadamore@1108 230
mcimadamore@1108 231 void check() {
mcimadamore@1108 232
vromero@1482 233 EnumSet<WarningKind> expectedWarnings =
vromero@1482 234 EnumSet.noneOf(WarningKind.class);
mcimadamore@1108 235
mcimadamore@1108 236 if (sourceLevel == SourceLevel.JDK_7 &&
mcimadamore@1108 237 trustMe == TrustMe.TRUST &&
mcimadamore@1108 238 suppressLevel != SuppressLevel.VARARGS &&
mcimadamore@1108 239 xlint != XlintOption.NONE &&
vromero@1482 240 sig.isVarargs &&
vromero@1482 241 !sig.isReifiableArg &&
vromero@1482 242 body.hasAliasing &&
vromero@1482 243 (methKind == MethodKind.CONSTRUCTOR ||
vromero@1482 244 (methKind == MethodKind.METHOD &&
vromero@1482 245 modKind != ModifierKind.NONE))) {
mcimadamore@1108 246 expectedWarnings.add(WarningKind.UNSAFE_BODY);
mcimadamore@1108 247 }
mcimadamore@1108 248
mcimadamore@1108 249 if (sourceLevel == SourceLevel.JDK_7 &&
mcimadamore@1108 250 trustMe == TrustMe.DONT_TRUST &&
mcimadamore@1108 251 sig.isVarargs &&
mcimadamore@1108 252 !sig.isReifiableArg &&
mcimadamore@1108 253 xlint == XlintOption.ALL) {
mcimadamore@1108 254 expectedWarnings.add(WarningKind.UNSAFE_DECL);
mcimadamore@1108 255 }
mcimadamore@1108 256
mcimadamore@1108 257 if (sourceLevel == SourceLevel.JDK_7 &&
mcimadamore@1108 258 trustMe == TrustMe.TRUST &&
mcimadamore@1108 259 (!sig.isVarargs ||
vromero@1482 260 (modKind == ModifierKind.NONE &&
vromero@1482 261 methKind == MethodKind.METHOD))) {
mcimadamore@1108 262 expectedWarnings.add(WarningKind.MALFORMED_SAFEVARARGS);
mcimadamore@1108 263 }
mcimadamore@1108 264
mcimadamore@1108 265 if (sourceLevel == SourceLevel.JDK_7 &&
mcimadamore@1108 266 trustMe == TrustMe.TRUST &&
mcimadamore@1108 267 xlint != XlintOption.NONE &&
mcimadamore@1108 268 suppressLevel != SuppressLevel.VARARGS &&
vromero@1482 269 (modKind != ModifierKind.NONE ||
vromero@1482 270 methKind == MethodKind.CONSTRUCTOR) &&
mcimadamore@1108 271 sig.isVarargs &&
mcimadamore@1108 272 sig.isReifiableArg) {
mcimadamore@1108 273 expectedWarnings.add(WarningKind.REDUNDANT_SAFEVARARGS);
mcimadamore@1108 274 }
mcimadamore@1108 275
mcimadamore@1108 276 if (!expectedWarnings.containsAll(dc.warnings) ||
mcimadamore@1108 277 !dc.warnings.containsAll(expectedWarnings)) {
mcimadamore@1108 278 throw new Error("invalid diagnostics for source:\n" +
mcimadamore@1108 279 source.getCharContent(true) +
mcimadamore@1108 280 "\nOptions: " + xlint.getXlintOption() +
mcimadamore@1108 281 "\nExpected warnings: " + expectedWarnings +
mcimadamore@1108 282 "\nFound warnings: " + dc.warnings);
mcimadamore@1108 283 }
mcimadamore@1108 284 }
mcimadamore@1108 285
mcimadamore@1108 286 class JavaSource extends SimpleJavaFileObject {
mcimadamore@795 287
mcimadamore@795 288 String template = "import com.sun.tools.javac.api.*;\n" +
mcimadamore@795 289 "import java.util.List;\n" +
mcimadamore@795 290 "class Test {\n" +
mcimadamore@795 291 " static void test(Object o) {}\n" +
mcimadamore@795 292 " static void testArr(Object[] o) {}\n" +
mcimadamore@795 293 " #T \n #S #M { #B }\n" +
mcimadamore@795 294 "}\n";
mcimadamore@795 295
mcimadamore@795 296 String source;
mcimadamore@795 297
mcimadamore@1108 298 public JavaSource() {
mcimadamore@795 299 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
mcimadamore@795 300 source = template.replace("#T", trustMe.anno).
mcimadamore@795 301 replace("#S", suppressLevel.getSuppressAnno()).
mcimadamore@1108 302 replace("#M", sig.getSignature(modKind, methKind)).
mcimadamore@795 303 replace("#B", body.body);
mcimadamore@795 304 }
mcimadamore@795 305
mcimadamore@795 306 @Override
mcimadamore@795 307 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@795 308 return source;
mcimadamore@795 309 }
mcimadamore@795 310 }
mcimadamore@795 311
vromero@1482 312 class DiagnosticChecker
vromero@1482 313 implements javax.tools.DiagnosticListener<JavaFileObject> {
mcimadamore@795 314
mcimadamore@1108 315 EnumSet<WarningKind> warnings = EnumSet.noneOf(WarningKind.class);
mcimadamore@795 316
mcimadamore@795 317 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@795 318 if (diagnostic.getKind() == Diagnostic.Kind.WARNING) {
vromero@1482 319 if (diagnostic.getCode().
vromero@1482 320 contains("unsafe.use.varargs.param")) {
mcimadamore@1108 321 setWarning(WarningKind.UNSAFE_BODY);
vromero@1482 322 } else if (diagnostic.getCode().
vromero@1482 323 contains("redundant.trustme")) {
mcimadamore@1108 324 setWarning(WarningKind.REDUNDANT_SAFEVARARGS);
mcimadamore@795 325 }
mcimadamore@795 326 } else if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING &&
vromero@1482 327 diagnostic.getCode().
vromero@1482 328 contains("varargs.non.reifiable.type")) {
mcimadamore@1108 329 setWarning(WarningKind.UNSAFE_DECL);
mcimadamore@795 330 } else if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
mcimadamore@795 331 diagnostic.getCode().contains("invalid.trustme")) {
mcimadamore@1108 332 setWarning(WarningKind.MALFORMED_SAFEVARARGS);
mcimadamore@795 333 }
mcimadamore@795 334 }
mcimadamore@1108 335
mcimadamore@1108 336 void setWarning(WarningKind wk) {
mcimadamore@1108 337 if (!warnings.add(wk)) {
vromero@1482 338 throw new AssertionError("Duplicate warning of kind " +
vromero@1482 339 wk + " in source:\n" + source);
mcimadamore@1108 340 }
mcimadamore@1108 341 }
mcimadamore@1108 342
mcimadamore@1108 343 boolean hasWarning(WarningKind wk) {
mcimadamore@1108 344 return warnings.contains(wk);
mcimadamore@1108 345 }
mcimadamore@795 346 }
vromero@1482 347
mcimadamore@795 348 }

mercurial