aoqi@0: /* aoqi@0: * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * @test aoqi@0: * @bug 8003280 aoqi@0: * @summary Add lambda tests aoqi@0: * Test SAM conversion of lambda expressions in combinations of different contexts, aoqi@0: * lambda body types(statement/expression), explict/implicit target type etc, to verify aoqi@0: * SAM conversion being conducted successfully as expected. aoqi@0: */ aoqi@0: aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import java.net.URI; aoqi@0: import java.util.Arrays; aoqi@0: import javax.tools.Diagnostic; aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: public class SamConversionComboTest { aoqi@0: aoqi@0: enum FInterface { aoqi@0: A("A", "interface A { Integer m(int i); }"), aoqi@0: B("B", "interface B { int m(Integer i); }"), aoqi@0: C("C", "interface C { int m(Integer i) throws Exception; }"); aoqi@0: aoqi@0: String interfaceType; aoqi@0: String interfaceDef; aoqi@0: aoqi@0: FInterface(String interfaceType, String interfaceDef) { aoqi@0: this.interfaceType = interfaceType; aoqi@0: this.interfaceDef = interfaceDef; aoqi@0: } aoqi@0: aoqi@0: String getParameterType() { aoqi@0: switch(this) { aoqi@0: case A: aoqi@0: return "int"; aoqi@0: case B: aoqi@0: case C: aoqi@0: return "Integer"; aoqi@0: default: aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum Context { aoqi@0: ASSIGNMENT("#FType f = #LBody;"), aoqi@0: METHOD_CALL("void method1(#FType f) { }\n" + aoqi@0: " void method2() {\n" + aoqi@0: " method1(#LBody);\n" + aoqi@0: " }"), aoqi@0: CONSTRUCTOR("X x = new X(#LBody);"), aoqi@0: RETURN_OF_METHOD("#FType method1() {\n" + aoqi@0: " return #LBody;\n" + aoqi@0: "}"), aoqi@0: ARRAY_INITIALIZER("Object[] oarray = {\"a\", 1, (#FType)#LBody};"), aoqi@0: LAMBDA_BODY("#FType f = n -> ((#FType)#LBody).m(n);"), aoqi@0: CAST("void test() throws Exception { int n = ((#FType)#LBody).m(1); }"), aoqi@0: CONDITIONAL_EXPRESSION("#FType f = 2 > 1 ? #LBody : null;"); aoqi@0: aoqi@0: String context; aoqi@0: aoqi@0: Context(String context) { aoqi@0: this.context = context; aoqi@0: } aoqi@0: aoqi@0: String getContext(FInterface f, LambdaKind lk, LambdaBody lb, ReturnValue rv) { aoqi@0: return context.replace("#FType", f.interfaceType).replace("#LBody", lb.getLambdaBody(f, lk, rv)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum LambdaKind { aoqi@0: EXPRESSION("#VAL"), aoqi@0: STATEMENT("{return #VAL;}"), aoqi@0: EXCEPTION_STMT("{throw new Exception();}"); aoqi@0: aoqi@0: String stmt; aoqi@0: aoqi@0: LambdaKind(String stmt) { aoqi@0: this.stmt = stmt; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum ReturnValue { aoqi@0: INT("i + 1"), aoqi@0: INTEGER("new Integer(i+1)"), aoqi@0: INT2("i.intValue() + 1"), aoqi@0: STRING("i + \"\""), aoqi@0: DOUBLE("i * 1.0"); aoqi@0: aoqi@0: String rValue; aoqi@0: aoqi@0: ReturnValue(String rValue) { aoqi@0: this.rValue = rValue; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum LambdaBody { aoqi@0: IMPLICIT("i -> #RET"),//type inferred aoqi@0: EXPLICIT("(#Type i) -> #RET");//explicit type aoqi@0: aoqi@0: String bodyStr; aoqi@0: aoqi@0: LambdaBody(String bodyStr) { aoqi@0: this.bodyStr = bodyStr; aoqi@0: } aoqi@0: aoqi@0: String getLambdaBody(FInterface fi, LambdaKind lk, ReturnValue rv) { aoqi@0: return bodyStr.replace("#Type", fi.getParameterType()).replace("#RET", lk.stmt.replace("#VAL", rv.rValue)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: boolean checkSamConversion() { aoqi@0: if(lambdaKind != LambdaKind.EXCEPTION_STMT && (returnValue == ReturnValue.DOUBLE || returnValue == ReturnValue.STRING)) //return type mismatch aoqi@0: return false; aoqi@0: if(context != Context.CONSTRUCTOR) {//context other than construcotr argument aoqi@0: if(fInterface != FInterface.C && lambdaKind == LambdaKind.EXCEPTION_STMT) aoqi@0: return false; aoqi@0: if(fInterface == FInterface.A && returnValue == ReturnValue.INT2) aoqi@0: return false; aoqi@0: } aoqi@0: else { //constructor argument context aoqi@0: //match X(A a) or X(B b) or X(C c) aoqi@0: if (lambdaKind == LambdaKind.EXCEPTION_STMT) { aoqi@0: return false; //ambiguous target type aoqi@0: } aoqi@0: else if(lambdaBody == LambdaBody.IMPLICIT) { aoqi@0: return false; aoqi@0: } aoqi@0: else { //explicit parameter type aoqi@0: if(fInterface.getParameterType().equals("Integer")) //ambiguous target type aoqi@0: //e.g. X x = new X((Integer i) -> i + 1); aoqi@0: return false; aoqi@0: if(returnValue == ReturnValue.INT2) aoqi@0: //e.g. X x = new X(int i -> i.intValue() + 1); aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: SourceFile samSourceFile = new SourceFile("FInterface.java", "#C") { aoqi@0: public String toString() { aoqi@0: String interfaces = ""; aoqi@0: for(FInterface fi : FInterface.values()) aoqi@0: interfaces += fi.interfaceDef + "\n"; aoqi@0: return template.replace("#C", interfaces); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: String clientTemplate = "class Client {\n" + aoqi@0: " #Context\n" + aoqi@0: "}\n\n" + aoqi@0: aoqi@0: "class X {\n" + aoqi@0: " int value = 0;\n\n" + aoqi@0: aoqi@0: " X(A a) {\n" + aoqi@0: " value = a.m(6);\n" + aoqi@0: " }\n\n" + aoqi@0: aoqi@0: " X(B b) {\n" + aoqi@0: " value = b.m(7);\n" + aoqi@0: " }\n\n" + aoqi@0: aoqi@0: " X(C c) {\n" + aoqi@0: " try {\n" + aoqi@0: " value = c.m(8);\n" + aoqi@0: " } catch (Exception e){}\n" + aoqi@0: " }\n" + aoqi@0: "}"; aoqi@0: SourceFile clientSourceFile = new SourceFile("Client.java", clientTemplate) { aoqi@0: public String toString() { aoqi@0: return template.replace("#Context", context.getContext(fInterface, lambdaKind, lambdaBody, returnValue)); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: void test() throws Exception { aoqi@0: System.out.println("\n===================================="); aoqi@0: System.out.println(fInterface + ", " + context + ", " + lambdaKind + ", " + lambdaBody + ", " + returnValue); aoqi@0: System.out.println(samSourceFile + "\n"); aoqi@0: String clientFileStr = clientSourceFile.toString(); aoqi@0: System.out.println(clientFileStr.substring(0, clientFileStr.indexOf("\n\n"))); aoqi@0: aoqi@0: final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); aoqi@0: DiagnosticChecker dc = new DiagnosticChecker(); aoqi@0: JavacTask ct = (JavacTask)tool.getTask(null, null, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile)); aoqi@0: try { aoqi@0: ct.analyze(); aoqi@0: } catch (Exception e) { aoqi@0: throw new AssertionError("failing SAM source file \n" + samSourceFile + "\n\n" + "failing client source file \n"+ clientSourceFile); aoqi@0: } aoqi@0: if (dc.errorFound == checkSamConversion()) { aoqi@0: throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile); aoqi@0: } aoqi@0: count++; aoqi@0: } aoqi@0: aoqi@0: abstract class SourceFile extends SimpleJavaFileObject { aoqi@0: aoqi@0: protected String template; aoqi@0: aoqi@0: public SourceFile(String filename, String template) { aoqi@0: super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE); aoqi@0: this.template = template; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return toString(); aoqi@0: } aoqi@0: aoqi@0: public abstract String toString(); aoqi@0: } aoqi@0: aoqi@0: static class DiagnosticChecker implements javax.tools.DiagnosticListener { aoqi@0: aoqi@0: boolean errorFound = false; aoqi@0: aoqi@0: public void report(Diagnostic diagnostic) { aoqi@0: if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { aoqi@0: errorFound = true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: FInterface fInterface; aoqi@0: Context context; aoqi@0: LambdaBody lambdaBody; aoqi@0: LambdaKind lambdaKind; aoqi@0: ReturnValue returnValue; aoqi@0: static int count = 0; aoqi@0: aoqi@0: SamConversionComboTest(FInterface f, Context c, LambdaBody lb, LambdaKind lk, ReturnValue rv) { aoqi@0: fInterface = f; aoqi@0: context = c; aoqi@0: lambdaKind = lk; aoqi@0: lambdaBody = lb; aoqi@0: returnValue = rv; aoqi@0: } aoqi@0: aoqi@0: public static void main(String[] args) throws Exception { aoqi@0: for(Context ct : Context.values()) { aoqi@0: for (FInterface fi : FInterface.values()) { aoqi@0: for (LambdaKind lk: LambdaKind.values()) { aoqi@0: for (LambdaBody lb : LambdaBody.values()) { aoqi@0: for(ReturnValue rv : ReturnValue.values()) { aoqi@0: new SamConversionComboTest(fi, ct, lb, lk, rv).test(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: System.out.println("total tests: " + count); aoqi@0: } aoqi@0: }