test/tools/javac/lambda/methodReference/SamConversionComboTest.java

Mon, 21 Jan 2013 20:13:56 +0000

author
mcimadamore
date
Mon, 21 Jan 2013 20:13:56 +0000
changeset 1510
7873d37f5b37
parent 1415
01c9d4161882
child 2227
998b10c43157
permissions
-rw-r--r--

8005244: Implement overload resolution as per latest spec EDR
Summary: Add support for stuck expressions and provisional applicability
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 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 8003280
    27  * @summary Add lambda tests
    28  *   Test SAM conversion of method references in combinations of different contexts,
    29  *           lambda body types(statement/expression), boxing/unboxing etc, to verify
    30  *           SAM conversion being conducted successfully as expected.
    31  */
    33 import com.sun.source.util.JavacTask;
    34 import java.net.URI;
    35 import java.util.Arrays;
    36 import javax.tools.Diagnostic;
    37 import javax.tools.JavaCompiler;
    38 import javax.tools.JavaFileObject;
    39 import javax.tools.SimpleJavaFileObject;
    40 import javax.tools.ToolProvider;
    41 import javax.tools.StandardJavaFileManager;
    43 public class SamConversionComboTest {
    45     enum FInterface {
    46         A("A", "interface A { Integer m(int i); }"),
    47         B("B", "interface B { int m(Integer i); }"),
    48         C("C", "interface C { int m(int i) throws Exception; }");
    50         String interfaceType;
    51         String interfaceDef;
    53         FInterface(String interfaceType, String interfaceDef) {
    54             this.interfaceType = interfaceType;
    55             this.interfaceDef = interfaceDef;
    56         }
    57     }
    59     enum Context {
    60         ASSIGNMENT("#FType f = #MR;"),
    61         METHOD_CALL("void method1(#FType f) { }\n" +
    62                     "void method2() {\n" +
    63                     "    method1(#MR);\n" +
    64                     "}"),
    65         CONSTRUCTOR("X x = new X(#MR);"),
    66         RETURN_OF_METHOD("#FType method1() {\n" +
    67                          "    return #MR;\n" +
    68                          "}"),
    69         ARRAY_INITIALIZER("#FType[] oarray = {#MR};"),
    70         LAMBDA_BODY("#FType f = n -> ((#FType)#MR).m(n);"),
    71         CAST("void test() throws Exception { int n = ((#FType)#MR).m(1); }"),
    72         CONDITIONAL_EXPRESSION("#FType f = 2 > 1 ? #MR : null;");
    74         String context;
    76         Context(String context) {
    77             this.context = context;
    78         }
    80         String getContext(FInterface f, MethodReference mr) {
    81             return context.replace("#FType", f.interfaceType).replace("#MR", mr.mrValue);
    82         }
    83     }
    85     enum MethodReference {
    86         METHOD1("X::method1"),
    87         METHOD2("new X()::method2"),
    88         METHOD3("X::method3"),
    89         METHOD4("new X()::method4"),
    90         METHOD5("new X()::method5"),
    91         METHOD6("X::method6"),
    92         METHOD7("X::method7"),
    93         METHOD8("X::method8");
    95         String mrValue;
    97         MethodReference(String mr) {
    98             mrValue = mr;
    99         }
   100     }
   102     enum MethodDef {
   103         METHOD1("    static Integer method1(int n) {\n" +
   104                 "        return n + 1;\n" +
   105                 "    }\n", 0),
   106         METHOD2("    int method2(Integer n) {\n" +
   107                 "        return value == 0 ? n + 2 : n + value;\n" +
   108                 "    }\n", 1),
   109         METHOD3("    static int method3(int n) {\n" +
   110                 "        return n + 3;\n" +
   111                 "    }\n", 2),
   112         METHOD4("    Integer method4(Integer n) {\n" +
   113                 "        return value == 0 ? n + 4 : n + value;\n" +
   114                 "    }\n", 3),
   115         METHOD5("    Integer method5(Integer n) {\n" +
   116                 "        return value == 0 ? new Integer(n + 5) : new Integer(n + value);\n" +
   117                 "    }\n", 4),
   118         METHOD6("    static int method6(Integer n) throws Exception{\n" +
   119                 "        throw new Exception();\n" +
   120                 "    }\n", 5),
   121         METHOD7("    static int method7(String s){\n" +
   122                 "        return s.length();\n" +
   123                 "    }\n", 6),
   124         METHOD8("    static String method8(Integer n){\n" +
   125                 "        return n + \"\";\n" +
   126                 "    }\n", 7);
   128         String methodStr;
   129         int index;
   131         MethodDef(String ms, int i) {
   132             methodStr = ms;
   133             index = i;
   134         }
   136         MethodReference getMethodReference() {
   137             return MethodReference.values()[index];
   138         }
   139     }
   141     SourceFile samSourceFile = new SourceFile("FInterface.java", "#C") {
   142         public String toString() {
   143             String interfaces = "";
   144             for(FInterface fi : FInterface.values())
   145                 interfaces += fi.interfaceDef + "\n";
   146             return template.replace("#C", interfaces);
   147         }
   148     };
   150     String clientTemplate = "class Client {\n" +
   151                             "    #Context\n" +
   152                             "}\n\n" +
   154                             "class X {\n" +
   155                             "    int value = 0;\n\n" +
   157                             "    X() {\n" +
   158                             "    }\n\n" +
   160                             "    X(A a) {\n" +
   161                             "        value = a.m(9);\n" +
   162                             "    }\n\n" +
   164                             "    X(B b) {\n" +
   165                             "        value = b.m(9);\n" +
   166                             "    }\n\n" +
   168                             "    X(C c) {\n" +
   169                             "        try {\n" +
   170                             "            value = c.m(9);\n" +
   171                             "        } catch (Exception e){}\n" +
   172                             "    }\n\n" +
   174                             "#MethodDef" +
   175                             "}";
   177     SourceFile clientSourceFile = new SourceFile("Client.java", clientTemplate) {
   178         public String toString() {
   179             return template.replace("#Context", context.getContext(fInterface, methodReference)).replace("#MethodDef", methodDef.methodStr);
   180         }
   181     };
   183     boolean checkSamConversion() {
   184         if(methodDef == MethodDef.METHOD7 || methodDef == MethodDef.METHOD8)//method signature mismatch
   185             return false;
   186         if(context != Context.CONSTRUCTOR && fInterface != FInterface.C && methodDef == MethodDef.METHOD6)
   187         //method that throws exceptions not thrown by the interface method is a mismatch
   188             return false;
   189         if(context == Context.CONSTRUCTOR)
   190                return false;
   191         return true;
   192     }
   194     void test() throws Exception {
   195         System.out.println("\n====================================");
   196         System.out.println(fInterface + ", " +  context + ", " + methodReference);
   197         System.out.println(samSourceFile + "\n" + clientSourceFile);
   199         DiagnosticChecker dc = new DiagnosticChecker();
   200         JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
   201         ct.analyze();
   202         if (dc.errorFound == checkSamConversion()) {
   203             throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile);
   204         }
   205         count++;
   206     }
   208     abstract class SourceFile extends SimpleJavaFileObject {
   210         protected String template;
   212         public SourceFile(String filename, String template) {
   213             super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
   214             this.template = template;
   215         }
   217         @Override
   218         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   219             return toString();
   220         }
   222         public abstract String toString();
   223     }
   225     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   227         boolean errorFound = false;
   229         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   230             if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   231                 errorFound = true;
   232             }
   233         }
   234     }
   236     FInterface fInterface;
   237     Context context;
   238     MethodDef methodDef;
   239     MethodReference methodReference;
   240     static int count = 0;
   242     static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   243     static StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   245     SamConversionComboTest(FInterface f, Context c, MethodDef md) {
   246         fInterface = f;
   247         context = c;
   248         methodDef = md;
   249         methodReference = md.getMethodReference();
   250     }
   252     public static void main(String[] args) throws Exception {
   253         for(Context ct : Context.values()) {
   254             for (FInterface fi : FInterface.values()) {
   255                 for (MethodDef md: MethodDef.values()) {
   256                     new SamConversionComboTest(fi, ct, md).test();
   257                 }
   258             }
   259         }
   260         System.out.println("total tests: " + count);
   261     }
   262 }

mercurial