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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /**
aoqi@0 25 * @test
aoqi@0 26 * @bug 6945418 6993978 8006694
aoqi@0 27 * @summary Project Coin: Simplified Varargs Method Invocation
aoqi@0 28 * temporarily workaround combo tests are causing time out in several platforms
aoqi@0 29 * @author mcimadamore
aoqi@0 30 * @library ../../lib
aoqi@0 31 * @build JavacTestingAbstractThreadedTest
aoqi@0 32 * @run main/othervm Warn4
aoqi@0 33 */
aoqi@0 34
aoqi@0 35 // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
aoqi@0 36 // see JDK-8006746
aoqi@0 37
aoqi@0 38 import java.net.URI;
aoqi@0 39 import java.util.Arrays;
aoqi@0 40 import java.util.Set;
aoqi@0 41 import java.util.HashSet;
aoqi@0 42 import javax.tools.Diagnostic;
aoqi@0 43 import javax.tools.JavaCompiler;
aoqi@0 44 import javax.tools.JavaFileObject;
aoqi@0 45 import javax.tools.SimpleJavaFileObject;
aoqi@0 46 import javax.tools.ToolProvider;
aoqi@0 47 import com.sun.source.util.JavacTask;
aoqi@0 48
aoqi@0 49 public class Warn4
aoqi@0 50 extends JavacTestingAbstractThreadedTest
aoqi@0 51 implements Runnable {
aoqi@0 52
aoqi@0 53 final static Warning[] error = null;
aoqi@0 54 final static Warning[] none = new Warning[] {};
aoqi@0 55 final static Warning[] vararg = new Warning[] { Warning.VARARGS };
aoqi@0 56 final static Warning[] unchecked = new Warning[] { Warning.UNCHECKED };
aoqi@0 57 final static Warning[] both =
aoqi@0 58 new Warning[] { Warning.VARARGS, Warning.UNCHECKED };
aoqi@0 59
aoqi@0 60 enum Warning {
aoqi@0 61 UNCHECKED("generic.array.creation"),
aoqi@0 62 VARARGS("varargs.non.reifiable.type");
aoqi@0 63
aoqi@0 64 String key;
aoqi@0 65
aoqi@0 66 Warning(String key) {
aoqi@0 67 this.key = key;
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 boolean isSuppressed(TrustMe trustMe, SourceLevel source,
aoqi@0 71 SuppressLevel suppressLevelClient,
aoqi@0 72 SuppressLevel suppressLevelDecl,
aoqi@0 73 ModifierKind modKind) {
aoqi@0 74 switch(this) {
aoqi@0 75 case VARARGS:
aoqi@0 76 return source == SourceLevel.JDK_6 ||
aoqi@0 77 suppressLevelDecl == SuppressLevel.UNCHECKED ||
aoqi@0 78 trustMe == TrustMe.TRUST;
aoqi@0 79 case UNCHECKED:
aoqi@0 80 return suppressLevelClient == SuppressLevel.UNCHECKED ||
aoqi@0 81 (trustMe == TrustMe.TRUST && modKind !=
aoqi@0 82 ModifierKind.NONE && source == SourceLevel.JDK_7);
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 SuppressLevel supLev = this == VARARGS ?
aoqi@0 86 suppressLevelDecl :
aoqi@0 87 suppressLevelClient;
aoqi@0 88 return supLev == SuppressLevel.UNCHECKED ||
aoqi@0 89 (trustMe == TrustMe.TRUST && modKind != ModifierKind.NONE);
aoqi@0 90 }
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 enum SourceLevel {
aoqi@0 94 JDK_6("6"),
aoqi@0 95 JDK_7("7");
aoqi@0 96
aoqi@0 97 String sourceKey;
aoqi@0 98
aoqi@0 99 SourceLevel(String sourceKey) {
aoqi@0 100 this.sourceKey = sourceKey;
aoqi@0 101 }
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 enum TrustMe {
aoqi@0 105 DONT_TRUST(""),
aoqi@0 106 TRUST("@java.lang.SafeVarargs");
aoqi@0 107
aoqi@0 108 String anno;
aoqi@0 109
aoqi@0 110 TrustMe(String anno) {
aoqi@0 111 this.anno = anno;
aoqi@0 112 }
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 enum ModifierKind {
aoqi@0 116 NONE(" "),
aoqi@0 117 FINAL("final "),
aoqi@0 118 STATIC("static ");
aoqi@0 119
aoqi@0 120 String mod;
aoqi@0 121
aoqi@0 122 ModifierKind(String mod) {
aoqi@0 123 this.mod = mod;
aoqi@0 124 }
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 enum SuppressLevel {
aoqi@0 128 NONE(""),
aoqi@0 129 UNCHECKED("unchecked");
aoqi@0 130
aoqi@0 131 String lint;
aoqi@0 132
aoqi@0 133 SuppressLevel(String lint) {
aoqi@0 134 this.lint = lint;
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 String getSuppressAnno() {
aoqi@0 138 return "@SuppressWarnings(\"" + lint + "\")";
aoqi@0 139 }
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 enum Signature {
aoqi@0 143 UNBOUND("void #name(List<?>#arity arg) { #body }",
aoqi@0 144 new Warning[][] {none, none, none, none, error}),
aoqi@0 145 INVARIANT_TVAR("<Z> void #name(List<Z>#arity arg) { #body }",
aoqi@0 146 new Warning[][] {both, both, error, both, error}),
aoqi@0 147 TVAR("<Z> void #name(Z#arity arg) { #body }",
aoqi@0 148 new Warning[][] {both, both, both, both, vararg}),
aoqi@0 149 INVARIANT("void #name(List<String>#arity arg) { #body }",
aoqi@0 150 new Warning[][] {error, error, error, both, error}),
aoqi@0 151 UNPARAMETERIZED("void #name(String#arity arg) { #body }",
aoqi@0 152 new Warning[][] {error, error, error, error, none});
aoqi@0 153
aoqi@0 154 String template;
aoqi@0 155 Warning[][] warnings;
aoqi@0 156
aoqi@0 157 Signature(String template, Warning[][] warnings) {
aoqi@0 158 this.template = template;
aoqi@0 159 this.warnings = warnings;
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 boolean isApplicableTo(Signature other) {
aoqi@0 163 return warnings[other.ordinal()] != null;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 boolean giveUnchecked(Signature other) {
aoqi@0 167 return warnings[other.ordinal()] == unchecked ||
aoqi@0 168 warnings[other.ordinal()] == both;
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 boolean giveVarargs(Signature other) {
aoqi@0 172 return warnings[other.ordinal()] == vararg ||
aoqi@0 173 warnings[other.ordinal()] == both;
aoqi@0 174 }
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 public static void main(String... args) throws Exception {
aoqi@0 178 for (SourceLevel sourceLevel : SourceLevel.values()) {
aoqi@0 179 for (TrustMe trustMe : TrustMe.values()) {
aoqi@0 180 for (SuppressLevel suppressLevelClient : SuppressLevel.values()) {
aoqi@0 181 for (SuppressLevel suppressLevelDecl : SuppressLevel.values()) {
aoqi@0 182 for (ModifierKind modKind : ModifierKind.values()) {
aoqi@0 183 for (Signature vararg_meth : Signature.values()) {
aoqi@0 184 for (Signature client_meth : Signature.values()) {
aoqi@0 185 if (vararg_meth.isApplicableTo(client_meth)) {
aoqi@0 186 pool.execute(new Warn4(sourceLevel,
aoqi@0 187 trustMe,
aoqi@0 188 suppressLevelClient,
aoqi@0 189 suppressLevelDecl,
aoqi@0 190 modKind,
aoqi@0 191 vararg_meth,
aoqi@0 192 client_meth));
aoqi@0 193 }
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196 }
aoqi@0 197 }
aoqi@0 198 }
aoqi@0 199 }
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 checkAfterExec();
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 SourceLevel sourceLevel;
aoqi@0 206 TrustMe trustMe;
aoqi@0 207 SuppressLevel suppressLevelClient;
aoqi@0 208 SuppressLevel suppressLevelDecl;
aoqi@0 209 ModifierKind modKind;
aoqi@0 210 Signature vararg_meth;
aoqi@0 211 Signature client_meth;
aoqi@0 212 DiagnosticChecker diagChecker;
aoqi@0 213
aoqi@0 214 public Warn4(SourceLevel sourceLevel, TrustMe trustMe,
aoqi@0 215 SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl,
aoqi@0 216 ModifierKind modKind, Signature vararg_meth, Signature client_meth) {
aoqi@0 217 this.sourceLevel = sourceLevel;
aoqi@0 218 this.trustMe = trustMe;
aoqi@0 219 this.suppressLevelClient = suppressLevelClient;
aoqi@0 220 this.suppressLevelDecl = suppressLevelDecl;
aoqi@0 221 this.modKind = modKind;
aoqi@0 222 this.vararg_meth = vararg_meth;
aoqi@0 223 this.client_meth = client_meth;
aoqi@0 224 this.diagChecker = new DiagnosticChecker();
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 @Override
aoqi@0 228 public void run() {
aoqi@0 229 int id = checkCount.incrementAndGet();
aoqi@0 230 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
aoqi@0 231 JavaSource source = new JavaSource(id);
aoqi@0 232 JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), diagChecker,
aoqi@0 233 Arrays.asList("-Xlint:unchecked", "-source", sourceLevel.sourceKey),
aoqi@0 234 null, Arrays.asList(source));
aoqi@0 235 ct.call(); //to get mandatory notes
aoqi@0 236 check(source, new boolean[] {vararg_meth.giveUnchecked(client_meth),
aoqi@0 237 vararg_meth.giveVarargs(client_meth)});
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 void check(JavaSource source, boolean[] warnArr) {
aoqi@0 241 boolean badOutput = false;
aoqi@0 242 for (Warning wkind : Warning.values()) {
aoqi@0 243 boolean isSuppressed = wkind.isSuppressed(trustMe, sourceLevel,
aoqi@0 244 suppressLevelClient, suppressLevelDecl, modKind);
aoqi@0 245 badOutput |= (warnArr[wkind.ordinal()] && !isSuppressed) !=
aoqi@0 246 diagChecker.warnings.contains(wkind);
aoqi@0 247 }
aoqi@0 248 if (badOutput) {
aoqi@0 249 throw new Error("invalid diagnostics for source:\n" +
aoqi@0 250 source.getCharContent(true) +
aoqi@0 251 "\nExpected unchecked warning: " + warnArr[0] +
aoqi@0 252 "\nExpected unsafe vararg warning: " + warnArr[1] +
aoqi@0 253 "\nWarnings: " + diagChecker.warnings +
aoqi@0 254 "\nSource level: " + sourceLevel);
aoqi@0 255 }
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 class JavaSource extends SimpleJavaFileObject {
aoqi@0 259
aoqi@0 260 String source;
aoqi@0 261
aoqi@0 262 public JavaSource(int id) {
aoqi@0 263 super(URI.create(String.format("myfo:/Test%d.java", id)),
aoqi@0 264 JavaFileObject.Kind.SOURCE);
aoqi@0 265 String meth1 = vararg_meth.template.replace("#arity", "...");
aoqi@0 266 meth1 = meth1.replace("#name", "m");
aoqi@0 267 meth1 = meth1.replace("#body", "");
aoqi@0 268 meth1 = trustMe.anno + "\n" + suppressLevelDecl.getSuppressAnno() +
aoqi@0 269 modKind.mod + meth1;
aoqi@0 270 String meth2 = client_meth.template.replace("#arity", "");
aoqi@0 271 meth2 = meth2.replace("#name", "test");
aoqi@0 272 meth2 = meth2.replace("#body", "m(arg);");
aoqi@0 273 meth2 = suppressLevelClient.getSuppressAnno() + meth2;
aoqi@0 274 source = String.format("import java.util.List;\n" +
aoqi@0 275 "class Test%s {\n %s \n %s \n } \n", id, meth1, meth2);
aoqi@0 276 }
aoqi@0 277
aoqi@0 278 @Override
aoqi@0 279 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 280 return source;
aoqi@0 281 }
aoqi@0 282 }
aoqi@0 283
aoqi@0 284 static class DiagnosticChecker
aoqi@0 285 implements javax.tools.DiagnosticListener<JavaFileObject> {
aoqi@0 286
aoqi@0 287 Set<Warning> warnings = new HashSet<>();
aoqi@0 288
aoqi@0 289 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
aoqi@0 290 if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING ||
aoqi@0 291 diagnostic.getKind() == Diagnostic.Kind.WARNING) {
aoqi@0 292 if (diagnostic.getCode().contains(Warning.VARARGS.key)) {
aoqi@0 293 warnings.add(Warning.VARARGS);
aoqi@0 294 } else if(diagnostic.getCode().contains(Warning.UNCHECKED.key)) {
aoqi@0 295 warnings.add(Warning.UNCHECKED);
aoqi@0 296 }
aoqi@0 297 }
aoqi@0 298 }
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 }

mercurial