langtools/test/tools/javac/lambda/MethodReferenceParserTest.java
author mcimadamore
Sun, 11 Dec 2011 17:48:25 +0000
changeset 11149 ee702a4f9c5f
parent 11144 8a4ae514eedf
child 11322 6ee85f80967a
permissions -rw-r--r--
7120266: javac fails to compile hotspot code Summary: Parser changes for method references cause bad intercation with method call syntax Reviewed-by: jjg

/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 7115052
 * @ignore 7120266
 * @summary Add parser support for method references
 */

import com.sun.source.util.JavacTask;
import java.net.URI;
import java.util.Arrays;
import javax.tools.Diagnostic;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class MethodReferenceParserTest {

    static int checkCount = 0;

    enum ReferenceKind {
        METHOD_REF("#Q##Gm"),
        CONSTRUCTOR_REF("#Q##Gnew"),
        ERR_SUPER("#Q##Gsuper"),
        ERR_METH0("#Q##Gm()"),
        ERR_METH1("#Q##Gm(X)"),
        ERR_CONSTR0("#Q##Gnew()"),
        ERR_CONSTR1("#Q##Gnew(X)");

        String referenceTemplate;

        ReferenceKind(String referenceTemplate) {
            this.referenceTemplate = referenceTemplate;
        }

        String getReferenceString(QualifierKind qk, GenericKind gk) {
            return referenceTemplate
                    .replaceAll("#Q", qk.qualifier)
                    .replaceAll("#G", gk.typeParameters);
        }

        boolean erroneous() {
            switch (this) {
                case ERR_SUPER:
                case ERR_METH0:
                case ERR_METH1:
                case ERR_CONSTR0:
                case ERR_CONSTR1:
                    return true;
                default: return false;
            }
        }
    }

    enum GenericKind {
        NONE(""),
        ONE("<X>"),
        TWO("<X,Y>");

        String typeParameters;

        GenericKind(String typeParameters) {
            this.typeParameters = typeParameters;
        }
    }

    enum QualifierKind {
        THIS("this"),
        SUPER("super"),
        NEW("new Foo()"),
        METHOD("m()"),
        FIELD("a.f"),
        UBOUND_SIMPLE("A"),
        UNBOUND_GENERIC1("A<X>"),
        UNBOUND_GENERIC2("A<X, Y>"),
        UNBOUND_GENERIC3("A<? extends X, ? super Y>");

        String qualifier;

        QualifierKind(String qualifier) {
            this.qualifier = qualifier;
        }
    }

    enum ExprKind {
        NONE("#R#S"),
        SINGLE_PAREN1("(#R#S)"),
        SINGLE_PAREN2("(#R)#S"),
        DOUBLE_PAREN1("((#R#S))"),
        DOUBLE_PAREN2("((#R)#S)"),
        DOUBLE_PAREN3("((#R))#S");

        String expressionTemplate;

        ExprKind(String expressionTemplate) {
            this.expressionTemplate = expressionTemplate;
        }

        String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
            return expressionTemplate
                    .replaceAll("#R", rk.getReferenceString(qk, gk))
                    .replaceAll("#S", sk.subExpression);
        }
    }

    enum SubExprKind {
        NONE(""),
        SELECT_FIELD(".f"),
        SELECT_METHOD(".f()"),
        SELECT_NEW(".new Foo()"),
        POSTINC("++"),
        POSTDEC("--");

        String subExpression;

        SubExprKind(String subExpression) {
            this.subExpression = subExpression;
        }
    }

    public static void main(String... args) throws Exception {

        //create default shared JavaCompiler - reused across multiple compilations
        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);

        for (ReferenceKind rk : ReferenceKind.values()) {
            for (QualifierKind qk : QualifierKind.values()) {
                for (GenericKind gk : GenericKind.values()) {
                    for (SubExprKind sk : SubExprKind.values()) {
                        for (ExprKind ek : ExprKind.values()) {
                            new MethodReferenceParserTest(rk, qk, gk, sk, ek).run(comp, fm);
                        }
                    }
                }
            }
        }
        System.out.println("Total check executed: " + checkCount);
    }

    ReferenceKind rk;
    QualifierKind qk;
    GenericKind gk;
    SubExprKind sk;
    ExprKind ek;
    JavaSource source;
    DiagnosticChecker diagChecker;

    MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek) {
        this.rk = rk;
        this.qk = qk;
        this.gk = gk;
        this.sk = sk;
        this.ek = ek;
        this.source = new JavaSource();
        this.diagChecker = new DiagnosticChecker();
    }

    class JavaSource extends SimpleJavaFileObject {

        String template = "class Test {\n" +
                          "   SAM s = #E;\n" +
                          "}";

        String source;

        public JavaSource() {
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
            source = template.replaceAll("#E", ek.expressionString(rk, qk, gk, sk));
        }

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return source;
        }
    }

    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
                Arrays.asList("-XDallowMethodReferences"), null, Arrays.asList(source));
        try {
            ct.parse();
        } catch (Throwable ex) {
            throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
        }
        check();
    }

    void check() {
        checkCount++;

        if (diagChecker.errorFound != rk.erroneous()) {
            throw new Error("invalid diagnostics for source:\n" +
                source.getCharContent(true) +
                "\nFound error: " + diagChecker.errorFound +
                "\nExpected error: " + rk.erroneous());
        }
    }

    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {

        boolean errorFound;

        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
                errorFound = true;
            }
        }
    }
}