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