test/tools/javac/lambda/MethodReferenceParserTest.java

changeset 1145
3343b22e2761
child 1150
e55270a7a022
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/MethodReferenceParserTest.java	Mon Nov 28 16:05:46 2011 +0000
     1.3 @@ -0,0 +1,233 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 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
    1.30 + * @summary Add parser support for method references
    1.31 + */
    1.32 +
    1.33 +import com.sun.source.util.JavacTask;
    1.34 +import java.net.URI;
    1.35 +import java.util.Arrays;
    1.36 +import javax.tools.Diagnostic;
    1.37 +import javax.tools.JavaCompiler;
    1.38 +import javax.tools.JavaFileObject;
    1.39 +import javax.tools.SimpleJavaFileObject;
    1.40 +import javax.tools.StandardJavaFileManager;
    1.41 +import javax.tools.ToolProvider;
    1.42 +
    1.43 +public class MethodReferenceParserTest {
    1.44 +
    1.45 +    static int checkCount = 0;
    1.46 +
    1.47 +    enum ReferenceKind {
    1.48 +        METHOD_REF("#Q##Gm"),
    1.49 +        CONSTRUCTOR_REF("#Q##Gnew"),
    1.50 +        ERR_SUPER("#Q##Gsuper"),
    1.51 +        ERR_METH0("#Q##Gm()"),
    1.52 +        ERR_METH1("#Q##Gm(X)"),
    1.53 +        ERR_CONSTR0("#Q##Gnew()"),
    1.54 +        ERR_CONSTR1("#Q##Gnew(X)");
    1.55 +
    1.56 +        String referenceTemplate;
    1.57 +
    1.58 +        ReferenceKind(String referenceTemplate) {
    1.59 +            this.referenceTemplate = referenceTemplate;
    1.60 +        }
    1.61 +
    1.62 +        String getReferenceString(QualifierKind qk, GenericKind gk) {
    1.63 +            return referenceTemplate
    1.64 +                    .replaceAll("#Q", qk.qualifier)
    1.65 +                    .replaceAll("#G", gk.typeParameters);
    1.66 +        }
    1.67 +
    1.68 +        boolean erroneous() {
    1.69 +            switch (this) {
    1.70 +                case ERR_SUPER:
    1.71 +                case ERR_METH0:
    1.72 +                case ERR_METH1:
    1.73 +                case ERR_CONSTR0:
    1.74 +                case ERR_CONSTR1:
    1.75 +                    return true;
    1.76 +                default: return false;
    1.77 +            }
    1.78 +        }
    1.79 +    }
    1.80 +
    1.81 +    enum GenericKind {
    1.82 +        NONE(""),
    1.83 +        ONE("<X>"),
    1.84 +        TWO("<X,Y>");
    1.85 +
    1.86 +        String typeParameters;
    1.87 +
    1.88 +        GenericKind(String typeParameters) {
    1.89 +            this.typeParameters = typeParameters;
    1.90 +        }
    1.91 +    }
    1.92 +
    1.93 +    enum QualifierKind {
    1.94 +        THIS("this"),
    1.95 +        SUPER("super"),
    1.96 +        NEW("new Foo()"),
    1.97 +        METHOD("m()"),
    1.98 +        FIELD("a.f"),
    1.99 +        UBOUND_SIMPLE("A"),
   1.100 +        UNBOUND_GENERIC1("A<X>"),
   1.101 +        UNBOUND_GENERIC2("A<X, Y>"),
   1.102 +        UNBOUND_GENERIC3("A<? extends X, ? super Y>");
   1.103 +
   1.104 +        String qualifier;
   1.105 +
   1.106 +        QualifierKind(String qualifier) {
   1.107 +            this.qualifier = qualifier;
   1.108 +        }
   1.109 +    }
   1.110 +
   1.111 +    enum ExprKind {
   1.112 +        NONE("#R#S"),
   1.113 +        SINGLE_PAREN1("(#R#S)"),
   1.114 +        SINGLE_PAREN2("(#R)#S"),
   1.115 +        DOUBLE_PAREN1("((#R#S))"),
   1.116 +        DOUBLE_PAREN2("((#R)#S)"),
   1.117 +        DOUBLE_PAREN3("((#R))#S");
   1.118 +
   1.119 +        String expressionTemplate;
   1.120 +
   1.121 +        ExprKind(String expressionTemplate) {
   1.122 +            this.expressionTemplate = expressionTemplate;
   1.123 +        }
   1.124 +
   1.125 +        String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
   1.126 +            return expressionTemplate
   1.127 +                    .replaceAll("#R", rk.getReferenceString(qk, gk))
   1.128 +                    .replaceAll("#S", sk.subExpression);
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +    enum SubExprKind {
   1.133 +        NONE(""),
   1.134 +        SELECT_FIELD(".f"),
   1.135 +        SELECT_METHOD(".f()"),
   1.136 +        SELECT_NEW(".new Foo()"),
   1.137 +        POSTINC("++"),
   1.138 +        POSTDEC("--");
   1.139 +
   1.140 +        String subExpression;
   1.141 +
   1.142 +        SubExprKind(String subExpression) {
   1.143 +            this.subExpression = subExpression;
   1.144 +        }
   1.145 +    }
   1.146 +
   1.147 +    public static void main(String... args) throws Exception {
   1.148 +
   1.149 +        //create default shared JavaCompiler - reused across multiple compilations
   1.150 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   1.151 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   1.152 +
   1.153 +        for (ReferenceKind rk : ReferenceKind.values()) {
   1.154 +            for (QualifierKind qk : QualifierKind.values()) {
   1.155 +                for (GenericKind gk : GenericKind.values()) {
   1.156 +                    for (SubExprKind sk : SubExprKind.values()) {
   1.157 +                        for (ExprKind ek : ExprKind.values()) {
   1.158 +                            new MethodReferenceParserTest(rk, qk, gk, sk, ek).run(comp, fm);
   1.159 +                        }
   1.160 +                    }
   1.161 +                }
   1.162 +            }
   1.163 +        }
   1.164 +        System.out.println("Total check executed: " + checkCount);
   1.165 +    }
   1.166 +
   1.167 +    ReferenceKind rk;
   1.168 +    QualifierKind qk;
   1.169 +    GenericKind gk;
   1.170 +    SubExprKind sk;
   1.171 +    ExprKind ek;
   1.172 +    JavaSource source;
   1.173 +    DiagnosticChecker diagChecker;
   1.174 +
   1.175 +    MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek) {
   1.176 +        this.rk = rk;
   1.177 +        this.qk = qk;
   1.178 +        this.gk = gk;
   1.179 +        this.sk = sk;
   1.180 +        this.ek = ek;
   1.181 +        this.source = new JavaSource();
   1.182 +        this.diagChecker = new DiagnosticChecker();
   1.183 +    }
   1.184 +
   1.185 +    class JavaSource extends SimpleJavaFileObject {
   1.186 +
   1.187 +        String template = "class Test {\n" +
   1.188 +                          "   SAM s = #E;\n" +
   1.189 +                          "}";
   1.190 +
   1.191 +        String source;
   1.192 +
   1.193 +        public JavaSource() {
   1.194 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.195 +            source = template.replaceAll("#E", ek.expressionString(rk, qk, gk, sk));
   1.196 +        }
   1.197 +
   1.198 +        @Override
   1.199 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.200 +            return source;
   1.201 +        }
   1.202 +    }
   1.203 +
   1.204 +    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   1.205 +        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   1.206 +                Arrays.asList("-XDallowMethodReferences"), null, Arrays.asList(source));
   1.207 +        try {
   1.208 +            ct.parse();
   1.209 +        } catch (Throwable ex) {
   1.210 +            throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
   1.211 +        }
   1.212 +        check();
   1.213 +    }
   1.214 +
   1.215 +    void check() {
   1.216 +        checkCount++;
   1.217 +
   1.218 +        if (diagChecker.errorFound != rk.erroneous()) {
   1.219 +            throw new Error("invalid diagnostics for source:\n" +
   1.220 +                source.getCharContent(true) +
   1.221 +                "\nFound error: " + diagChecker.errorFound +
   1.222 +                "\nExpected error: " + rk.erroneous());
   1.223 +        }
   1.224 +    }
   1.225 +
   1.226 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.227 +
   1.228 +        boolean errorFound;
   1.229 +
   1.230 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.231 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.232 +                errorFound = true;
   1.233 +            }
   1.234 +        }
   1.235 +    }
   1.236 +}

mercurial