test/tools/javac/lambda/MethodReferenceParserTest.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/lambda/MethodReferenceParserTest.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,265 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 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 7115052 8003280 8006694
    1.30 + * @summary Add lambda tests
    1.31 + *  Add parser support for method references
    1.32 + *  temporarily workaround combo tests are causing time out in several platforms
    1.33 + * @library ../lib
    1.34 + * @build JavacTestingAbstractThreadedTest
    1.35 + * @run main/othervm MethodReferenceParserTest
    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 javax.tools.Diagnostic;
    1.44 +import javax.tools.JavaFileObject;
    1.45 +import javax.tools.SimpleJavaFileObject;
    1.46 +import com.sun.source.util.JavacTask;
    1.47 +
    1.48 +public class MethodReferenceParserTest
    1.49 +    extends JavacTestingAbstractThreadedTest
    1.50 +    implements Runnable {
    1.51 +
    1.52 +    enum ReferenceKind {
    1.53 +        METHOD_REF("#Q::#Gm"),
    1.54 +        CONSTRUCTOR_REF("#Q::#Gnew"),
    1.55 +        FALSE_REF("min < max"),
    1.56 +        ERR_SUPER("#Q::#Gsuper"),
    1.57 +        ERR_METH0("#Q::#Gm()"),
    1.58 +        ERR_METH1("#Q::#Gm(X)"),
    1.59 +        ERR_CONSTR0("#Q::#Gnew()"),
    1.60 +        ERR_CONSTR1("#Q::#Gnew(X)");
    1.61 +
    1.62 +        String referenceTemplate;
    1.63 +
    1.64 +        ReferenceKind(String referenceTemplate) {
    1.65 +            this.referenceTemplate = referenceTemplate;
    1.66 +        }
    1.67 +
    1.68 +        String getReferenceString(QualifierKind qk, GenericKind gk) {
    1.69 +            return referenceTemplate
    1.70 +                    .replaceAll("#Q", qk.qualifier)
    1.71 +                    .replaceAll("#G", gk.typeParameters);
    1.72 +        }
    1.73 +
    1.74 +        boolean erroneous() {
    1.75 +            switch (this) {
    1.76 +                case ERR_SUPER:
    1.77 +                case ERR_METH0:
    1.78 +                case ERR_METH1:
    1.79 +                case ERR_CONSTR0:
    1.80 +                case ERR_CONSTR1:
    1.81 +                    return true;
    1.82 +                default: return false;
    1.83 +            }
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    enum ContextKind {
    1.88 +        ASSIGN("SAM s = #E;"),
    1.89 +        METHOD("m(#E, i);");
    1.90 +
    1.91 +        String contextTemplate;
    1.92 +
    1.93 +        ContextKind(String contextTemplate) {
    1.94 +            this.contextTemplate = contextTemplate;
    1.95 +        }
    1.96 +
    1.97 +        String contextString(ExprKind ek, ReferenceKind rk, QualifierKind qk,
    1.98 +                GenericKind gk, SubExprKind sk) {
    1.99 +            return contextTemplate.replaceAll("#E", ek.expressionString(rk, qk, gk, sk));
   1.100 +        }
   1.101 +    }
   1.102 +
   1.103 +    enum GenericKind {
   1.104 +        NONE(""),
   1.105 +        ONE("<X>"),
   1.106 +        TWO("<X,Y>");
   1.107 +
   1.108 +        String typeParameters;
   1.109 +
   1.110 +        GenericKind(String typeParameters) {
   1.111 +            this.typeParameters = typeParameters;
   1.112 +        }
   1.113 +    }
   1.114 +
   1.115 +    enum QualifierKind {
   1.116 +        THIS("this"),
   1.117 +        SUPER("super"),
   1.118 +        NEW("new Foo()"),
   1.119 +        METHOD("m()"),
   1.120 +        FIELD("a.f"),
   1.121 +        UBOUND_SIMPLE("A"),
   1.122 +        UNBOUND_ARRAY1("int[]"),
   1.123 +        UNBOUND_ARRAY2("A<G>[][]"),
   1.124 +        UNBOUND_GENERIC1("A<X>"),
   1.125 +        UNBOUND_GENERIC2("A<X, Y>"),
   1.126 +        UNBOUND_GENERIC3("A<? extends X, ? super Y>"),
   1.127 +        UNBOUND_GENERIC4("A<int[], short[][]>"),
   1.128 +        NESTED_GENERIC1("A<A<X,Y>, A<X,Y>>"),
   1.129 +        NESTED_GENERIC2("A<A<A<X,Y>,A<X,Y>>, A<A<X,Y>,A<X,Y>>>");
   1.130 +
   1.131 +        String qualifier;
   1.132 +
   1.133 +        QualifierKind(String qualifier) {
   1.134 +            this.qualifier = qualifier;
   1.135 +        }
   1.136 +    }
   1.137 +
   1.138 +    enum ExprKind {
   1.139 +        NONE("#R::S"),
   1.140 +        SINGLE_PAREN1("(#R#S)"),
   1.141 +        SINGLE_PAREN2("(#R)#S"),
   1.142 +        DOUBLE_PAREN1("((#R#S))"),
   1.143 +        DOUBLE_PAREN2("((#R)#S)"),
   1.144 +        DOUBLE_PAREN3("((#R))#S");
   1.145 +
   1.146 +        String expressionTemplate;
   1.147 +
   1.148 +        ExprKind(String expressionTemplate) {
   1.149 +            this.expressionTemplate = expressionTemplate;
   1.150 +        }
   1.151 +
   1.152 +        String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
   1.153 +            return expressionTemplate
   1.154 +                    .replaceAll("#R", rk.getReferenceString(qk, gk))
   1.155 +                    .replaceAll("#S", sk.subExpression);
   1.156 +        }
   1.157 +    }
   1.158 +
   1.159 +    enum SubExprKind {
   1.160 +        NONE(""),
   1.161 +        SELECT_FIELD(".f"),
   1.162 +        SELECT_METHOD(".f()"),
   1.163 +        SELECT_NEW(".new Foo()"),
   1.164 +        POSTINC("++"),
   1.165 +        POSTDEC("--");
   1.166 +
   1.167 +        String subExpression;
   1.168 +
   1.169 +        SubExprKind(String subExpression) {
   1.170 +            this.subExpression = subExpression;
   1.171 +        }
   1.172 +    }
   1.173 +
   1.174 +    public static void main(String... args) throws Exception {
   1.175 +        for (ReferenceKind rk : ReferenceKind.values()) {
   1.176 +            for (QualifierKind qk : QualifierKind.values()) {
   1.177 +                for (GenericKind gk : GenericKind.values()) {
   1.178 +                    for (SubExprKind sk : SubExprKind.values()) {
   1.179 +                        for (ExprKind ek : ExprKind.values()) {
   1.180 +                            for (ContextKind ck : ContextKind.values()) {
   1.181 +                                pool.execute(new MethodReferenceParserTest(rk, qk, gk, sk, ek, ck));
   1.182 +                            }
   1.183 +                        }
   1.184 +                    }
   1.185 +                }
   1.186 +            }
   1.187 +        }
   1.188 +
   1.189 +        checkAfterExec();
   1.190 +    }
   1.191 +
   1.192 +    ReferenceKind rk;
   1.193 +    QualifierKind qk;
   1.194 +    GenericKind gk;
   1.195 +    SubExprKind sk;
   1.196 +    ExprKind ek;
   1.197 +    ContextKind ck;
   1.198 +    JavaSource source;
   1.199 +    DiagnosticChecker diagChecker;
   1.200 +
   1.201 +    MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek, ContextKind ck) {
   1.202 +        this.rk = rk;
   1.203 +        this.qk = qk;
   1.204 +        this.gk = gk;
   1.205 +        this.sk = sk;
   1.206 +        this.ek = ek;
   1.207 +        this.ck = ck;
   1.208 +        this.source = new JavaSource();
   1.209 +        this.diagChecker = new DiagnosticChecker();
   1.210 +    }
   1.211 +
   1.212 +    class JavaSource extends SimpleJavaFileObject {
   1.213 +
   1.214 +        String template = "class Test {\n" +
   1.215 +                          "   void test() {\n" +
   1.216 +                          "      #C\n" +
   1.217 +                          "   }" +
   1.218 +                          "}";
   1.219 +
   1.220 +        String source;
   1.221 +
   1.222 +        public JavaSource() {
   1.223 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.224 +            source = template.replaceAll("#C", ck.contextString(ek, rk, qk, gk, sk));
   1.225 +        }
   1.226 +
   1.227 +        @Override
   1.228 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.229 +            return source;
   1.230 +        }
   1.231 +    }
   1.232 +
   1.233 +    @Override
   1.234 +    public void run() {
   1.235 +        JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
   1.236 +                null, null, Arrays.asList(source));
   1.237 +        try {
   1.238 +            ct.parse();
   1.239 +        } catch (Throwable ex) {
   1.240 +            processException(ex);
   1.241 +            return;
   1.242 +        }
   1.243 +        check();
   1.244 +    }
   1.245 +
   1.246 +    void check() {
   1.247 +        checkCount.incrementAndGet();
   1.248 +
   1.249 +        if (diagChecker.errorFound != rk.erroneous()) {
   1.250 +            throw new Error("invalid diagnostics for source:\n" +
   1.251 +                source.getCharContent(true) +
   1.252 +                "\nFound error: " + diagChecker.errorFound +
   1.253 +                "\nExpected error: " + rk.erroneous());
   1.254 +        }
   1.255 +    }
   1.256 +
   1.257 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.258 +
   1.259 +        boolean errorFound;
   1.260 +
   1.261 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.262 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.263 +                errorFound = true;
   1.264 +            }
   1.265 +        }
   1.266 +    }
   1.267 +
   1.268 +}

mercurial