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@795: * @bug 6993978 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.ArrayList; mcimadamore@795: import java.util.Arrays; 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: 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@795: static 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@795: public JavaSource(TrustMe trustMe, SuppressLevel suppressLevel, ModifierKind modKind, mcimadamore@795: MethodKind methKind, SignatureKind meth, BodyKind body) { 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@795: replace("#M", meth.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@795: public static void main(String... args) throws Exception { mcimadamore@795: for (SourceLevel sourceLevel : SourceLevel.values()) { mcimadamore@795: for (XlintOption xlint : XlintOption.values()) { mcimadamore@795: for (TrustMe trustMe : TrustMe.values()) { mcimadamore@795: for (SuppressLevel suppressLevel : SuppressLevel.values()) { mcimadamore@795: for (ModifierKind modKind : ModifierKind.values()) { mcimadamore@795: for (MethodKind methKind : MethodKind.values()) { mcimadamore@795: for (SignatureKind sig : SignatureKind.values()) { mcimadamore@795: for (BodyKind body : BodyKind.values()) { mcimadamore@795: test(sourceLevel, mcimadamore@795: xlint, mcimadamore@795: trustMe, mcimadamore@795: suppressLevel, mcimadamore@795: modKind, mcimadamore@795: methKind, mcimadamore@795: sig, mcimadamore@795: body); mcimadamore@795: } mcimadamore@795: } mcimadamore@795: } mcimadamore@795: } mcimadamore@795: } mcimadamore@795: } mcimadamore@795: } mcimadamore@795: } mcimadamore@795: } mcimadamore@795: jjh@892: // Create a single file manager and reuse it for each compile to save time. jjh@892: static StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null); jjh@892: mcimadamore@795: static void test(SourceLevel sourceLevel, XlintOption xlint, TrustMe trustMe, SuppressLevel suppressLevel, mcimadamore@795: ModifierKind modKind, MethodKind methKind, SignatureKind sig, BodyKind body) throws Exception { mcimadamore@795: final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); mcimadamore@795: JavaSource source = new JavaSource(trustMe, suppressLevel, modKind, methKind, sig, body); mcimadamore@795: DiagnosticChecker dc = new DiagnosticChecker(); jjh@892: JavacTask ct = (JavacTask)tool.getTask(null, fm, dc, mcimadamore@795: Arrays.asList(xlint.getXlintOption(), "-source", sourceLevel.sourceKey), null, Arrays.asList(source)); mcimadamore@795: ct.analyze(); mcimadamore@795: check(sourceLevel, dc, source, xlint, trustMe, mcimadamore@795: suppressLevel, modKind, methKind, sig, body); mcimadamore@795: } mcimadamore@795: mcimadamore@795: static void check(SourceLevel sourceLevel, DiagnosticChecker dc, JavaSource source, mcimadamore@795: XlintOption xlint, TrustMe trustMe, SuppressLevel suppressLevel, ModifierKind modKind, mcimadamore@795: MethodKind methKind, SignatureKind meth, BodyKind body) { mcimadamore@795: mcimadamore@795: boolean hasPotentiallyUnsafeBody = sourceLevel == SourceLevel.JDK_7 && mcimadamore@795: trustMe == TrustMe.TRUST && mcimadamore@795: suppressLevel != SuppressLevel.VARARGS && mcimadamore@795: xlint != XlintOption.NONE && mcimadamore@795: meth.isVarargs && !meth.isReifiableArg && body.hasAliasing && mcimadamore@795: (methKind == MethodKind.CONSTRUCTOR || (methKind == MethodKind.METHOD && modKind != ModifierKind.NONE)); mcimadamore@795: mcimadamore@795: boolean hasPotentiallyPollutingDecl = sourceLevel == SourceLevel.JDK_7 && mcimadamore@795: trustMe == TrustMe.DONT_TRUST && mcimadamore@795: meth.isVarargs && mcimadamore@795: !meth.isReifiableArg && mcimadamore@795: xlint == XlintOption.ALL; mcimadamore@795: mcimadamore@795: boolean hasMalformedAnnoInDecl = sourceLevel == SourceLevel.JDK_7 && mcimadamore@795: trustMe == TrustMe.TRUST && mcimadamore@795: (!meth.isVarargs || mcimadamore@795: (modKind == ModifierKind.NONE && methKind == MethodKind.METHOD)); mcimadamore@795: mcimadamore@795: boolean hasRedundantAnnoInDecl = sourceLevel == SourceLevel.JDK_7 && mcimadamore@795: trustMe == TrustMe.TRUST && mcimadamore@795: xlint != XlintOption.NONE && mcimadamore@795: suppressLevel != SuppressLevel.VARARGS && mcimadamore@795: (modKind != ModifierKind.NONE || methKind == MethodKind.CONSTRUCTOR) && mcimadamore@795: meth.isVarargs && mcimadamore@795: meth.isReifiableArg; mcimadamore@795: mcimadamore@795: if (hasPotentiallyUnsafeBody != dc.hasPotentiallyUnsafeBody || mcimadamore@795: hasPotentiallyPollutingDecl != dc.hasPotentiallyPollutingDecl || mcimadamore@795: hasMalformedAnnoInDecl != dc.hasMalformedAnnoInDecl || mcimadamore@795: hasRedundantAnnoInDecl != dc.hasRedundantAnnoInDecl) { mcimadamore@795: throw new Error("invalid diagnostics for source:\n" + mcimadamore@795: source.getCharContent(true) + mcimadamore@795: "\nOptions: " + xlint.getXlintOption() + mcimadamore@795: "\nExpected potentially unsafe body warning: " + hasPotentiallyUnsafeBody + mcimadamore@795: "\nExpected potentially polluting decl warning: " + hasPotentiallyPollutingDecl + mcimadamore@795: "\nExpected malformed anno error: " + hasMalformedAnnoInDecl + mcimadamore@795: "\nExpected redundant anno warning: " + hasRedundantAnnoInDecl + mcimadamore@795: "\nFound potentially unsafe body warning: " + dc.hasPotentiallyUnsafeBody + mcimadamore@795: "\nFound potentially polluting decl warning: " + dc.hasPotentiallyPollutingDecl + mcimadamore@795: "\nFound malformed anno error: " + dc.hasMalformedAnnoInDecl + mcimadamore@795: "\nFound redundant anno warning: " + dc.hasRedundantAnnoInDecl); mcimadamore@795: } mcimadamore@795: } mcimadamore@795: mcimadamore@795: static class DiagnosticChecker implements javax.tools.DiagnosticListener { mcimadamore@795: mcimadamore@795: boolean hasPotentiallyUnsafeBody = false; mcimadamore@795: boolean hasPotentiallyPollutingDecl = false; mcimadamore@795: boolean hasMalformedAnnoInDecl = false; mcimadamore@795: boolean hasRedundantAnnoInDecl = false; 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@795: hasPotentiallyUnsafeBody = true; mcimadamore@795: } else if (diagnostic.getCode().contains("redundant.trustme")) { mcimadamore@795: hasRedundantAnnoInDecl = true; mcimadamore@795: } mcimadamore@795: } else if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING && mcimadamore@795: diagnostic.getCode().contains("varargs.non.reifiable.type")) { mcimadamore@795: hasPotentiallyPollutingDecl = true; mcimadamore@795: } else if (diagnostic.getKind() == Diagnostic.Kind.ERROR && mcimadamore@795: diagnostic.getCode().contains("invalid.trustme")) { mcimadamore@795: hasMalformedAnnoInDecl = true; mcimadamore@795: } mcimadamore@795: } mcimadamore@795: } mcimadamore@795: }