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

Thu, 10 Jun 2010 16:08:01 -0700

author
jjg
date
Thu, 10 Jun 2010 16:08:01 -0700
changeset 581
f2fdd52e4e87
parent 580
46cf751559ae
child 795
7b99f98b3035
permissions
-rw-r--r--

6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

mcimadamore@580 1 /*
jjg@581 2 * Copyright (c) 2010, 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
mcimadamore@580 26 * @bug 6945418
mcimadamore@580 27 * @summary Project Coin: Simplified Varargs Method Invocation
mcimadamore@580 28 * @author mcimadamore
mcimadamore@580 29 * @run main Warn4
mcimadamore@580 30 */
mcimadamore@580 31 import com.sun.source.util.JavacTask;
mcimadamore@580 32 import java.net.URI;
mcimadamore@580 33 import java.util.Arrays;
mcimadamore@580 34 import java.util.Set;
mcimadamore@580 35 import java.util.HashSet;
mcimadamore@580 36 import javax.tools.Diagnostic;
mcimadamore@580 37 import javax.tools.JavaCompiler;
mcimadamore@580 38 import javax.tools.JavaFileObject;
mcimadamore@580 39 import javax.tools.SimpleJavaFileObject;
mcimadamore@580 40 import javax.tools.ToolProvider;
mcimadamore@580 41
mcimadamore@580 42 public class Warn4 {
mcimadamore@580 43
mcimadamore@580 44 final static Warning[] error = null;
mcimadamore@580 45 final static Warning[] none = new Warning[] {};
mcimadamore@580 46 final static Warning[] vararg = new Warning[] { Warning.VARARGS };
mcimadamore@580 47 final static Warning[] unchecked = new Warning[] { Warning.UNCHECKED };
mcimadamore@580 48 final static Warning[] both = new Warning[] { Warning.VARARGS, Warning.UNCHECKED };
mcimadamore@580 49
mcimadamore@580 50 enum Warning {
mcimadamore@580 51 UNCHECKED("unchecked"),
mcimadamore@580 52 VARARGS("varargs");
mcimadamore@580 53
mcimadamore@580 54 String category;
mcimadamore@580 55
mcimadamore@580 56 Warning(String category) {
mcimadamore@580 57 this.category = category;
mcimadamore@580 58 }
mcimadamore@580 59
mcimadamore@580 60 boolean isEnabled(XlintOption xlint, SuppressLevel suppressLevel) {
mcimadamore@580 61 return Arrays.asList(xlint.enabledWarnings).contains(this);
mcimadamore@580 62 }
mcimadamore@580 63
mcimadamore@580 64 boolean isSuppressed(SuppressLevel suppressLevel) {
mcimadamore@580 65 return Arrays.asList(suppressLevel.suppressedWarnings).contains(VARARGS);
mcimadamore@580 66 }
mcimadamore@580 67 }
mcimadamore@580 68
mcimadamore@580 69 enum XlintOption {
mcimadamore@580 70 NONE(),
mcimadamore@580 71 UNCHECKED(Warning.UNCHECKED),
mcimadamore@580 72 VARARGS(Warning.VARARGS),
mcimadamore@580 73 ALL(Warning.UNCHECKED, Warning.VARARGS);
mcimadamore@580 74
mcimadamore@580 75 Warning[] enabledWarnings;
mcimadamore@580 76
mcimadamore@580 77 XlintOption(Warning... enabledWarnings) {
mcimadamore@580 78 this.enabledWarnings = enabledWarnings;
mcimadamore@580 79 }
mcimadamore@580 80
mcimadamore@580 81 String getXlintOption() {
mcimadamore@580 82 StringBuilder buf = new StringBuilder();
mcimadamore@580 83 String sep = "";
mcimadamore@580 84 for (Warning w : enabledWarnings) {
mcimadamore@580 85 buf.append(sep);
mcimadamore@580 86 buf.append(w.category);
mcimadamore@580 87 sep=",";
mcimadamore@580 88 }
mcimadamore@580 89 return "-Xlint:" +
mcimadamore@580 90 (this == NONE ? "none" : buf.toString());
mcimadamore@580 91 }
mcimadamore@580 92 }
mcimadamore@580 93
mcimadamore@580 94 enum SuppressLevel {
mcimadamore@580 95 NONE(),
mcimadamore@580 96 UNCHECKED(Warning.UNCHECKED),
mcimadamore@580 97 VARARGS(Warning.VARARGS),
mcimadamore@580 98 ALL(Warning.UNCHECKED, Warning.VARARGS);
mcimadamore@580 99
mcimadamore@580 100 Warning[] suppressedWarnings;
mcimadamore@580 101
mcimadamore@580 102 SuppressLevel(Warning... suppressedWarnings) {
mcimadamore@580 103 this.suppressedWarnings = suppressedWarnings;
mcimadamore@580 104 }
mcimadamore@580 105
mcimadamore@580 106 String getSuppressAnnotation() {
mcimadamore@580 107 StringBuilder buf = new StringBuilder();
mcimadamore@580 108 String sep = "";
mcimadamore@580 109 for (Warning w : suppressedWarnings) {
mcimadamore@580 110 buf.append(sep);
mcimadamore@580 111 buf.append("\"");
mcimadamore@580 112 buf.append(w.category);
mcimadamore@580 113 buf.append("\"");
mcimadamore@580 114 sep=",";
mcimadamore@580 115 }
mcimadamore@580 116 return this == NONE ? "" :
mcimadamore@580 117 "@SuppressWarnings({" + buf.toString() + "})";
mcimadamore@580 118 }
mcimadamore@580 119 }
mcimadamore@580 120
mcimadamore@580 121 enum Signature {
mcimadamore@580 122
mcimadamore@580 123 EXTENDS_TVAR("<Z> void #name(List<? extends Z>#arity arg) { #body }",
mcimadamore@580 124 new Warning[][] {both, both, both, both, error, both, both, both, error}),
mcimadamore@580 125 SUPER_TVAR("<Z> void #name(List<? super Z>#arity arg) { #body }",
mcimadamore@580 126 new Warning[][] {error, both, error, both, error, error, both, both, error}),
mcimadamore@580 127 UNBOUND("void #name(List<?>#arity arg) { #body }",
mcimadamore@580 128 new Warning[][] {none, none, none, none, none, none, none, none, error}),
mcimadamore@580 129 INVARIANT_TVAR("<Z> void #name(List<Z>#arity arg) { #body }",
mcimadamore@580 130 new Warning[][] {both, both, both, both, error, both, both, both, error}),
mcimadamore@580 131 TVAR("<Z> void #name(Z#arity arg) { #body }",
mcimadamore@580 132 new Warning[][] {both, both, both, both, both, both, both, both, vararg}),
mcimadamore@580 133 EXTENDS("void #name(List<? extends String>#arity arg) { #body }",
mcimadamore@580 134 new Warning[][] {error, error, error, error, error, both, error, both, error}),
mcimadamore@580 135 SUPER("void #name(List<? super String>#arity arg) { #body }",
mcimadamore@580 136 new Warning[][] {error, error, error, error, error, error, both, both, error}),
mcimadamore@580 137 INVARIANT("void #name(List<String>#arity arg) { #body }",
mcimadamore@580 138 new Warning[][] {error, error, error, error, error, error, error, both, error}),
mcimadamore@580 139 UNPARAMETERIZED("void #name(String#arity arg) { #body }",
mcimadamore@580 140 new Warning[][] {error, error, error, error, error, error, error, error, none});
mcimadamore@580 141
mcimadamore@580 142 String template;
mcimadamore@580 143 Warning[][] warnings;
mcimadamore@580 144
mcimadamore@580 145 Signature(String template, Warning[][] warnings) {
mcimadamore@580 146 this.template = template;
mcimadamore@580 147 this.warnings = warnings;
mcimadamore@580 148 }
mcimadamore@580 149
mcimadamore@580 150 boolean isApplicableTo(Signature other) {
mcimadamore@580 151 return warnings[other.ordinal()] != null;
mcimadamore@580 152 }
mcimadamore@580 153
mcimadamore@580 154 boolean giveUnchecked(Signature other) {
mcimadamore@580 155 return warnings[other.ordinal()] == unchecked ||
mcimadamore@580 156 warnings[other.ordinal()] == both;
mcimadamore@580 157 }
mcimadamore@580 158
mcimadamore@580 159 boolean giveVarargs(Signature other) {
mcimadamore@580 160 return warnings[other.ordinal()] == vararg ||
mcimadamore@580 161 warnings[other.ordinal()] == both;
mcimadamore@580 162 }
mcimadamore@580 163 }
mcimadamore@580 164
mcimadamore@580 165 public static void main(String... args) throws Exception {
mcimadamore@580 166 for (XlintOption xlint : XlintOption.values()) {
mcimadamore@580 167 for (SuppressLevel suppressLevel : SuppressLevel.values()) {
mcimadamore@580 168 for (Signature vararg_meth : Signature.values()) {
mcimadamore@580 169 for (Signature client_meth : Signature.values()) {
mcimadamore@580 170 if (vararg_meth.isApplicableTo(client_meth)) {
mcimadamore@580 171 test(xlint,
mcimadamore@580 172 suppressLevel,
mcimadamore@580 173 vararg_meth,
mcimadamore@580 174 client_meth);
mcimadamore@580 175 }
mcimadamore@580 176 }
mcimadamore@580 177 }
mcimadamore@580 178 }
mcimadamore@580 179 }
mcimadamore@580 180 }
mcimadamore@580 181
mcimadamore@580 182 static void test(XlintOption xlint, SuppressLevel suppressLevel,
mcimadamore@580 183 Signature vararg_meth, Signature client_meth) throws Exception {
mcimadamore@580 184 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
mcimadamore@580 185 JavaSource source = new JavaSource(suppressLevel, vararg_meth, client_meth);
mcimadamore@580 186 DiagnosticChecker dc = new DiagnosticChecker();
mcimadamore@580 187 JavacTask ct = (JavacTask)tool.getTask(null, null, dc,
mcimadamore@580 188 Arrays.asList(xlint.getXlintOption()), null, Arrays.asList(source));
mcimadamore@580 189 ct.generate(); //to get mandatory notes
mcimadamore@580 190 check(dc.warnings,
mcimadamore@580 191 dc.notes,
mcimadamore@580 192 new boolean[] {vararg_meth.giveUnchecked(client_meth),
mcimadamore@580 193 vararg_meth.giveVarargs(client_meth)},
mcimadamore@580 194 source, xlint, suppressLevel);
mcimadamore@580 195 }
mcimadamore@580 196
mcimadamore@580 197 static void check(Set<Warning> warnings, Set<Warning> notes, boolean[] warnArr, JavaSource source, XlintOption xlint, SuppressLevel suppressLevel) {
mcimadamore@580 198 boolean badOutput = false;
mcimadamore@580 199 for (Warning wkind : Warning.values()) {
mcimadamore@580 200 badOutput |= (warnArr[wkind.ordinal()] && !wkind.isSuppressed(suppressLevel)) !=
mcimadamore@580 201 (wkind.isEnabled(xlint, suppressLevel) ?
mcimadamore@580 202 warnings.contains(wkind) :
mcimadamore@580 203 notes.contains(wkind));
mcimadamore@580 204 }
mcimadamore@580 205 if (badOutput) {
mcimadamore@580 206 throw new Error("invalid diagnostics for source:\n" +
mcimadamore@580 207 source.getCharContent(true) +
mcimadamore@580 208 "\nOptions: " + xlint.getXlintOption() +
mcimadamore@580 209 "\nExpected unchecked warning: " + warnArr[0] +
mcimadamore@580 210 "\nExpected unsafe vararg warning: " + warnArr[1] +
mcimadamore@580 211 "\nWarnings: " + warnings +
mcimadamore@580 212 "\nNotes: " + notes);
mcimadamore@580 213 }
mcimadamore@580 214 }
mcimadamore@580 215
mcimadamore@580 216 static class JavaSource extends SimpleJavaFileObject {
mcimadamore@580 217
mcimadamore@580 218 String source;
mcimadamore@580 219
mcimadamore@580 220 public JavaSource(SuppressLevel suppressLevel, Signature vararg_meth, Signature client_meth) {
mcimadamore@580 221 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
mcimadamore@580 222 String meth1 = vararg_meth.template.replace("#arity", "...");
mcimadamore@580 223 meth1 = meth1.replace("#name", "m");
mcimadamore@580 224 meth1 = meth1.replace("#body", "");
mcimadamore@580 225 meth1 = suppressLevel.getSuppressAnnotation() + meth1;
mcimadamore@580 226 String meth2 = client_meth.template.replace("#arity", "");
mcimadamore@580 227 meth2 = meth2.replace("#name", "test");
mcimadamore@580 228 meth2 = meth2.replace("#body", "m(arg);");
mcimadamore@580 229 source = "import java.util.List;\n" +
mcimadamore@580 230 "class Test {\n" + meth1 +
mcimadamore@580 231 "\n" + meth2 + "\n}\n";
mcimadamore@580 232 }
mcimadamore@580 233
mcimadamore@580 234 @Override
mcimadamore@580 235 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@580 236 return source;
mcimadamore@580 237 }
mcimadamore@580 238 }
mcimadamore@580 239
mcimadamore@580 240 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
mcimadamore@580 241
mcimadamore@580 242 Set<Warning> warnings = new HashSet<>();
mcimadamore@580 243 Set<Warning> notes = new HashSet<>();
mcimadamore@580 244
mcimadamore@580 245 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@580 246 if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING ||
mcimadamore@580 247 diagnostic.getKind() == Diagnostic.Kind.WARNING) {
mcimadamore@580 248 warnings.add(diagnostic.getCode().contains("varargs") ?
mcimadamore@580 249 Warning.VARARGS :
mcimadamore@580 250 Warning.UNCHECKED);
mcimadamore@580 251 }
mcimadamore@580 252 else if (diagnostic.getKind() == Diagnostic.Kind.NOTE) {
mcimadamore@580 253 notes.add(diagnostic.getCode().contains("varargs") ?
mcimadamore@580 254 Warning.VARARGS :
mcimadamore@580 255 Warning.UNCHECKED);
mcimadamore@580 256 }
mcimadamore@580 257 }
mcimadamore@580 258 }
mcimadamore@580 259 }

mercurial