test/tools/javac/lambda/MethodReferenceParserTest.java

Mon, 19 Dec 2011 12:07:07 +0000

author
mcimadamore
date
Mon, 19 Dec 2011 12:07:07 +0000
changeset 1165
1ae5988e201b
parent 1150
e55270a7a022
child 1352
d4b3cb1ece84
permissions
-rw-r--r--

7120463: Fix method reference parser support in order to avoid ambiguities
Summary: Add lookahead routine to disambiguate between method reference in method context and binary expression
Reviewed-by: jjg, dlsmith

     1 /*
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 7115052
    27  * @summary Add parser support for method references
    28  */
    30 import com.sun.source.util.JavacTask;
    31 import java.net.URI;
    32 import java.util.Arrays;
    33 import javax.tools.Diagnostic;
    34 import javax.tools.JavaCompiler;
    35 import javax.tools.JavaFileObject;
    36 import javax.tools.SimpleJavaFileObject;
    37 import javax.tools.StandardJavaFileManager;
    38 import javax.tools.ToolProvider;
    40 public class MethodReferenceParserTest {
    42     static int checkCount = 0;
    44     enum ReferenceKind {
    45         METHOD_REF("#Q##Gm"),
    46         CONSTRUCTOR_REF("#Q##Gnew"),
    47         FALSE_REF("min < max"),
    48         ERR_SUPER("#Q##Gsuper"),
    49         ERR_METH0("#Q##Gm()"),
    50         ERR_METH1("#Q##Gm(X)"),
    51         ERR_CONSTR0("#Q##Gnew()"),
    52         ERR_CONSTR1("#Q##Gnew(X)");
    54         String referenceTemplate;
    56         ReferenceKind(String referenceTemplate) {
    57             this.referenceTemplate = referenceTemplate;
    58         }
    60         String getReferenceString(QualifierKind qk, GenericKind gk) {
    61             return referenceTemplate
    62                     .replaceAll("#Q", qk.qualifier)
    63                     .replaceAll("#G", gk.typeParameters);
    64         }
    66         boolean erroneous() {
    67             switch (this) {
    68                 case ERR_SUPER:
    69                 case ERR_METH0:
    70                 case ERR_METH1:
    71                 case ERR_CONSTR0:
    72                 case ERR_CONSTR1:
    73                     return true;
    74                 default: return false;
    75             }
    76         }
    77     }
    79     enum ContextKind {
    80         ASSIGN("SAM s = #E;"),
    81         METHOD("m(#E, i);");
    83         String contextTemplate;
    85         ContextKind(String contextTemplate) {
    86             this.contextTemplate = contextTemplate;
    87         }
    89         String contextString(ExprKind ek, ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
    90             return contextTemplate.replaceAll("#E", ek.expressionString(rk, qk, gk, sk));
    91         }
    92     }
    94     enum GenericKind {
    95         NONE(""),
    96         ONE("<X>"),
    97         TWO("<X,Y>");
    99         String typeParameters;
   101         GenericKind(String typeParameters) {
   102             this.typeParameters = typeParameters;
   103         }
   104     }
   106     enum QualifierKind {
   107         THIS("this"),
   108         SUPER("super"),
   109         NEW("new Foo()"),
   110         METHOD("m()"),
   111         FIELD("a.f"),
   112         UBOUND_SIMPLE("A"),
   113         UNBOUND_GENERIC1("A<X>"),
   114         UNBOUND_GENERIC2("A<X, Y>"),
   115         UNBOUND_GENERIC3("A<? extends X, ? super Y>"),
   116         UNBOUND_GENERIC4("A<int[], short[][]>"),
   117         NESTED_GENERIC1("A<A<X,Y>, A<X,Y>>"),
   118         NESTED_GENERIC2("A<A<A<X,Y>,A<X,Y>>, A<A<X,Y>,A<X,Y>>>");
   120         String qualifier;
   122         QualifierKind(String qualifier) {
   123             this.qualifier = qualifier;
   124         }
   125     }
   127     enum ExprKind {
   128         NONE("#R#S"),
   129         SINGLE_PAREN1("(#R#S)"),
   130         SINGLE_PAREN2("(#R)#S"),
   131         DOUBLE_PAREN1("((#R#S))"),
   132         DOUBLE_PAREN2("((#R)#S)"),
   133         DOUBLE_PAREN3("((#R))#S");
   135         String expressionTemplate;
   137         ExprKind(String expressionTemplate) {
   138             this.expressionTemplate = expressionTemplate;
   139         }
   141         String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
   142             return expressionTemplate
   143                     .replaceAll("#R", rk.getReferenceString(qk, gk))
   144                     .replaceAll("#S", sk.subExpression);
   145         }
   146     }
   148     enum SubExprKind {
   149         NONE(""),
   150         SELECT_FIELD(".f"),
   151         SELECT_METHOD(".f()"),
   152         SELECT_NEW(".new Foo()"),
   153         POSTINC("++"),
   154         POSTDEC("--");
   156         String subExpression;
   158         SubExprKind(String subExpression) {
   159             this.subExpression = subExpression;
   160         }
   161     }
   163     public static void main(String... args) throws Exception {
   165         //create default shared JavaCompiler - reused across multiple compilations
   166         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   167         StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   169         for (ReferenceKind rk : ReferenceKind.values()) {
   170             for (QualifierKind qk : QualifierKind.values()) {
   171                 for (GenericKind gk : GenericKind.values()) {
   172                     for (SubExprKind sk : SubExprKind.values()) {
   173                         for (ExprKind ek : ExprKind.values()) {
   174                             for (ContextKind ck : ContextKind.values()) {
   175                                 new MethodReferenceParserTest(rk, qk, gk, sk, ek, ck).run(comp, fm);
   176                             }
   177                         }
   178                     }
   179                 }
   180             }
   181         }
   182         System.out.println("Total check executed: " + checkCount);
   183     }
   185     ReferenceKind rk;
   186     QualifierKind qk;
   187     GenericKind gk;
   188     SubExprKind sk;
   189     ExprKind ek;
   190     ContextKind ck;
   191     JavaSource source;
   192     DiagnosticChecker diagChecker;
   194     MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek, ContextKind ck) {
   195         this.rk = rk;
   196         this.qk = qk;
   197         this.gk = gk;
   198         this.sk = sk;
   199         this.ek = ek;
   200         this.ck = ck;
   201         this.source = new JavaSource();
   202         this.diagChecker = new DiagnosticChecker();
   203     }
   205     class JavaSource extends SimpleJavaFileObject {
   207         String template = "class Test {\n" +
   208                           "   void test() {\n" +
   209                           "      #C\n" +
   210                           "   }" +
   211                           "}";
   213         String source;
   215         public JavaSource() {
   216             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   217             source = template.replaceAll("#C", ck.contextString(ek, rk, qk, gk, sk));
   218         }
   220         @Override
   221         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   222             return source;
   223         }
   224     }
   226     void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   227         JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   228                 Arrays.asList("-XDallowMethodReferences"), null, Arrays.asList(source));
   229         try {
   230             ct.parse();
   231         } catch (Throwable ex) {
   232             throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
   233         }
   234         check();
   235     }
   237     void check() {
   238         checkCount++;
   240         if (diagChecker.errorFound != rk.erroneous()) {
   241             throw new Error("invalid diagnostics for source:\n" +
   242                 source.getCharContent(true) +
   243                 "\nFound error: " + diagChecker.errorFound +
   244                 "\nExpected error: " + rk.erroneous());
   245         }
   246     }
   248     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   250         boolean errorFound;
   252         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   253             if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   254                 errorFound = true;
   255             }
   256         }
   257     }
   258 }

mercurial