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