aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * @test aoqi@0: * @bug 6945418 6993978 8006694 aoqi@0: * @summary Project Coin: Simplified Varargs Method Invocation aoqi@0: * temporarily workaround combo tests are causing time out in several platforms aoqi@0: * @author mcimadamore aoqi@0: * @library ../../lib aoqi@0: * @build JavacTestingAbstractThreadedTest aoqi@0: * @run main/othervm Warn4 aoqi@0: */ aoqi@0: aoqi@0: // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) aoqi@0: // see JDK-8006746 aoqi@0: aoqi@0: import java.net.URI; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.Set; aoqi@0: import java.util.HashSet; aoqi@0: import javax.tools.Diagnostic; aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.ToolProvider; aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: aoqi@0: public class Warn4 aoqi@0: extends JavacTestingAbstractThreadedTest aoqi@0: implements Runnable { aoqi@0: aoqi@0: final static Warning[] error = null; aoqi@0: final static Warning[] none = new Warning[] {}; aoqi@0: final static Warning[] vararg = new Warning[] { Warning.VARARGS }; aoqi@0: final static Warning[] unchecked = new Warning[] { Warning.UNCHECKED }; aoqi@0: final static Warning[] both = aoqi@0: new Warning[] { Warning.VARARGS, Warning.UNCHECKED }; aoqi@0: aoqi@0: enum Warning { aoqi@0: UNCHECKED("generic.array.creation"), aoqi@0: VARARGS("varargs.non.reifiable.type"); aoqi@0: aoqi@0: String key; aoqi@0: aoqi@0: Warning(String key) { aoqi@0: this.key = key; aoqi@0: } aoqi@0: aoqi@0: boolean isSuppressed(TrustMe trustMe, SourceLevel source, aoqi@0: SuppressLevel suppressLevelClient, aoqi@0: SuppressLevel suppressLevelDecl, aoqi@0: ModifierKind modKind) { aoqi@0: switch(this) { aoqi@0: case VARARGS: aoqi@0: return source == SourceLevel.JDK_6 || aoqi@0: suppressLevelDecl == SuppressLevel.UNCHECKED || aoqi@0: trustMe == TrustMe.TRUST; aoqi@0: case UNCHECKED: aoqi@0: return suppressLevelClient == SuppressLevel.UNCHECKED || aoqi@0: (trustMe == TrustMe.TRUST && modKind != aoqi@0: ModifierKind.NONE && source == SourceLevel.JDK_7); aoqi@0: } aoqi@0: aoqi@0: SuppressLevel supLev = this == VARARGS ? aoqi@0: suppressLevelDecl : aoqi@0: suppressLevelClient; aoqi@0: return supLev == SuppressLevel.UNCHECKED || aoqi@0: (trustMe == TrustMe.TRUST && modKind != ModifierKind.NONE); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum SourceLevel { aoqi@0: JDK_6("6"), aoqi@0: JDK_7("7"); aoqi@0: aoqi@0: String sourceKey; aoqi@0: aoqi@0: SourceLevel(String sourceKey) { aoqi@0: this.sourceKey = sourceKey; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum TrustMe { aoqi@0: DONT_TRUST(""), aoqi@0: TRUST("@java.lang.SafeVarargs"); aoqi@0: aoqi@0: String anno; aoqi@0: aoqi@0: TrustMe(String anno) { aoqi@0: this.anno = anno; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum ModifierKind { aoqi@0: NONE(" "), aoqi@0: FINAL("final "), aoqi@0: STATIC("static "); aoqi@0: aoqi@0: String mod; aoqi@0: aoqi@0: ModifierKind(String mod) { aoqi@0: this.mod = mod; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum SuppressLevel { aoqi@0: NONE(""), aoqi@0: UNCHECKED("unchecked"); aoqi@0: aoqi@0: String lint; aoqi@0: aoqi@0: SuppressLevel(String lint) { aoqi@0: this.lint = lint; aoqi@0: } aoqi@0: aoqi@0: String getSuppressAnno() { aoqi@0: return "@SuppressWarnings(\"" + lint + "\")"; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum Signature { aoqi@0: UNBOUND("void #name(List#arity arg) { #body }", aoqi@0: new Warning[][] {none, none, none, none, error}), aoqi@0: INVARIANT_TVAR(" void #name(List#arity arg) { #body }", aoqi@0: new Warning[][] {both, both, error, both, error}), aoqi@0: TVAR(" void #name(Z#arity arg) { #body }", aoqi@0: new Warning[][] {both, both, both, both, vararg}), aoqi@0: INVARIANT("void #name(List#arity arg) { #body }", aoqi@0: new Warning[][] {error, error, error, both, error}), aoqi@0: UNPARAMETERIZED("void #name(String#arity arg) { #body }", aoqi@0: new Warning[][] {error, error, error, error, none}); aoqi@0: aoqi@0: String template; aoqi@0: Warning[][] warnings; aoqi@0: aoqi@0: Signature(String template, Warning[][] warnings) { aoqi@0: this.template = template; aoqi@0: this.warnings = warnings; aoqi@0: } aoqi@0: aoqi@0: boolean isApplicableTo(Signature other) { aoqi@0: return warnings[other.ordinal()] != null; aoqi@0: } aoqi@0: aoqi@0: boolean giveUnchecked(Signature other) { aoqi@0: return warnings[other.ordinal()] == unchecked || aoqi@0: warnings[other.ordinal()] == both; aoqi@0: } aoqi@0: aoqi@0: boolean giveVarargs(Signature other) { aoqi@0: return warnings[other.ordinal()] == vararg || aoqi@0: warnings[other.ordinal()] == both; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: for (SourceLevel sourceLevel : SourceLevel.values()) { aoqi@0: for (TrustMe trustMe : TrustMe.values()) { aoqi@0: for (SuppressLevel suppressLevelClient : SuppressLevel.values()) { aoqi@0: for (SuppressLevel suppressLevelDecl : SuppressLevel.values()) { aoqi@0: for (ModifierKind modKind : ModifierKind.values()) { aoqi@0: for (Signature vararg_meth : Signature.values()) { aoqi@0: for (Signature client_meth : Signature.values()) { aoqi@0: if (vararg_meth.isApplicableTo(client_meth)) { aoqi@0: pool.execute(new Warn4(sourceLevel, aoqi@0: trustMe, aoqi@0: suppressLevelClient, aoqi@0: suppressLevelDecl, aoqi@0: modKind, aoqi@0: vararg_meth, aoqi@0: client_meth)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: checkAfterExec(); aoqi@0: } aoqi@0: aoqi@0: SourceLevel sourceLevel; aoqi@0: TrustMe trustMe; aoqi@0: SuppressLevel suppressLevelClient; aoqi@0: SuppressLevel suppressLevelDecl; aoqi@0: ModifierKind modKind; aoqi@0: Signature vararg_meth; aoqi@0: Signature client_meth; aoqi@0: DiagnosticChecker diagChecker; aoqi@0: aoqi@0: public Warn4(SourceLevel sourceLevel, TrustMe trustMe, aoqi@0: SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl, aoqi@0: ModifierKind modKind, Signature vararg_meth, Signature client_meth) { aoqi@0: this.sourceLevel = sourceLevel; aoqi@0: this.trustMe = trustMe; aoqi@0: this.suppressLevelClient = suppressLevelClient; aoqi@0: this.suppressLevelDecl = suppressLevelDecl; aoqi@0: this.modKind = modKind; aoqi@0: this.vararg_meth = vararg_meth; aoqi@0: this.client_meth = client_meth; aoqi@0: this.diagChecker = new DiagnosticChecker(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void run() { aoqi@0: int id = checkCount.incrementAndGet(); aoqi@0: final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); aoqi@0: JavaSource source = new JavaSource(id); aoqi@0: JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), diagChecker, aoqi@0: Arrays.asList("-Xlint:unchecked", "-source", sourceLevel.sourceKey), aoqi@0: null, Arrays.asList(source)); aoqi@0: ct.call(); //to get mandatory notes aoqi@0: check(source, new boolean[] {vararg_meth.giveUnchecked(client_meth), aoqi@0: vararg_meth.giveVarargs(client_meth)}); aoqi@0: } aoqi@0: aoqi@0: void check(JavaSource source, boolean[] warnArr) { aoqi@0: boolean badOutput = false; aoqi@0: for (Warning wkind : Warning.values()) { aoqi@0: boolean isSuppressed = wkind.isSuppressed(trustMe, sourceLevel, aoqi@0: suppressLevelClient, suppressLevelDecl, modKind); aoqi@0: badOutput |= (warnArr[wkind.ordinal()] && !isSuppressed) != aoqi@0: diagChecker.warnings.contains(wkind); aoqi@0: } aoqi@0: if (badOutput) { aoqi@0: throw new Error("invalid diagnostics for source:\n" + aoqi@0: source.getCharContent(true) + aoqi@0: "\nExpected unchecked warning: " + warnArr[0] + aoqi@0: "\nExpected unsafe vararg warning: " + warnArr[1] + aoqi@0: "\nWarnings: " + diagChecker.warnings + aoqi@0: "\nSource level: " + sourceLevel); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class JavaSource extends SimpleJavaFileObject { aoqi@0: aoqi@0: String source; aoqi@0: aoqi@0: public JavaSource(int id) { aoqi@0: super(URI.create(String.format("myfo:/Test%d.java", id)), aoqi@0: JavaFileObject.Kind.SOURCE); aoqi@0: String meth1 = vararg_meth.template.replace("#arity", "..."); aoqi@0: meth1 = meth1.replace("#name", "m"); aoqi@0: meth1 = meth1.replace("#body", ""); aoqi@0: meth1 = trustMe.anno + "\n" + suppressLevelDecl.getSuppressAnno() + aoqi@0: modKind.mod + meth1; aoqi@0: String meth2 = client_meth.template.replace("#arity", ""); aoqi@0: meth2 = meth2.replace("#name", "test"); aoqi@0: meth2 = meth2.replace("#body", "m(arg);"); aoqi@0: meth2 = suppressLevelClient.getSuppressAnno() + meth2; aoqi@0: source = String.format("import java.util.List;\n" + aoqi@0: "class Test%s {\n %s \n %s \n } \n", id, meth1, meth2); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return source; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static class DiagnosticChecker aoqi@0: implements javax.tools.DiagnosticListener { aoqi@0: aoqi@0: Set warnings = new HashSet<>(); aoqi@0: aoqi@0: public void report(Diagnostic diagnostic) { aoqi@0: if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING || aoqi@0: diagnostic.getKind() == Diagnostic.Kind.WARNING) { aoqi@0: if (diagnostic.getCode().contains(Warning.VARARGS.key)) { aoqi@0: warnings.add(Warning.VARARGS); aoqi@0: } else if(diagnostic.getCode().contains(Warning.UNCHECKED.key)) { aoqi@0: warnings.add(Warning.UNCHECKED); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: }