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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/varargs/warning/Warn4.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,301 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * @test
    1.29 + * @bug     6945418 6993978 8006694
    1.30 + * @summary Project Coin: Simplified Varargs Method Invocation
    1.31 + *  temporarily workaround combo tests are causing time out in several platforms
    1.32 + * @author  mcimadamore
    1.33 + * @library ../../lib
    1.34 + * @build JavacTestingAbstractThreadedTest
    1.35 + * @run main/othervm Warn4
    1.36 + */
    1.37 +
    1.38 +// use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
    1.39 +// see JDK-8006746
    1.40 +
    1.41 +import java.net.URI;
    1.42 +import java.util.Arrays;
    1.43 +import java.util.Set;
    1.44 +import java.util.HashSet;
    1.45 +import javax.tools.Diagnostic;
    1.46 +import javax.tools.JavaCompiler;
    1.47 +import javax.tools.JavaFileObject;
    1.48 +import javax.tools.SimpleJavaFileObject;
    1.49 +import javax.tools.ToolProvider;
    1.50 +import com.sun.source.util.JavacTask;
    1.51 +
    1.52 +public class Warn4
    1.53 +    extends JavacTestingAbstractThreadedTest
    1.54 +    implements Runnable {
    1.55 +
    1.56 +    final static Warning[] error = null;
    1.57 +    final static Warning[] none = new Warning[] {};
    1.58 +    final static Warning[] vararg = new Warning[] { Warning.VARARGS };
    1.59 +    final static Warning[] unchecked = new Warning[] { Warning.UNCHECKED };
    1.60 +    final static Warning[] both =
    1.61 +            new Warning[] { Warning.VARARGS, Warning.UNCHECKED };
    1.62 +
    1.63 +    enum Warning {
    1.64 +        UNCHECKED("generic.array.creation"),
    1.65 +        VARARGS("varargs.non.reifiable.type");
    1.66 +
    1.67 +        String key;
    1.68 +
    1.69 +        Warning(String key) {
    1.70 +            this.key = key;
    1.71 +        }
    1.72 +
    1.73 +        boolean isSuppressed(TrustMe trustMe, SourceLevel source,
    1.74 +                SuppressLevel suppressLevelClient,
    1.75 +                SuppressLevel suppressLevelDecl,
    1.76 +                ModifierKind modKind) {
    1.77 +            switch(this) {
    1.78 +                case VARARGS:
    1.79 +                    return source == SourceLevel.JDK_6 ||
    1.80 +                            suppressLevelDecl == SuppressLevel.UNCHECKED ||
    1.81 +                            trustMe == TrustMe.TRUST;
    1.82 +                case UNCHECKED:
    1.83 +                    return suppressLevelClient == SuppressLevel.UNCHECKED ||
    1.84 +                        (trustMe == TrustMe.TRUST && modKind !=
    1.85 +                            ModifierKind.NONE && source == SourceLevel.JDK_7);
    1.86 +            }
    1.87 +
    1.88 +            SuppressLevel supLev = this == VARARGS ?
    1.89 +                suppressLevelDecl :
    1.90 +                suppressLevelClient;
    1.91 +            return supLev == SuppressLevel.UNCHECKED ||
    1.92 +                    (trustMe == TrustMe.TRUST && modKind != ModifierKind.NONE);
    1.93 +        }
    1.94 +    }
    1.95 +
    1.96 +    enum SourceLevel {
    1.97 +        JDK_6("6"),
    1.98 +        JDK_7("7");
    1.99 +
   1.100 +        String sourceKey;
   1.101 +
   1.102 +        SourceLevel(String sourceKey) {
   1.103 +            this.sourceKey = sourceKey;
   1.104 +        }
   1.105 +    }
   1.106 +
   1.107 +    enum TrustMe {
   1.108 +        DONT_TRUST(""),
   1.109 +        TRUST("@java.lang.SafeVarargs");
   1.110 +
   1.111 +        String anno;
   1.112 +
   1.113 +        TrustMe(String anno) {
   1.114 +            this.anno = anno;
   1.115 +        }
   1.116 +    }
   1.117 +
   1.118 +    enum ModifierKind {
   1.119 +        NONE(" "),
   1.120 +        FINAL("final "),
   1.121 +        STATIC("static ");
   1.122 +
   1.123 +        String mod;
   1.124 +
   1.125 +        ModifierKind(String mod) {
   1.126 +            this.mod = mod;
   1.127 +        }
   1.128 +    }
   1.129 +
   1.130 +    enum SuppressLevel {
   1.131 +        NONE(""),
   1.132 +        UNCHECKED("unchecked");
   1.133 +
   1.134 +        String lint;
   1.135 +
   1.136 +        SuppressLevel(String lint) {
   1.137 +            this.lint = lint;
   1.138 +        }
   1.139 +
   1.140 +        String getSuppressAnno() {
   1.141 +            return "@SuppressWarnings(\"" + lint + "\")";
   1.142 +        }
   1.143 +    }
   1.144 +
   1.145 +    enum Signature {
   1.146 +        UNBOUND("void #name(List<?>#arity arg) { #body }",
   1.147 +            new Warning[][] {none, none, none, none, error}),
   1.148 +        INVARIANT_TVAR("<Z> void #name(List<Z>#arity arg) { #body }",
   1.149 +            new Warning[][] {both, both, error, both, error}),
   1.150 +        TVAR("<Z> void #name(Z#arity arg) { #body }",
   1.151 +            new Warning[][] {both, both, both, both, vararg}),
   1.152 +        INVARIANT("void #name(List<String>#arity arg) { #body }",
   1.153 +            new Warning[][] {error, error, error, both, error}),
   1.154 +        UNPARAMETERIZED("void #name(String#arity arg) { #body }",
   1.155 +            new Warning[][] {error, error, error, error, none});
   1.156 +
   1.157 +        String template;
   1.158 +        Warning[][] warnings;
   1.159 +
   1.160 +        Signature(String template, Warning[][] warnings) {
   1.161 +            this.template = template;
   1.162 +            this.warnings = warnings;
   1.163 +        }
   1.164 +
   1.165 +        boolean isApplicableTo(Signature other) {
   1.166 +            return warnings[other.ordinal()] != null;
   1.167 +        }
   1.168 +
   1.169 +        boolean giveUnchecked(Signature other) {
   1.170 +            return warnings[other.ordinal()] == unchecked ||
   1.171 +                    warnings[other.ordinal()] == both;
   1.172 +        }
   1.173 +
   1.174 +        boolean giveVarargs(Signature other) {
   1.175 +            return warnings[other.ordinal()] == vararg ||
   1.176 +                    warnings[other.ordinal()] == both;
   1.177 +        }
   1.178 +    }
   1.179 +
   1.180 +    public static void main(String... args) throws Exception {
   1.181 +        for (SourceLevel sourceLevel : SourceLevel.values()) {
   1.182 +            for (TrustMe trustMe : TrustMe.values()) {
   1.183 +                for (SuppressLevel suppressLevelClient : SuppressLevel.values()) {
   1.184 +                    for (SuppressLevel suppressLevelDecl : SuppressLevel.values()) {
   1.185 +                        for (ModifierKind modKind : ModifierKind.values()) {
   1.186 +                            for (Signature vararg_meth : Signature.values()) {
   1.187 +                                for (Signature client_meth : Signature.values()) {
   1.188 +                                    if (vararg_meth.isApplicableTo(client_meth)) {
   1.189 +                                        pool.execute(new Warn4(sourceLevel,
   1.190 +                                                trustMe,
   1.191 +                                                suppressLevelClient,
   1.192 +                                                suppressLevelDecl,
   1.193 +                                                modKind,
   1.194 +                                                vararg_meth,
   1.195 +                                                client_meth));
   1.196 +                                    }
   1.197 +                                }
   1.198 +                            }
   1.199 +                        }
   1.200 +                    }
   1.201 +                }
   1.202 +            }
   1.203 +        }
   1.204 +
   1.205 +        checkAfterExec();
   1.206 +    }
   1.207 +
   1.208 +    SourceLevel sourceLevel;
   1.209 +    TrustMe trustMe;
   1.210 +    SuppressLevel suppressLevelClient;
   1.211 +    SuppressLevel suppressLevelDecl;
   1.212 +    ModifierKind modKind;
   1.213 +    Signature vararg_meth;
   1.214 +    Signature client_meth;
   1.215 +    DiagnosticChecker diagChecker;
   1.216 +
   1.217 +    public Warn4(SourceLevel sourceLevel, TrustMe trustMe,
   1.218 +            SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl,
   1.219 +            ModifierKind modKind, Signature vararg_meth, Signature client_meth) {
   1.220 +        this.sourceLevel = sourceLevel;
   1.221 +        this.trustMe = trustMe;
   1.222 +        this.suppressLevelClient = suppressLevelClient;
   1.223 +        this.suppressLevelDecl = suppressLevelDecl;
   1.224 +        this.modKind = modKind;
   1.225 +        this.vararg_meth = vararg_meth;
   1.226 +        this.client_meth = client_meth;
   1.227 +        this.diagChecker = new DiagnosticChecker();
   1.228 +    }
   1.229 +
   1.230 +    @Override
   1.231 +    public void run() {
   1.232 +        int id = checkCount.incrementAndGet();
   1.233 +        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   1.234 +        JavaSource source = new JavaSource(id);
   1.235 +        JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), diagChecker,
   1.236 +                Arrays.asList("-Xlint:unchecked", "-source", sourceLevel.sourceKey),
   1.237 +                null, Arrays.asList(source));
   1.238 +        ct.call(); //to get mandatory notes
   1.239 +        check(source, new boolean[] {vararg_meth.giveUnchecked(client_meth),
   1.240 +                               vararg_meth.giveVarargs(client_meth)});
   1.241 +    }
   1.242 +
   1.243 +    void check(JavaSource source, boolean[] warnArr) {
   1.244 +        boolean badOutput = false;
   1.245 +        for (Warning wkind : Warning.values()) {
   1.246 +            boolean isSuppressed = wkind.isSuppressed(trustMe, sourceLevel,
   1.247 +                    suppressLevelClient, suppressLevelDecl, modKind);
   1.248 +            badOutput |= (warnArr[wkind.ordinal()] && !isSuppressed) !=
   1.249 +                    diagChecker.warnings.contains(wkind);
   1.250 +        }
   1.251 +        if (badOutput) {
   1.252 +            throw new Error("invalid diagnostics for source:\n" +
   1.253 +                    source.getCharContent(true) +
   1.254 +                    "\nExpected unchecked warning: " + warnArr[0] +
   1.255 +                    "\nExpected unsafe vararg warning: " + warnArr[1] +
   1.256 +                    "\nWarnings: " + diagChecker.warnings +
   1.257 +                    "\nSource level: " + sourceLevel);
   1.258 +        }
   1.259 +    }
   1.260 +
   1.261 +    class JavaSource extends SimpleJavaFileObject {
   1.262 +
   1.263 +        String source;
   1.264 +
   1.265 +        public JavaSource(int id) {
   1.266 +            super(URI.create(String.format("myfo:/Test%d.java", id)),
   1.267 +                    JavaFileObject.Kind.SOURCE);
   1.268 +            String meth1 = vararg_meth.template.replace("#arity", "...");
   1.269 +            meth1 = meth1.replace("#name", "m");
   1.270 +            meth1 = meth1.replace("#body", "");
   1.271 +            meth1 = trustMe.anno + "\n" + suppressLevelDecl.getSuppressAnno() +
   1.272 +                    modKind.mod + meth1;
   1.273 +            String meth2 = client_meth.template.replace("#arity", "");
   1.274 +            meth2 = meth2.replace("#name", "test");
   1.275 +            meth2 = meth2.replace("#body", "m(arg);");
   1.276 +            meth2 = suppressLevelClient.getSuppressAnno() + meth2;
   1.277 +            source = String.format("import java.util.List;\n" +
   1.278 +                     "class Test%s {\n %s \n %s \n } \n", id, meth1, meth2);
   1.279 +        }
   1.280 +
   1.281 +        @Override
   1.282 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.283 +            return source;
   1.284 +        }
   1.285 +    }
   1.286 +
   1.287 +    static class DiagnosticChecker
   1.288 +        implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.289 +
   1.290 +        Set<Warning> warnings = new HashSet<>();
   1.291 +
   1.292 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.293 +            if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING ||
   1.294 +                    diagnostic.getKind() == Diagnostic.Kind.WARNING) {
   1.295 +                if (diagnostic.getCode().contains(Warning.VARARGS.key)) {
   1.296 +                    warnings.add(Warning.VARARGS);
   1.297 +                } else if(diagnostic.getCode().contains(Warning.UNCHECKED.key)) {
   1.298 +                    warnings.add(Warning.UNCHECKED);
   1.299 +                }
   1.300 +            }
   1.301 +        }
   1.302 +    }
   1.303 +
   1.304 +}

mercurial