test/tools/javac/lambda/MethodReferenceParserTest.java

Sat, 06 Oct 2012 10:35:38 +0100

author
mcimadamore
date
Sat, 06 Oct 2012 10:35:38 +0100
changeset 1352
d4b3cb1ece84
parent 1165
1ae5988e201b
child 1415
01c9d4161882
permissions
-rw-r--r--

7177386: Add attribution support for method references
Summary: Add type-checking/lookup routines for method references
Reviewed-by: jjg, dlsmith

     1 /*
     2  * Copyright (c) 2011, 2012, 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_ARRAY1("int[]"),
   114         UNBOUND_ARRAY2("A<G>[][]"),
   115         UNBOUND_GENERIC1("A<X>"),
   116         UNBOUND_GENERIC2("A<X, Y>"),
   117         UNBOUND_GENERIC3("A<? extends X, ? super Y>"),
   118         UNBOUND_GENERIC4("A<int[], short[][]>"),
   119         NESTED_GENERIC1("A<A<X,Y>, A<X,Y>>"),
   120         NESTED_GENERIC2("A<A<A<X,Y>,A<X,Y>>, A<A<X,Y>,A<X,Y>>>");
   122         String qualifier;
   124         QualifierKind(String qualifier) {
   125             this.qualifier = qualifier;
   126         }
   127     }
   129     enum ExprKind {
   130         NONE("#R::S"),
   131         SINGLE_PAREN1("(#R#S)"),
   132         SINGLE_PAREN2("(#R)#S"),
   133         DOUBLE_PAREN1("((#R#S))"),
   134         DOUBLE_PAREN2("((#R)#S)"),
   135         DOUBLE_PAREN3("((#R))#S");
   137         String expressionTemplate;
   139         ExprKind(String expressionTemplate) {
   140             this.expressionTemplate = expressionTemplate;
   141         }
   143         String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
   144             return expressionTemplate
   145                     .replaceAll("#R", rk.getReferenceString(qk, gk))
   146                     .replaceAll("#S", sk.subExpression);
   147         }
   148     }
   150     enum SubExprKind {
   151         NONE(""),
   152         SELECT_FIELD(".f"),
   153         SELECT_METHOD(".f()"),
   154         SELECT_NEW(".new Foo()"),
   155         POSTINC("++"),
   156         POSTDEC("--");
   158         String subExpression;
   160         SubExprKind(String subExpression) {
   161             this.subExpression = subExpression;
   162         }
   163     }
   165     public static void main(String... args) throws Exception {
   167         //create default shared JavaCompiler - reused across multiple compilations
   168         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   169         StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   171         for (ReferenceKind rk : ReferenceKind.values()) {
   172             for (QualifierKind qk : QualifierKind.values()) {
   173                 for (GenericKind gk : GenericKind.values()) {
   174                     for (SubExprKind sk : SubExprKind.values()) {
   175                         for (ExprKind ek : ExprKind.values()) {
   176                             for (ContextKind ck : ContextKind.values()) {
   177                                 new MethodReferenceParserTest(rk, qk, gk, sk, ek, ck).run(comp, fm);
   178                             }
   179                         }
   180                     }
   181                 }
   182             }
   183         }
   184         System.out.println("Total check executed: " + checkCount);
   185     }
   187     ReferenceKind rk;
   188     QualifierKind qk;
   189     GenericKind gk;
   190     SubExprKind sk;
   191     ExprKind ek;
   192     ContextKind ck;
   193     JavaSource source;
   194     DiagnosticChecker diagChecker;
   196     MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek, ContextKind ck) {
   197         this.rk = rk;
   198         this.qk = qk;
   199         this.gk = gk;
   200         this.sk = sk;
   201         this.ek = ek;
   202         this.ck = ck;
   203         this.source = new JavaSource();
   204         this.diagChecker = new DiagnosticChecker();
   205     }
   207     class JavaSource extends SimpleJavaFileObject {
   209         String template = "class Test {\n" +
   210                           "   void test() {\n" +
   211                           "      #C\n" +
   212                           "   }" +
   213                           "}";
   215         String source;
   217         public JavaSource() {
   218             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   219             source = template.replaceAll("#C", ck.contextString(ek, rk, qk, gk, sk));
   220         }
   222         @Override
   223         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   224             return source;
   225         }
   226     }
   228     void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   229         JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   230                 Arrays.asList("-XDallowMethodReferences"), null, Arrays.asList(source));
   231         try {
   232             ct.parse();
   233         } catch (Throwable ex) {
   234             throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
   235         }
   236         check();
   237     }
   239     void check() {
   240         checkCount++;
   242         if (diagChecker.errorFound != rk.erroneous()) {
   243             throw new Error("invalid diagnostics for source:\n" +
   244                 source.getCharContent(true) +
   245                 "\nFound error: " + diagChecker.errorFound +
   246                 "\nExpected error: " + rk.erroneous());
   247         }
   248     }
   250     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   252         boolean errorFound;
   254         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   255             if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   256                 errorFound = true;
   257             }
   258         }
   259     }
   260 }

mercurial