langtools/test/tools/javac/lambda/lambdaExpression/SamConversionComboTest.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 27388 d694da45bd7a
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27388
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     4
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     8
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    14
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    18
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    21
 * questions.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    22
 */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    23
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    24
/**
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    25
 * @test
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    26
 * @bug 8003280
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    27
 * @summary Add lambda tests
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    28
 *   Test SAM conversion of lambda expressions in combinations of different contexts,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    29
 *           lambda body types(statement/expression), explict/implicit target type etc, to verify
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    30
 *           SAM conversion being conducted successfully as expected.
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27388
diff changeset
    31
 * @modules jdk.compiler
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    32
 */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    33
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    34
import com.sun.source.util.JavacTask;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    35
import java.net.URI;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    36
import java.util.Arrays;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    37
import javax.tools.Diagnostic;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    38
import javax.tools.JavaCompiler;
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
    39
import javax.tools.JavaFileManager;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    40
import javax.tools.JavaFileObject;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    41
import javax.tools.SimpleJavaFileObject;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    42
import javax.tools.ToolProvider;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    43
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    44
public class SamConversionComboTest {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    45
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    46
    enum FInterface {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    47
        A("A", "interface A { Integer m(int i); }"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    48
        B("B", "interface B { int m(Integer i); }"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    49
        C("C", "interface C { int m(Integer i) throws Exception; }");
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    50
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    51
        String interfaceType;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    52
        String interfaceDef;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    53
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    54
        FInterface(String interfaceType, String interfaceDef) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    55
            this.interfaceType = interfaceType;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    56
            this.interfaceDef = interfaceDef;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    57
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    58
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    59
        String getParameterType() {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    60
            switch(this) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    61
            case A:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    62
                return "int";
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    63
            case B:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    64
            case C:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    65
                return "Integer";
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    66
            default:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    67
                return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    68
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    69
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    70
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    71
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    72
    enum Context {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    73
        ASSIGNMENT("#FType f = #LBody;"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    74
        METHOD_CALL("void method1(#FType f) { }\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    75
                    "    void method2() {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    76
                    "        method1(#LBody);\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    77
                    "    }"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    78
        CONSTRUCTOR("X x = new X(#LBody);"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    79
        RETURN_OF_METHOD("#FType method1() {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    80
                         "    return #LBody;\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    81
                         "}"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    82
        ARRAY_INITIALIZER("Object[] oarray = {\"a\", 1, (#FType)#LBody};"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    83
        LAMBDA_BODY("#FType f = n -> ((#FType)#LBody).m(n);"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    84
        CAST("void test() throws Exception { int n = ((#FType)#LBody).m(1); }"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    85
        CONDITIONAL_EXPRESSION("#FType f = 2 > 1 ? #LBody : null;");
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    86
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    87
        String context;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    88
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    89
        Context(String context) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    90
            this.context = context;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    91
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    92
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    93
        String getContext(FInterface f, LambdaKind lk, LambdaBody lb, ReturnValue rv) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    94
            return context.replace("#FType", f.interfaceType).replace("#LBody", lb.getLambdaBody(f, lk, rv));
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    95
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    96
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    97
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    98
    enum LambdaKind {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    99
        EXPRESSION("#VAL"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   100
        STATEMENT("{return #VAL;}"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   101
        EXCEPTION_STMT("{throw new Exception();}");
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   102
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   103
        String stmt;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   104
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   105
        LambdaKind(String stmt) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   106
            this.stmt = stmt;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   107
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   108
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   109
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   110
    enum ReturnValue {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   111
        INT("i + 1"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   112
        INTEGER("new Integer(i+1)"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   113
        INT2("i.intValue() + 1"),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   114
        STRING("i + \"\""),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   115
        DOUBLE("i * 1.0");
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   116
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   117
        String rValue;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   118
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   119
        ReturnValue(String rValue) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   120
            this.rValue = rValue;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   121
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   122
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   123
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   124
    enum LambdaBody {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   125
        IMPLICIT("i -> #RET"),//type inferred
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   126
        EXPLICIT("(#Type i) -> #RET");//explicit type
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   127
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   128
        String bodyStr;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   129
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   130
        LambdaBody(String bodyStr) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   131
            this.bodyStr = bodyStr;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   132
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   133
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   134
        String getLambdaBody(FInterface fi, LambdaKind lk, ReturnValue rv) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   135
            return bodyStr.replace("#Type", fi.getParameterType()).replace("#RET", lk.stmt.replace("#VAL", rv.rValue));
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   136
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   137
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   138
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   139
    boolean checkSamConversion() {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   140
        if(lambdaKind != LambdaKind.EXCEPTION_STMT && (returnValue == ReturnValue.DOUBLE || returnValue == ReturnValue.STRING)) //return type mismatch
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   141
            return false;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   142
        if(context != Context.CONSTRUCTOR) {//context other than construcotr argument
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   143
            if(fInterface != FInterface.C && lambdaKind == LambdaKind.EXCEPTION_STMT)
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   144
                return false;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   145
            if(fInterface == FInterface.A && returnValue == ReturnValue.INT2)
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   146
                return false;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   147
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   148
        else { //constructor argument context
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   149
            //match X(A a) or X(B b) or X(C c)
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   150
            if (lambdaKind == LambdaKind.EXCEPTION_STMT) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   151
                return false; //ambiguous target type
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   152
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   153
            else if(lambdaBody == LambdaBody.IMPLICIT) {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14547
diff changeset
   154
                return false;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   155
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   156
            else { //explicit parameter type
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   157
                if(fInterface.getParameterType().equals("Integer")) //ambiguous target type
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   158
                //e.g. X x = new X((Integer i) -> i + 1);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   159
                    return false;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   160
                if(returnValue == ReturnValue.INT2)
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   161
                //e.g. X x = new X(int i -> i.intValue() + 1);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   162
                    return false;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   163
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   164
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   165
        return true;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   166
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   167
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   168
    SourceFile samSourceFile = new SourceFile("FInterface.java", "#C") {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   169
        public String toString() {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   170
            String interfaces = "";
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   171
            for(FInterface fi : FInterface.values())
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   172
                interfaces += fi.interfaceDef + "\n";
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   173
            return template.replace("#C", interfaces);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   174
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   175
    };
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   176
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   177
    String clientTemplate = "class Client {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   178
                            "    #Context\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   179
                            "}\n\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   180
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   181
                            "class X {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   182
                            "    int value = 0;\n\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   183
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   184
                            "    X(A a) {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   185
                            "        value = a.m(6);\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   186
                            "    }\n\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   187
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   188
                            "    X(B b) {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   189
                            "        value = b.m(7);\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   190
                            "    }\n\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   191
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   192
                            "    X(C c) {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   193
                            "        try {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   194
                            "            value = c.m(8);\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   195
                            "        } catch (Exception e){}\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   196
                            "    }\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   197
                            "}";
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   198
    SourceFile clientSourceFile = new SourceFile("Client.java", clientTemplate) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   199
        public String toString() {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   200
            return template.replace("#Context", context.getContext(fInterface, lambdaKind, lambdaBody, returnValue));
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   201
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   202
    };
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   203
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   204
    void test() throws Exception {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   205
        System.out.println("\n====================================");
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   206
        System.out.println(fInterface + ", " +  context + ", " + lambdaKind + ", " + lambdaBody + ", " + returnValue);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   207
        System.out.println(samSourceFile + "\n");
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   208
        String clientFileStr = clientSourceFile.toString();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   209
        System.out.println(clientFileStr.substring(0, clientFileStr.indexOf("\n\n")));
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   210
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   211
        DiagnosticChecker dc = new DiagnosticChecker();
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   212
        JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22448
diff changeset
   213
        try {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22448
diff changeset
   214
            ct.analyze();
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22448
diff changeset
   215
        } catch (Exception e) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22448
diff changeset
   216
            throw new AssertionError("failing SAM source file \n" + samSourceFile + "\n\n" + "failing client source file \n"+ clientSourceFile);
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22448
diff changeset
   217
        }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   218
        if (dc.errorFound == checkSamConversion()) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   219
            throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   220
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   221
        count++;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   222
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   223
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   224
    abstract class SourceFile extends SimpleJavaFileObject {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   225
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   226
        protected String template;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   227
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   228
        public SourceFile(String filename, String template) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   229
            super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   230
            this.template = template;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   231
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   232
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   233
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   234
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   235
            return toString();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   236
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   237
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   238
        public abstract String toString();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   239
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   240
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   241
    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   242
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   243
        boolean errorFound = false;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   244
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   245
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   246
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   247
                errorFound = true;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   248
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   249
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   250
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   251
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   252
    FInterface fInterface;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   253
    Context context;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   254
    LambdaBody lambdaBody;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   255
    LambdaKind lambdaKind;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   256
    ReturnValue returnValue;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   257
    static int count = 0;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   258
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   259
    static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   260
    static JavaFileManager fm = comp.getStandardFileManager(null, null, null);
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   261
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   262
    SamConversionComboTest(FInterface f, Context c, LambdaBody lb, LambdaKind lk, ReturnValue rv) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   263
        fInterface = f;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   264
        context = c;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   265
        lambdaKind = lk;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   266
        lambdaBody = lb;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   267
        returnValue = rv;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   268
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   269
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   270
    public static void main(String[] args) throws Exception {
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   271
        try {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   272
            for(Context ct : Context.values()) {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   273
                for (FInterface fi : FInterface.values()) {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   274
                    for (LambdaKind lk: LambdaKind.values()) {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   275
                        for (LambdaBody lb : LambdaBody.values()) {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   276
                            for(ReturnValue rv : ReturnValue.values()) {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   277
                                new SamConversionComboTest(fi, ct, lb, lk, rv).test();
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   278
                            }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   279
                        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   280
                    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   281
                }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   282
            }
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   283
        System.out.println("total tests: " + count);
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   284
        } finally {
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 24066
diff changeset
   285
            fm.close();
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   286
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   287
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   288
}