Wed, 08 Oct 2014 14:16:40 -0700
Merge
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 |
vromero@1520 | 26 | * @bug 6993978 7097436 8006694 |
mcimadamore@795 | 27 | * @summary Project Coin: Annotation to reduce varargs warnings |
vromero@1520 | 28 | * temporarily workaround combo tests are causing time out in several platforms |
mcimadamore@795 | 29 | * @author mcimadamore |
vromero@1482 | 30 | * @library ../../lib |
vromero@1482 | 31 | * @build JavacTestingAbstractThreadedTest |
vromero@1520 | 32 | * @run main/othervm Warn5 |
mcimadamore@795 | 33 | */ |
vromero@1520 | 34 | |
vromero@1520 | 35 | // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) |
vromero@1520 | 36 | // see JDK-8006746 |
vromero@1520 | 37 | |
mcimadamore@795 | 38 | import java.net.URI; |
mcimadamore@795 | 39 | import java.util.Arrays; |
mcimadamore@1108 | 40 | import java.util.EnumSet; |
mcimadamore@795 | 41 | import javax.tools.Diagnostic; |
mcimadamore@795 | 42 | import javax.tools.JavaCompiler; |
mcimadamore@795 | 43 | import javax.tools.JavaFileObject; |
mcimadamore@795 | 44 | import javax.tools.SimpleJavaFileObject; |
mcimadamore@795 | 45 | import javax.tools.ToolProvider; |
vromero@1482 | 46 | import com.sun.source.util.JavacTask; |
mcimadamore@795 | 47 | |
vromero@1482 | 48 | public class Warn5 |
vromero@1482 | 49 | extends JavacTestingAbstractThreadedTest |
vromero@1482 | 50 | implements Runnable { |
mcimadamore@795 | 51 | |
mcimadamore@795 | 52 | enum XlintOption { |
mcimadamore@795 | 53 | NONE("none"), |
mcimadamore@795 | 54 | ALL("all"); |
mcimadamore@795 | 55 | |
mcimadamore@795 | 56 | String opt; |
mcimadamore@795 | 57 | |
mcimadamore@795 | 58 | XlintOption(String opt) { |
mcimadamore@795 | 59 | this.opt = opt; |
mcimadamore@795 | 60 | } |
mcimadamore@795 | 61 | |
mcimadamore@795 | 62 | String getXlintOption() { |
mcimadamore@795 | 63 | return "-Xlint:" + opt; |
mcimadamore@795 | 64 | } |
mcimadamore@795 | 65 | } |
mcimadamore@795 | 66 | |
mcimadamore@795 | 67 | enum TrustMe { |
mcimadamore@795 | 68 | DONT_TRUST(""), |
mcimadamore@795 | 69 | TRUST("@java.lang.SafeVarargs"); |
mcimadamore@795 | 70 | |
mcimadamore@795 | 71 | String anno; |
mcimadamore@795 | 72 | |
mcimadamore@795 | 73 | TrustMe(String anno) { |
mcimadamore@795 | 74 | this.anno = anno; |
mcimadamore@795 | 75 | } |
mcimadamore@795 | 76 | } |
mcimadamore@795 | 77 | |
mcimadamore@795 | 78 | enum SuppressLevel { |
mcimadamore@795 | 79 | NONE, |
mcimadamore@795 | 80 | VARARGS; |
mcimadamore@795 | 81 | |
mcimadamore@795 | 82 | String getSuppressAnno() { |
mcimadamore@795 | 83 | return this == VARARGS ? |
mcimadamore@795 | 84 | "@SuppressWarnings(\"varargs\")" : |
mcimadamore@795 | 85 | ""; |
mcimadamore@795 | 86 | } |
mcimadamore@795 | 87 | } |
mcimadamore@795 | 88 | |
mcimadamore@795 | 89 | enum ModifierKind { |
mcimadamore@795 | 90 | NONE(""), |
mcimadamore@795 | 91 | FINAL("final"), |
mcimadamore@795 | 92 | STATIC("static"); |
mcimadamore@795 | 93 | |
mcimadamore@795 | 94 | String mod; |
mcimadamore@795 | 95 | |
mcimadamore@795 | 96 | ModifierKind(String mod) { |
mcimadamore@795 | 97 | this.mod = mod; |
mcimadamore@795 | 98 | } |
mcimadamore@795 | 99 | } |
mcimadamore@795 | 100 | |
mcimadamore@795 | 101 | enum MethodKind { |
mcimadamore@795 | 102 | METHOD("void m"), |
mcimadamore@795 | 103 | CONSTRUCTOR("Test"); |
mcimadamore@795 | 104 | |
mcimadamore@795 | 105 | String name; |
mcimadamore@795 | 106 | |
mcimadamore@795 | 107 | MethodKind(String name) { |
mcimadamore@795 | 108 | this.name = name; |
mcimadamore@795 | 109 | } |
mcimadamore@795 | 110 | } |
mcimadamore@795 | 111 | |
mcimadamore@795 | 112 | enum SourceLevel { |
mcimadamore@795 | 113 | JDK_6("6"), |
mcimadamore@795 | 114 | JDK_7("7"); |
mcimadamore@795 | 115 | |
mcimadamore@795 | 116 | String sourceKey; |
mcimadamore@795 | 117 | |
mcimadamore@795 | 118 | SourceLevel(String sourceKey) { |
mcimadamore@795 | 119 | this.sourceKey = sourceKey; |
mcimadamore@795 | 120 | } |
mcimadamore@795 | 121 | } |
mcimadamore@795 | 122 | |
mcimadamore@795 | 123 | enum SignatureKind { |
mcimadamore@795 | 124 | VARARGS_X("#K <X>#N(X... x)", false, true), |
mcimadamore@795 | 125 | VARARGS_STRING("#K #N(String... x)", true, true), |
mcimadamore@795 | 126 | ARRAY_X("#K <X>#N(X[] x)", false, false), |
mcimadamore@795 | 127 | ARRAY_STRING("#K #N(String[] x)", true, false); |
mcimadamore@795 | 128 | |
mcimadamore@795 | 129 | String stub; |
mcimadamore@795 | 130 | boolean isReifiableArg; |
mcimadamore@795 | 131 | boolean isVarargs; |
mcimadamore@795 | 132 | |
mcimadamore@795 | 133 | SignatureKind(String stub, boolean isReifiableArg, boolean isVarargs) { |
mcimadamore@795 | 134 | this.stub = stub; |
mcimadamore@795 | 135 | this.isReifiableArg = isReifiableArg; |
mcimadamore@795 | 136 | this.isVarargs = isVarargs; |
mcimadamore@795 | 137 | } |
mcimadamore@795 | 138 | |
mcimadamore@795 | 139 | String getSignature(ModifierKind modKind, MethodKind methKind) { |
mcimadamore@795 | 140 | return methKind != MethodKind.CONSTRUCTOR ? |
mcimadamore@795 | 141 | stub.replace("#K", modKind.mod).replace("#N", methKind.name) : |
mcimadamore@795 | 142 | stub.replace("#K", "").replace("#N", methKind.name); |
mcimadamore@795 | 143 | } |
mcimadamore@795 | 144 | } |
mcimadamore@795 | 145 | |
mcimadamore@795 | 146 | enum BodyKind { |
mcimadamore@795 | 147 | ASSIGN("Object o = x;", true), |
mcimadamore@795 | 148 | CAST("Object o = (Object)x;", true), |
mcimadamore@795 | 149 | METH("test(x);", true), |
mcimadamore@795 | 150 | PRINT("System.out.println(x.toString());", false), |
mcimadamore@795 | 151 | ARRAY_ASSIGN("Object[] o = x;", true), |
mcimadamore@795 | 152 | ARRAY_CAST("Object[] o = (Object[])x;", true), |
mcimadamore@795 | 153 | ARRAY_METH("testArr(x);", true); |
mcimadamore@795 | 154 | |
mcimadamore@795 | 155 | String body; |
mcimadamore@795 | 156 | boolean hasAliasing; |
mcimadamore@795 | 157 | |
mcimadamore@795 | 158 | BodyKind(String body, boolean hasAliasing) { |
mcimadamore@795 | 159 | this.body = body; |
mcimadamore@795 | 160 | this.hasAliasing = hasAliasing; |
mcimadamore@795 | 161 | } |
mcimadamore@795 | 162 | } |
mcimadamore@795 | 163 | |
mcimadamore@1108 | 164 | enum WarningKind { |
mcimadamore@1108 | 165 | UNSAFE_BODY, |
mcimadamore@1108 | 166 | UNSAFE_DECL, |
mcimadamore@1108 | 167 | MALFORMED_SAFEVARARGS, |
mcimadamore@1108 | 168 | REDUNDANT_SAFEVARARGS; |
mcimadamore@1108 | 169 | } |
mcimadamore@1108 | 170 | |
mcimadamore@1108 | 171 | public static void main(String... args) throws Exception { |
mcimadamore@1108 | 172 | for (SourceLevel sourceLevel : SourceLevel.values()) { |
mcimadamore@1108 | 173 | for (XlintOption xlint : XlintOption.values()) { |
mcimadamore@1108 | 174 | for (TrustMe trustMe : TrustMe.values()) { |
mcimadamore@1108 | 175 | for (SuppressLevel suppressLevel : SuppressLevel.values()) { |
mcimadamore@1108 | 176 | for (ModifierKind modKind : ModifierKind.values()) { |
mcimadamore@1108 | 177 | for (MethodKind methKind : MethodKind.values()) { |
mcimadamore@1108 | 178 | for (SignatureKind sig : SignatureKind.values()) { |
mcimadamore@1108 | 179 | for (BodyKind body : BodyKind.values()) { |
vromero@1482 | 180 | pool.execute(new Warn5(sourceLevel, |
vromero@1482 | 181 | xlint, trustMe, suppressLevel, |
vromero@1482 | 182 | modKind, methKind, sig, body)); |
mcimadamore@1108 | 183 | } |
mcimadamore@1108 | 184 | } |
mcimadamore@1108 | 185 | } |
mcimadamore@1108 | 186 | } |
mcimadamore@1108 | 187 | } |
mcimadamore@1108 | 188 | } |
mcimadamore@1108 | 189 | } |
mcimadamore@1108 | 190 | } |
vromero@1482 | 191 | |
vromero@1482 | 192 | checkAfterExec(false); |
mcimadamore@1108 | 193 | } |
mcimadamore@1108 | 194 | |
mcimadamore@1108 | 195 | final SourceLevel sourceLevel; |
mcimadamore@1108 | 196 | final XlintOption xlint; |
mcimadamore@1108 | 197 | final TrustMe trustMe; |
mcimadamore@1108 | 198 | final SuppressLevel suppressLevel; |
mcimadamore@1108 | 199 | final ModifierKind modKind; |
mcimadamore@1108 | 200 | final MethodKind methKind; |
mcimadamore@1108 | 201 | final SignatureKind sig; |
mcimadamore@1108 | 202 | final BodyKind body; |
mcimadamore@1108 | 203 | final JavaSource source; |
mcimadamore@1108 | 204 | final DiagnosticChecker dc; |
mcimadamore@1108 | 205 | |
vromero@1482 | 206 | public Warn5(SourceLevel sourceLevel, XlintOption xlint, TrustMe trustMe, |
vromero@1482 | 207 | SuppressLevel suppressLevel, ModifierKind modKind, |
vromero@1482 | 208 | MethodKind methKind, SignatureKind sig, BodyKind body) { |
mcimadamore@1108 | 209 | this.sourceLevel = sourceLevel; |
mcimadamore@1108 | 210 | this.xlint = xlint; |
mcimadamore@1108 | 211 | this.trustMe = trustMe; |
mcimadamore@1108 | 212 | this.suppressLevel = suppressLevel; |
mcimadamore@1108 | 213 | this.modKind = modKind; |
mcimadamore@1108 | 214 | this.methKind = methKind; |
mcimadamore@1108 | 215 | this.sig = sig; |
mcimadamore@1108 | 216 | this.body = body; |
mcimadamore@1108 | 217 | this.source = new JavaSource(); |
mcimadamore@1108 | 218 | this.dc = new DiagnosticChecker(); |
mcimadamore@1108 | 219 | } |
mcimadamore@1108 | 220 | |
vromero@1482 | 221 | @Override |
vromero@1482 | 222 | public void run() { |
mcimadamore@1108 | 223 | final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
vromero@1482 | 224 | JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), dc, |
vromero@1482 | 225 | Arrays.asList(xlint.getXlintOption(), |
vromero@1482 | 226 | "-source", sourceLevel.sourceKey), |
vromero@1482 | 227 | null, Arrays.asList(source)); |
vromero@1482 | 228 | try { |
vromero@1482 | 229 | ct.analyze(); |
vromero@1482 | 230 | } catch (Throwable t) { |
vromero@1482 | 231 | processException(t); |
vromero@1482 | 232 | } |
mcimadamore@1108 | 233 | check(); |
mcimadamore@1108 | 234 | } |
mcimadamore@1108 | 235 | |
mcimadamore@1108 | 236 | void check() { |
mcimadamore@1108 | 237 | |
vromero@1482 | 238 | EnumSet<WarningKind> expectedWarnings = |
vromero@1482 | 239 | EnumSet.noneOf(WarningKind.class); |
mcimadamore@1108 | 240 | |
mcimadamore@1108 | 241 | if (sourceLevel == SourceLevel.JDK_7 && |
mcimadamore@1108 | 242 | trustMe == TrustMe.TRUST && |
mcimadamore@1108 | 243 | suppressLevel != SuppressLevel.VARARGS && |
mcimadamore@1108 | 244 | xlint != XlintOption.NONE && |
vromero@1482 | 245 | sig.isVarargs && |
vromero@1482 | 246 | !sig.isReifiableArg && |
vromero@1482 | 247 | body.hasAliasing && |
vromero@1482 | 248 | (methKind == MethodKind.CONSTRUCTOR || |
vromero@1482 | 249 | (methKind == MethodKind.METHOD && |
vromero@1482 | 250 | modKind != ModifierKind.NONE))) { |
mcimadamore@1108 | 251 | expectedWarnings.add(WarningKind.UNSAFE_BODY); |
mcimadamore@1108 | 252 | } |
mcimadamore@1108 | 253 | |
mcimadamore@1108 | 254 | if (sourceLevel == SourceLevel.JDK_7 && |
mcimadamore@1108 | 255 | trustMe == TrustMe.DONT_TRUST && |
mcimadamore@1108 | 256 | sig.isVarargs && |
mcimadamore@1108 | 257 | !sig.isReifiableArg && |
mcimadamore@1108 | 258 | xlint == XlintOption.ALL) { |
mcimadamore@1108 | 259 | expectedWarnings.add(WarningKind.UNSAFE_DECL); |
mcimadamore@1108 | 260 | } |
mcimadamore@1108 | 261 | |
mcimadamore@1108 | 262 | if (sourceLevel == SourceLevel.JDK_7 && |
mcimadamore@1108 | 263 | trustMe == TrustMe.TRUST && |
mcimadamore@1108 | 264 | (!sig.isVarargs || |
vromero@1482 | 265 | (modKind == ModifierKind.NONE && |
vromero@1482 | 266 | methKind == MethodKind.METHOD))) { |
mcimadamore@1108 | 267 | expectedWarnings.add(WarningKind.MALFORMED_SAFEVARARGS); |
mcimadamore@1108 | 268 | } |
mcimadamore@1108 | 269 | |
mcimadamore@1108 | 270 | if (sourceLevel == SourceLevel.JDK_7 && |
mcimadamore@1108 | 271 | trustMe == TrustMe.TRUST && |
mcimadamore@1108 | 272 | xlint != XlintOption.NONE && |
mcimadamore@1108 | 273 | suppressLevel != SuppressLevel.VARARGS && |
vromero@1482 | 274 | (modKind != ModifierKind.NONE || |
vromero@1482 | 275 | methKind == MethodKind.CONSTRUCTOR) && |
mcimadamore@1108 | 276 | sig.isVarargs && |
mcimadamore@1108 | 277 | sig.isReifiableArg) { |
mcimadamore@1108 | 278 | expectedWarnings.add(WarningKind.REDUNDANT_SAFEVARARGS); |
mcimadamore@1108 | 279 | } |
mcimadamore@1108 | 280 | |
mcimadamore@1108 | 281 | if (!expectedWarnings.containsAll(dc.warnings) || |
mcimadamore@1108 | 282 | !dc.warnings.containsAll(expectedWarnings)) { |
mcimadamore@1108 | 283 | throw new Error("invalid diagnostics for source:\n" + |
mcimadamore@1108 | 284 | source.getCharContent(true) + |
mcimadamore@1108 | 285 | "\nOptions: " + xlint.getXlintOption() + |
mcimadamore@1108 | 286 | "\nExpected warnings: " + expectedWarnings + |
mcimadamore@1108 | 287 | "\nFound warnings: " + dc.warnings); |
mcimadamore@1108 | 288 | } |
mcimadamore@1108 | 289 | } |
mcimadamore@1108 | 290 | |
mcimadamore@1108 | 291 | class JavaSource extends SimpleJavaFileObject { |
mcimadamore@795 | 292 | |
mcimadamore@795 | 293 | String template = "import com.sun.tools.javac.api.*;\n" + |
mcimadamore@795 | 294 | "import java.util.List;\n" + |
mcimadamore@795 | 295 | "class Test {\n" + |
mcimadamore@795 | 296 | " static void test(Object o) {}\n" + |
mcimadamore@795 | 297 | " static void testArr(Object[] o) {}\n" + |
mcimadamore@795 | 298 | " #T \n #S #M { #B }\n" + |
mcimadamore@795 | 299 | "}\n"; |
mcimadamore@795 | 300 | |
mcimadamore@795 | 301 | String source; |
mcimadamore@795 | 302 | |
mcimadamore@1108 | 303 | public JavaSource() { |
mcimadamore@795 | 304 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@795 | 305 | source = template.replace("#T", trustMe.anno). |
mcimadamore@795 | 306 | replace("#S", suppressLevel.getSuppressAnno()). |
mcimadamore@1108 | 307 | replace("#M", sig.getSignature(modKind, methKind)). |
mcimadamore@795 | 308 | replace("#B", body.body); |
mcimadamore@795 | 309 | } |
mcimadamore@795 | 310 | |
mcimadamore@795 | 311 | @Override |
mcimadamore@795 | 312 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@795 | 313 | return source; |
mcimadamore@795 | 314 | } |
mcimadamore@795 | 315 | } |
mcimadamore@795 | 316 | |
vromero@1482 | 317 | class DiagnosticChecker |
vromero@1482 | 318 | implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@795 | 319 | |
mcimadamore@1108 | 320 | EnumSet<WarningKind> warnings = EnumSet.noneOf(WarningKind.class); |
mcimadamore@795 | 321 | |
mcimadamore@795 | 322 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@795 | 323 | if (diagnostic.getKind() == Diagnostic.Kind.WARNING) { |
vromero@1482 | 324 | if (diagnostic.getCode(). |
vromero@1482 | 325 | contains("unsafe.use.varargs.param")) { |
mcimadamore@1108 | 326 | setWarning(WarningKind.UNSAFE_BODY); |
vromero@1482 | 327 | } else if (diagnostic.getCode(). |
vromero@1482 | 328 | contains("redundant.trustme")) { |
mcimadamore@1108 | 329 | setWarning(WarningKind.REDUNDANT_SAFEVARARGS); |
mcimadamore@795 | 330 | } |
mcimadamore@795 | 331 | } else if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING && |
vromero@1482 | 332 | diagnostic.getCode(). |
vromero@1482 | 333 | contains("varargs.non.reifiable.type")) { |
mcimadamore@1108 | 334 | setWarning(WarningKind.UNSAFE_DECL); |
mcimadamore@795 | 335 | } else if (diagnostic.getKind() == Diagnostic.Kind.ERROR && |
mcimadamore@795 | 336 | diagnostic.getCode().contains("invalid.trustme")) { |
mcimadamore@1108 | 337 | setWarning(WarningKind.MALFORMED_SAFEVARARGS); |
mcimadamore@795 | 338 | } |
mcimadamore@795 | 339 | } |
mcimadamore@1108 | 340 | |
mcimadamore@1108 | 341 | void setWarning(WarningKind wk) { |
mcimadamore@1108 | 342 | if (!warnings.add(wk)) { |
vromero@1482 | 343 | throw new AssertionError("Duplicate warning of kind " + |
vromero@1482 | 344 | wk + " in source:\n" + source); |
mcimadamore@1108 | 345 | } |
mcimadamore@1108 | 346 | } |
mcimadamore@1108 | 347 | |
mcimadamore@1108 | 348 | boolean hasWarning(WarningKind wk) { |
mcimadamore@1108 | 349 | return warnings.contains(wk); |
mcimadamore@1108 | 350 | } |
mcimadamore@795 | 351 | } |
vromero@1482 | 352 | |
mcimadamore@795 | 353 | } |