Warn5.java revision 961
3969N/A/*
3969N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3969N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3969N/A *
3969N/A * This code is free software; you can redistribute it and/or modify it
3969N/A * under the terms of the GNU General Public License version 2 only, as
3969N/A * published by the Free Software Foundation.
3969N/A *
3969N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3969N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3969N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3969N/A * version 2 for more details (a copy is included in the LICENSE file that
3969N/A * accompanied this code).
3969N/A *
3969N/A * You should have received a copy of the GNU General Public License version
3969N/A * 2 along with this work; if not, write to the Free Software Foundation,
3969N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3969N/A *
3969N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3969N/A * or visit www.oracle.com if you need additional information or have any
3969N/A * questions.
3969N/A */
3969N/A
3969N/A/**
3969N/A * @test
3969N/A * @bug 6993978
3969N/A * @summary Project Coin: Annotation to reduce varargs warnings
3969N/A * @author mcimadamore
3969N/A * @run main Warn5
3969N/A */
3969N/Aimport com.sun.source.util.JavacTask;
3969N/Aimport com.sun.tools.javac.api.JavacTool;
3969N/Aimport java.net.URI;
3969N/Aimport java.util.ArrayList;
3969N/Aimport java.util.Arrays;
3969N/Aimport javax.tools.Diagnostic;
3969N/Aimport javax.tools.JavaCompiler;
3969N/Aimport javax.tools.JavaFileObject;
3969N/Aimport javax.tools.SimpleJavaFileObject;
3969N/Aimport javax.tools.StandardJavaFileManager;
3969N/Aimport javax.tools.ToolProvider;
3969N/A
3969N/Apublic class Warn5 {
3969N/A
3969N/A enum XlintOption {
3969N/A NONE("none"),
3969N/A ALL("all");
3969N/A
3969N/A String opt;
3969N/A
3969N/A XlintOption(String opt) {
3969N/A this.opt = opt;
3969N/A }
3969N/A
3969N/A String getXlintOption() {
3969N/A return "-Xlint:" + opt;
3969N/A }
3969N/A }
3969N/A
3969N/A enum TrustMe {
3969N/A DONT_TRUST(""),
3969N/A TRUST("@java.lang.SafeVarargs");
3969N/A
3969N/A String anno;
3969N/A
3969N/A TrustMe(String anno) {
3969N/A this.anno = anno;
3969N/A }
3969N/A }
3969N/A
3969N/A enum SuppressLevel {
3969N/A NONE,
3969N/A VARARGS;
3969N/A
3969N/A String getSuppressAnno() {
3969N/A return this == VARARGS ?
3969N/A "@SuppressWarnings(\"varargs\")" :
3969N/A "";
3969N/A }
3969N/A }
3969N/A
3969N/A enum ModifierKind {
3969N/A NONE(""),
3969N/A FINAL("final"),
3969N/A STATIC("static");
3969N/A
3969N/A String mod;
3969N/A
3969N/A ModifierKind(String mod) {
3969N/A this.mod = mod;
3969N/A }
3969N/A }
3969N/A
3969N/A enum MethodKind {
3969N/A METHOD("void m"),
3969N/A CONSTRUCTOR("Test");
3969N/A
3969N/A
3969N/A String name;
3969N/A
3969N/A MethodKind(String name) {
3969N/A this.name = name;
3969N/A }
3969N/A }
3969N/A
3969N/A enum SourceLevel {
3969N/A JDK_6("6"),
3969N/A JDK_7("7");
3969N/A
3969N/A String sourceKey;
3969N/A
3969N/A SourceLevel(String sourceKey) {
3969N/A this.sourceKey = sourceKey;
3969N/A }
3969N/A }
3969N/A
3969N/A enum SignatureKind {
3969N/A VARARGS_X("#K <X>#N(X... x)", false, true),
3969N/A VARARGS_STRING("#K #N(String... x)", true, true),
3969N/A ARRAY_X("#K <X>#N(X[] x)", false, false),
3969N/A ARRAY_STRING("#K #N(String[] x)", true, false);
3969N/A
3969N/A String stub;
3969N/A boolean isReifiableArg;
3969N/A boolean isVarargs;
3969N/A
3969N/A SignatureKind(String stub, boolean isReifiableArg, boolean isVarargs) {
3969N/A this.stub = stub;
3969N/A this.isReifiableArg = isReifiableArg;
3969N/A this.isVarargs = isVarargs;
3969N/A }
3969N/A
3969N/A String getSignature(ModifierKind modKind, MethodKind methKind) {
3969N/A return methKind != MethodKind.CONSTRUCTOR ?
3969N/A stub.replace("#K", modKind.mod).replace("#N", methKind.name) :
3969N/A stub.replace("#K", "").replace("#N", methKind.name);
3969N/A }
3969N/A }
3969N/A
3969N/A enum BodyKind {
3969N/A ASSIGN("Object o = x;", true),
3969N/A CAST("Object o = (Object)x;", true),
3969N/A METH("test(x);", true),
3969N/A PRINT("System.out.println(x.toString());", false),
3969N/A ARRAY_ASSIGN("Object[] o = x;", true),
3969N/A ARRAY_CAST("Object[] o = (Object[])x;", true),
3969N/A ARRAY_METH("testArr(x);", true);
3969N/A
3969N/A String body;
3969N/A boolean hasAliasing;
3969N/A
3969N/A BodyKind(String body, boolean hasAliasing) {
3969N/A this.body = body;
3969N/A this.hasAliasing = hasAliasing;
3969N/A }
3969N/A }
3969N/A
3969N/A static class JavaSource extends SimpleJavaFileObject {
3969N/A
3969N/A String template = "import com.sun.tools.javac.api.*;\n" +
3969N/A "import java.util.List;\n" +
3969N/A "class Test {\n" +
3969N/A " static void test(Object o) {}\n" +
3969N/A " static void testArr(Object[] o) {}\n" +
3969N/A " #T \n #S #M { #B }\n" +
3969N/A "}\n";
3969N/A
3969N/A String source;
3969N/A
3969N/A public JavaSource(TrustMe trustMe, SuppressLevel suppressLevel, ModifierKind modKind,
3969N/A MethodKind methKind, SignatureKind meth, BodyKind body) {
3969N/A super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
3969N/A source = template.replace("#T", trustMe.anno).
3969N/A replace("#S", suppressLevel.getSuppressAnno()).
3969N/A replace("#M", meth.getSignature(modKind, methKind)).
3969N/A replace("#B", body.body);
3969N/A }
3969N/A
3969N/A @Override
3969N/A public CharSequence getCharContent(boolean ignoreEncodingErrors) {
3969N/A return source;
3969N/A }
3969N/A }
3969N/A
3969N/A public static void main(String... args) throws Exception {
3969N/A for (SourceLevel sourceLevel : SourceLevel.values()) {
3969N/A for (XlintOption xlint : XlintOption.values()) {
3969N/A for (TrustMe trustMe : TrustMe.values()) {
3969N/A for (SuppressLevel suppressLevel : SuppressLevel.values()) {
3969N/A for (ModifierKind modKind : ModifierKind.values()) {
3969N/A for (MethodKind methKind : MethodKind.values()) {
3969N/A for (SignatureKind sig : SignatureKind.values()) {
3969N/A for (BodyKind body : BodyKind.values()) {
3969N/A test(sourceLevel,
3969N/A xlint,
3969N/A trustMe,
3969N/A suppressLevel,
3969N/A modKind,
3969N/A methKind,
3969N/A sig,
3969N/A body);
3969N/A }
3969N/A }
}
}
}
}
}
}
}
// Create a single file manager and reuse it for each compile to save time.
static StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
static void test(SourceLevel sourceLevel, XlintOption xlint, TrustMe trustMe, SuppressLevel suppressLevel,
ModifierKind modKind, MethodKind methKind, SignatureKind sig, BodyKind body) throws Exception {
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavaSource source = new JavaSource(trustMe, suppressLevel, modKind, methKind, sig, body);
DiagnosticChecker dc = new DiagnosticChecker();
JavacTask ct = (JavacTask)tool.getTask(null, fm, dc,
Arrays.asList(xlint.getXlintOption(), "-source", sourceLevel.sourceKey), null, Arrays.asList(source));
ct.analyze();
check(sourceLevel, dc, source, xlint, trustMe,
suppressLevel, modKind, methKind, sig, body);
}
static void check(SourceLevel sourceLevel, DiagnosticChecker dc, JavaSource source,
XlintOption xlint, TrustMe trustMe, SuppressLevel suppressLevel, ModifierKind modKind,
MethodKind methKind, SignatureKind meth, BodyKind body) {
boolean hasPotentiallyUnsafeBody = sourceLevel == SourceLevel.JDK_7 &&
trustMe == TrustMe.TRUST &&
suppressLevel != SuppressLevel.VARARGS &&
xlint != XlintOption.NONE &&
meth.isVarargs && !meth.isReifiableArg && body.hasAliasing &&
(methKind == MethodKind.CONSTRUCTOR || (methKind == MethodKind.METHOD && modKind != ModifierKind.NONE));
boolean hasPotentiallyPollutingDecl = sourceLevel == SourceLevel.JDK_7 &&
trustMe == TrustMe.DONT_TRUST &&
meth.isVarargs &&
!meth.isReifiableArg &&
xlint == XlintOption.ALL;
boolean hasMalformedAnnoInDecl = sourceLevel == SourceLevel.JDK_7 &&
trustMe == TrustMe.TRUST &&
(!meth.isVarargs ||
(modKind == ModifierKind.NONE && methKind == MethodKind.METHOD));
boolean hasRedundantAnnoInDecl = sourceLevel == SourceLevel.JDK_7 &&
trustMe == TrustMe.TRUST &&
xlint != XlintOption.NONE &&
suppressLevel != SuppressLevel.VARARGS &&
(modKind != ModifierKind.NONE || methKind == MethodKind.CONSTRUCTOR) &&
meth.isVarargs &&
meth.isReifiableArg;
if (hasPotentiallyUnsafeBody != dc.hasPotentiallyUnsafeBody ||
hasPotentiallyPollutingDecl != dc.hasPotentiallyPollutingDecl ||
hasMalformedAnnoInDecl != dc.hasMalformedAnnoInDecl ||
hasRedundantAnnoInDecl != dc.hasRedundantAnnoInDecl) {
throw new Error("invalid diagnostics for source:\n" +
source.getCharContent(true) +
"\nOptions: " + xlint.getXlintOption() +
"\nExpected potentially unsafe body warning: " + hasPotentiallyUnsafeBody +
"\nExpected potentially polluting decl warning: " + hasPotentiallyPollutingDecl +
"\nExpected malformed anno error: " + hasMalformedAnnoInDecl +
"\nExpected redundant anno warning: " + hasRedundantAnnoInDecl +
"\nFound potentially unsafe body warning: " + dc.hasPotentiallyUnsafeBody +
"\nFound potentially polluting decl warning: " + dc.hasPotentiallyPollutingDecl +
"\nFound malformed anno error: " + dc.hasMalformedAnnoInDecl +
"\nFound redundant anno warning: " + dc.hasRedundantAnnoInDecl);
}
}
static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
boolean hasPotentiallyUnsafeBody = false;
boolean hasPotentiallyPollutingDecl = false;
boolean hasMalformedAnnoInDecl = false;
boolean hasRedundantAnnoInDecl = false;
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.WARNING) {
if (diagnostic.getCode().contains("unsafe.use.varargs.param")) {
hasPotentiallyUnsafeBody = true;
} else if (diagnostic.getCode().contains("redundant.trustme")) {
hasRedundantAnnoInDecl = true;
}
} else if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING &&
diagnostic.getCode().contains("varargs.non.reifiable.type")) {
hasPotentiallyPollutingDecl = true;
} else if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
diagnostic.getCode().contains("invalid.trustme")) {
hasMalformedAnnoInDecl = true;
}
}
}
}