mcimadamore@580: /* jjg@581: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. mcimadamore@580: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@580: * mcimadamore@580: * This code is free software; you can redistribute it and/or modify it mcimadamore@580: * under the terms of the GNU General Public License version 2 only, as mcimadamore@580: * published by the Free Software Foundation. mcimadamore@580: * mcimadamore@580: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@580: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@580: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@580: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@580: * accompanied this code). mcimadamore@580: * mcimadamore@580: * You should have received a copy of the GNU General Public License version mcimadamore@580: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@580: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@580: * jjg@581: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@581: * or visit www.oracle.com if you need additional information or have any jjg@581: * questions. mcimadamore@580: */ mcimadamore@580: mcimadamore@580: /** mcimadamore@580: * @test mcimadamore@580: * @bug 6945418 mcimadamore@580: * @summary Project Coin: Simplified Varargs Method Invocation mcimadamore@580: * @author mcimadamore mcimadamore@580: * @run main Warn4 mcimadamore@580: */ mcimadamore@580: import com.sun.source.util.JavacTask; mcimadamore@580: import java.net.URI; mcimadamore@580: import java.util.Arrays; mcimadamore@580: import java.util.Set; mcimadamore@580: import java.util.HashSet; mcimadamore@580: import javax.tools.Diagnostic; mcimadamore@580: import javax.tools.JavaCompiler; mcimadamore@580: import javax.tools.JavaFileObject; mcimadamore@580: import javax.tools.SimpleJavaFileObject; mcimadamore@580: import javax.tools.ToolProvider; mcimadamore@580: mcimadamore@580: public class Warn4 { mcimadamore@580: mcimadamore@580: final static Warning[] error = null; mcimadamore@580: final static Warning[] none = new Warning[] {}; mcimadamore@580: final static Warning[] vararg = new Warning[] { Warning.VARARGS }; mcimadamore@580: final static Warning[] unchecked = new Warning[] { Warning.UNCHECKED }; mcimadamore@580: final static Warning[] both = new Warning[] { Warning.VARARGS, Warning.UNCHECKED }; mcimadamore@580: mcimadamore@580: enum Warning { mcimadamore@580: UNCHECKED("unchecked"), mcimadamore@580: VARARGS("varargs"); mcimadamore@580: mcimadamore@580: String category; mcimadamore@580: mcimadamore@580: Warning(String category) { mcimadamore@580: this.category = category; mcimadamore@580: } mcimadamore@580: mcimadamore@580: boolean isEnabled(XlintOption xlint, SuppressLevel suppressLevel) { mcimadamore@580: return Arrays.asList(xlint.enabledWarnings).contains(this); mcimadamore@580: } mcimadamore@580: mcimadamore@580: boolean isSuppressed(SuppressLevel suppressLevel) { mcimadamore@580: return Arrays.asList(suppressLevel.suppressedWarnings).contains(VARARGS); mcimadamore@580: } mcimadamore@580: } mcimadamore@580: mcimadamore@580: enum XlintOption { mcimadamore@580: NONE(), mcimadamore@580: UNCHECKED(Warning.UNCHECKED), mcimadamore@580: VARARGS(Warning.VARARGS), mcimadamore@580: ALL(Warning.UNCHECKED, Warning.VARARGS); mcimadamore@580: mcimadamore@580: Warning[] enabledWarnings; mcimadamore@580: mcimadamore@580: XlintOption(Warning... enabledWarnings) { mcimadamore@580: this.enabledWarnings = enabledWarnings; mcimadamore@580: } mcimadamore@580: mcimadamore@580: String getXlintOption() { mcimadamore@580: StringBuilder buf = new StringBuilder(); mcimadamore@580: String sep = ""; mcimadamore@580: for (Warning w : enabledWarnings) { mcimadamore@580: buf.append(sep); mcimadamore@580: buf.append(w.category); mcimadamore@580: sep=","; mcimadamore@580: } mcimadamore@580: return "-Xlint:" + mcimadamore@580: (this == NONE ? "none" : buf.toString()); mcimadamore@580: } mcimadamore@580: } mcimadamore@580: mcimadamore@580: enum SuppressLevel { mcimadamore@580: NONE(), mcimadamore@580: UNCHECKED(Warning.UNCHECKED), mcimadamore@580: VARARGS(Warning.VARARGS), mcimadamore@580: ALL(Warning.UNCHECKED, Warning.VARARGS); mcimadamore@580: mcimadamore@580: Warning[] suppressedWarnings; mcimadamore@580: mcimadamore@580: SuppressLevel(Warning... suppressedWarnings) { mcimadamore@580: this.suppressedWarnings = suppressedWarnings; mcimadamore@580: } mcimadamore@580: mcimadamore@580: String getSuppressAnnotation() { mcimadamore@580: StringBuilder buf = new StringBuilder(); mcimadamore@580: String sep = ""; mcimadamore@580: for (Warning w : suppressedWarnings) { mcimadamore@580: buf.append(sep); mcimadamore@580: buf.append("\""); mcimadamore@580: buf.append(w.category); mcimadamore@580: buf.append("\""); mcimadamore@580: sep=","; mcimadamore@580: } mcimadamore@580: return this == NONE ? "" : mcimadamore@580: "@SuppressWarnings({" + buf.toString() + "})"; mcimadamore@580: } mcimadamore@580: } mcimadamore@580: mcimadamore@580: enum Signature { mcimadamore@580: mcimadamore@580: EXTENDS_TVAR(" void #name(List#arity arg) { #body }", mcimadamore@580: new Warning[][] {both, both, both, both, error, both, both, both, error}), mcimadamore@580: SUPER_TVAR(" void #name(List#arity arg) { #body }", mcimadamore@580: new Warning[][] {error, both, error, both, error, error, both, both, error}), mcimadamore@580: UNBOUND("void #name(List#arity arg) { #body }", mcimadamore@580: new Warning[][] {none, none, none, none, none, none, none, none, error}), mcimadamore@580: INVARIANT_TVAR(" void #name(List#arity arg) { #body }", mcimadamore@580: new Warning[][] {both, both, both, both, error, both, both, both, error}), mcimadamore@580: TVAR(" void #name(Z#arity arg) { #body }", mcimadamore@580: new Warning[][] {both, both, both, both, both, both, both, both, vararg}), mcimadamore@580: EXTENDS("void #name(List#arity arg) { #body }", mcimadamore@580: new Warning[][] {error, error, error, error, error, both, error, both, error}), mcimadamore@580: SUPER("void #name(List#arity arg) { #body }", mcimadamore@580: new Warning[][] {error, error, error, error, error, error, both, both, error}), mcimadamore@580: INVARIANT("void #name(List#arity arg) { #body }", mcimadamore@580: new Warning[][] {error, error, error, error, error, error, error, both, error}), mcimadamore@580: UNPARAMETERIZED("void #name(String#arity arg) { #body }", mcimadamore@580: new Warning[][] {error, error, error, error, error, error, error, error, none}); mcimadamore@580: mcimadamore@580: String template; mcimadamore@580: Warning[][] warnings; mcimadamore@580: mcimadamore@580: Signature(String template, Warning[][] warnings) { mcimadamore@580: this.template = template; mcimadamore@580: this.warnings = warnings; mcimadamore@580: } mcimadamore@580: mcimadamore@580: boolean isApplicableTo(Signature other) { mcimadamore@580: return warnings[other.ordinal()] != null; mcimadamore@580: } mcimadamore@580: mcimadamore@580: boolean giveUnchecked(Signature other) { mcimadamore@580: return warnings[other.ordinal()] == unchecked || mcimadamore@580: warnings[other.ordinal()] == both; mcimadamore@580: } mcimadamore@580: mcimadamore@580: boolean giveVarargs(Signature other) { mcimadamore@580: return warnings[other.ordinal()] == vararg || mcimadamore@580: warnings[other.ordinal()] == both; mcimadamore@580: } mcimadamore@580: } mcimadamore@580: mcimadamore@580: public static void main(String... args) throws Exception { mcimadamore@580: for (XlintOption xlint : XlintOption.values()) { mcimadamore@580: for (SuppressLevel suppressLevel : SuppressLevel.values()) { mcimadamore@580: for (Signature vararg_meth : Signature.values()) { mcimadamore@580: for (Signature client_meth : Signature.values()) { mcimadamore@580: if (vararg_meth.isApplicableTo(client_meth)) { mcimadamore@580: test(xlint, mcimadamore@580: suppressLevel, mcimadamore@580: vararg_meth, mcimadamore@580: client_meth); mcimadamore@580: } mcimadamore@580: } mcimadamore@580: } mcimadamore@580: } mcimadamore@580: } mcimadamore@580: } mcimadamore@580: mcimadamore@580: static void test(XlintOption xlint, SuppressLevel suppressLevel, mcimadamore@580: Signature vararg_meth, Signature client_meth) throws Exception { mcimadamore@580: final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); mcimadamore@580: JavaSource source = new JavaSource(suppressLevel, vararg_meth, client_meth); mcimadamore@580: DiagnosticChecker dc = new DiagnosticChecker(); mcimadamore@580: JavacTask ct = (JavacTask)tool.getTask(null, null, dc, mcimadamore@580: Arrays.asList(xlint.getXlintOption()), null, Arrays.asList(source)); mcimadamore@580: ct.generate(); //to get mandatory notes mcimadamore@580: check(dc.warnings, mcimadamore@580: dc.notes, mcimadamore@580: new boolean[] {vararg_meth.giveUnchecked(client_meth), mcimadamore@580: vararg_meth.giveVarargs(client_meth)}, mcimadamore@580: source, xlint, suppressLevel); mcimadamore@580: } mcimadamore@580: mcimadamore@580: static void check(Set warnings, Set notes, boolean[] warnArr, JavaSource source, XlintOption xlint, SuppressLevel suppressLevel) { mcimadamore@580: boolean badOutput = false; mcimadamore@580: for (Warning wkind : Warning.values()) { mcimadamore@580: badOutput |= (warnArr[wkind.ordinal()] && !wkind.isSuppressed(suppressLevel)) != mcimadamore@580: (wkind.isEnabled(xlint, suppressLevel) ? mcimadamore@580: warnings.contains(wkind) : mcimadamore@580: notes.contains(wkind)); mcimadamore@580: } mcimadamore@580: if (badOutput) { mcimadamore@580: throw new Error("invalid diagnostics for source:\n" + mcimadamore@580: source.getCharContent(true) + mcimadamore@580: "\nOptions: " + xlint.getXlintOption() + mcimadamore@580: "\nExpected unchecked warning: " + warnArr[0] + mcimadamore@580: "\nExpected unsafe vararg warning: " + warnArr[1] + mcimadamore@580: "\nWarnings: " + warnings + mcimadamore@580: "\nNotes: " + notes); mcimadamore@580: } mcimadamore@580: } mcimadamore@580: mcimadamore@580: static class JavaSource extends SimpleJavaFileObject { mcimadamore@580: mcimadamore@580: String source; mcimadamore@580: mcimadamore@580: public JavaSource(SuppressLevel suppressLevel, Signature vararg_meth, Signature client_meth) { mcimadamore@580: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); mcimadamore@580: String meth1 = vararg_meth.template.replace("#arity", "..."); mcimadamore@580: meth1 = meth1.replace("#name", "m"); mcimadamore@580: meth1 = meth1.replace("#body", ""); mcimadamore@580: meth1 = suppressLevel.getSuppressAnnotation() + meth1; mcimadamore@580: String meth2 = client_meth.template.replace("#arity", ""); mcimadamore@580: meth2 = meth2.replace("#name", "test"); mcimadamore@580: meth2 = meth2.replace("#body", "m(arg);"); mcimadamore@580: source = "import java.util.List;\n" + mcimadamore@580: "class Test {\n" + meth1 + mcimadamore@580: "\n" + meth2 + "\n}\n"; mcimadamore@580: } mcimadamore@580: mcimadamore@580: @Override mcimadamore@580: public CharSequence getCharContent(boolean ignoreEncodingErrors) { mcimadamore@580: return source; mcimadamore@580: } mcimadamore@580: } mcimadamore@580: mcimadamore@580: static class DiagnosticChecker implements javax.tools.DiagnosticListener { mcimadamore@580: mcimadamore@580: Set warnings = new HashSet<>(); mcimadamore@580: Set notes = new HashSet<>(); mcimadamore@580: mcimadamore@580: public void report(Diagnostic diagnostic) { mcimadamore@580: if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING || mcimadamore@580: diagnostic.getKind() == Diagnostic.Kind.WARNING) { mcimadamore@580: warnings.add(diagnostic.getCode().contains("varargs") ? mcimadamore@580: Warning.VARARGS : mcimadamore@580: Warning.UNCHECKED); mcimadamore@580: } mcimadamore@580: else if (diagnostic.getKind() == Diagnostic.Kind.NOTE) { mcimadamore@580: notes.add(diagnostic.getCode().contains("varargs") ? mcimadamore@580: Warning.VARARGS : mcimadamore@580: Warning.UNCHECKED); mcimadamore@580: } mcimadamore@580: } mcimadamore@580: } mcimadamore@580: }