mcimadamore@1144: /* mcimadamore@1144: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. mcimadamore@1144: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@1144: * mcimadamore@1144: * This code is free software; you can redistribute it and/or modify it mcimadamore@1144: * under the terms of the GNU General Public License version 2 only, as mcimadamore@1144: * published by the Free Software Foundation. mcimadamore@1144: * mcimadamore@1144: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@1144: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@1144: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@1144: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@1144: * accompanied this code). mcimadamore@1144: * mcimadamore@1144: * You should have received a copy of the GNU General Public License version mcimadamore@1144: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@1144: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@1144: * mcimadamore@1144: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@1144: * or visit www.oracle.com if you need additional information or have any mcimadamore@1144: * questions. mcimadamore@1144: */ mcimadamore@1144: mcimadamore@1144: /* mcimadamore@1144: * @test mcimadamore@1144: * @bug 7115050 mcimadamore@1144: * @summary Add parser support for lambda expressions mcimadamore@1144: */ mcimadamore@1144: mcimadamore@1144: import com.sun.source.util.JavacTask; mcimadamore@1144: import java.net.URI; mcimadamore@1144: import java.util.Arrays; mcimadamore@1144: import javax.tools.Diagnostic; mcimadamore@1144: import javax.tools.JavaCompiler; mcimadamore@1144: import javax.tools.JavaFileObject; mcimadamore@1144: import javax.tools.SimpleJavaFileObject; mcimadamore@1144: import javax.tools.StandardJavaFileManager; mcimadamore@1144: import javax.tools.ToolProvider; mcimadamore@1144: mcimadamore@1144: public class LambdaParserTest { mcimadamore@1144: mcimadamore@1144: static int checkCount = 0; mcimadamore@1144: mcimadamore@1144: enum LambdaKind { mcimadamore@1144: NILARY_EXPR("()->x"), mcimadamore@1144: NILARY_STMT("()->{ return x; }"), mcimadamore@1144: ONEARY_SHORT_EXPR("x->x"), mcimadamore@1144: ONEARY_SHORT_STMT("x->{ return x; }"), mcimadamore@1144: ONEARY_EXPR("(#M1 #T1 x)->x"), mcimadamore@1144: ONEARY_STMT("(#M1 #T1 x)->{ return x; }"), mcimadamore@1144: TWOARY_EXPR("(#M1 #T1 x, #M2 #T2 y)->x"), mcimadamore@1144: TWOARY_STMT("(#M1 #T1 x, #M2 #T2 y)->{ return x; }"); mcimadamore@1144: mcimadamore@1144: String lambdaTemplate; mcimadamore@1144: mcimadamore@1144: LambdaKind(String lambdaTemplate) { mcimadamore@1144: this.lambdaTemplate = lambdaTemplate; mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: String getLambdaString(LambdaParameterKind pk1, LambdaParameterKind pk2, mcimadamore@1144: ModifierKind mk1, ModifierKind mk2) { mcimadamore@1144: return lambdaTemplate.replaceAll("#M1", mk1.modifier) mcimadamore@1144: .replaceAll("#M2", mk2.modifier) mcimadamore@1144: .replaceAll("#T1", pk1.parameterType) mcimadamore@1144: .replaceAll("#T2", pk2.parameterType); mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: int arity() { mcimadamore@1144: switch (this) { mcimadamore@1144: case NILARY_EXPR: mcimadamore@1144: case NILARY_STMT: return 0; mcimadamore@1144: case ONEARY_SHORT_EXPR: mcimadamore@1144: case ONEARY_SHORT_STMT: mcimadamore@1144: case ONEARY_EXPR: mcimadamore@1144: case ONEARY_STMT: return 1; mcimadamore@1144: case TWOARY_EXPR: mcimadamore@1144: case TWOARY_STMT: return 2; mcimadamore@1144: default: throw new AssertionError("Invalid lambda kind " + this); mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: boolean isShort() { mcimadamore@1144: return this == ONEARY_SHORT_EXPR || mcimadamore@1144: this == ONEARY_SHORT_STMT; mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: enum LambdaParameterKind { mcimadamore@1144: IMPLICIT(""), mcimadamore@1144: EXPLIICT_SIMPLE("A"), mcimadamore@1144: EXPLICIT_VARARGS("A..."), mcimadamore@1144: EXPLICIT_GENERIC1("A"), mcimadamore@1144: EXPLICIT_GENERIC3("A"); mcimadamore@1144: mcimadamore@1144: String parameterType; mcimadamore@1144: mcimadamore@1144: LambdaParameterKind(String parameterType) { mcimadamore@1144: this.parameterType = parameterType; mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: boolean explicit() { mcimadamore@1144: return this != IMPLICIT; mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: enum ModifierKind { mcimadamore@1144: NONE(""), mcimadamore@1144: FINAL("final"), mcimadamore@1144: PUBLIC("public"); mcimadamore@1144: mcimadamore@1144: String modifier; mcimadamore@1144: mcimadamore@1144: ModifierKind(String modifier) { mcimadamore@1144: this.modifier = modifier; mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: boolean compatibleWith(LambdaParameterKind pk) { mcimadamore@1144: switch (this) { mcimadamore@1144: case PUBLIC: return false; mcimadamore@1144: case FINAL: return pk != LambdaParameterKind.IMPLICIT; mcimadamore@1144: case NONE: return true; mcimadamore@1144: default: throw new AssertionError("Invalid modifier kind " + this); mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: enum ExprKind { mcimadamore@1144: NONE("#L#S"), mcimadamore@1144: SINGLE_PAREN1("(#L#S)"), mcimadamore@1144: SINGLE_PAREN2("(#L)#S"), mcimadamore@1144: DOUBLE_PAREN1("((#L#S))"), mcimadamore@1144: DOUBLE_PAREN2("((#L)#S)"), mcimadamore@1144: DOUBLE_PAREN3("((#L))#S"); mcimadamore@1144: mcimadamore@1144: String expressionTemplate; mcimadamore@1144: mcimadamore@1144: ExprKind(String expressionTemplate) { mcimadamore@1144: this.expressionTemplate = expressionTemplate; mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2, mcimadamore@1144: ModifierKind mk1, ModifierKind mk2, LambdaKind lk, SubExprKind sk) { mcimadamore@1144: return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2)) mcimadamore@1144: .replaceAll("#S", sk.subExpression); mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: enum SubExprKind { mcimadamore@1144: NONE(""), mcimadamore@1144: SELECT_FIELD(".f"), mcimadamore@1144: SELECT_METHOD(".f()"), mcimadamore@1144: SELECT_NEW(".new Foo()"), mcimadamore@1144: POSTINC("++"), mcimadamore@1144: POSTDEC("--"); mcimadamore@1144: mcimadamore@1144: String subExpression; mcimadamore@1144: mcimadamore@1144: SubExprKind(String subExpression) { mcimadamore@1144: this.subExpression = subExpression; mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: public static void main(String... args) throws Exception { mcimadamore@1144: mcimadamore@1144: //create default shared JavaCompiler - reused across multiple compilations mcimadamore@1144: JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); mcimadamore@1144: StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); mcimadamore@1144: mcimadamore@1144: for (LambdaKind lk : LambdaKind.values()) { mcimadamore@1144: for (LambdaParameterKind pk1 : LambdaParameterKind.values()) { mcimadamore@1144: if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT) continue; mcimadamore@1144: for (LambdaParameterKind pk2 : LambdaParameterKind.values()) { mcimadamore@1144: if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT) continue; mcimadamore@1144: for (ModifierKind mk1 : ModifierKind.values()) { mcimadamore@1144: if (mk1 != ModifierKind.NONE && lk.isShort()) continue; mcimadamore@1144: if (lk.arity() < 1 && mk1 != ModifierKind.NONE) continue; mcimadamore@1144: for (ModifierKind mk2 : ModifierKind.values()) { mcimadamore@1144: if (lk.arity() < 2 && mk2 != ModifierKind.NONE) continue; mcimadamore@1144: for (SubExprKind sk : SubExprKind.values()) { mcimadamore@1144: for (ExprKind ek : ExprKind.values()) { mcimadamore@1144: new LambdaParserTest(pk1, pk2, mk1, mk2, lk, sk, ek) mcimadamore@1144: .run(comp, fm); mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: System.out.println("Total check executed: " + checkCount); mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: LambdaParameterKind pk1; mcimadamore@1144: LambdaParameterKind pk2; mcimadamore@1144: ModifierKind mk1; mcimadamore@1144: ModifierKind mk2; mcimadamore@1144: LambdaKind lk; mcimadamore@1144: SubExprKind sk; mcimadamore@1144: ExprKind ek; mcimadamore@1144: JavaSource source; mcimadamore@1144: DiagnosticChecker diagChecker; mcimadamore@1144: mcimadamore@1144: LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2, ModifierKind mk1, mcimadamore@1144: ModifierKind mk2, LambdaKind lk, SubExprKind sk, ExprKind ek) { mcimadamore@1144: this.pk1 = pk1; mcimadamore@1144: this.pk2 = pk2; mcimadamore@1144: this.mk1 = mk1; mcimadamore@1144: this.mk2 = mk2; mcimadamore@1144: this.lk = lk; mcimadamore@1144: this.sk = sk; mcimadamore@1144: this.ek = ek; mcimadamore@1144: this.source = new JavaSource(); mcimadamore@1144: this.diagChecker = new DiagnosticChecker(); mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: class JavaSource extends SimpleJavaFileObject { mcimadamore@1144: mcimadamore@1144: String template = "class Test {\n" + mcimadamore@1144: " SAM s = #E;\n" + mcimadamore@1144: "}"; mcimadamore@1144: mcimadamore@1144: String source; mcimadamore@1144: mcimadamore@1144: public JavaSource() { mcimadamore@1144: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); mcimadamore@1144: source = template.replaceAll("#E", ek.expressionString(pk1, pk2, mk1, mk2, lk, sk)); mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: @Override mcimadamore@1144: public CharSequence getCharContent(boolean ignoreEncodingErrors) { mcimadamore@1144: return source; mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { mcimadamore@1144: JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, mcimadamore@1144: Arrays.asList("-XDallowLambda"), null, Arrays.asList(source)); mcimadamore@1144: try { mcimadamore@1144: ct.parse(); mcimadamore@1144: } catch (Throwable ex) { jjg@1169: throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true)); mcimadamore@1144: } mcimadamore@1144: check(); mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: void check() { mcimadamore@1144: checkCount++; mcimadamore@1144: mcimadamore@1144: boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) || mcimadamore@1144: (lk.arity() > 1 && !mk2.compatibleWith(pk2)); mcimadamore@1144: mcimadamore@1144: if (lk.arity() == 2 && mcimadamore@1144: (pk1.explicit() != pk2.explicit() || mcimadamore@1144: pk1 == LambdaParameterKind.EXPLICIT_VARARGS)) { mcimadamore@1144: errorExpected = true; mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: if (errorExpected != diagChecker.errorFound) { mcimadamore@1144: throw new Error("invalid diagnostics for source:\n" + mcimadamore@1144: source.getCharContent(true) + mcimadamore@1144: "\nFound error: " + diagChecker.errorFound + mcimadamore@1144: "\nExpected error: " + errorExpected); mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: static class DiagnosticChecker implements javax.tools.DiagnosticListener { mcimadamore@1144: mcimadamore@1144: boolean errorFound; mcimadamore@1144: mcimadamore@1144: public void report(Diagnostic diagnostic) { mcimadamore@1144: if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { mcimadamore@1144: errorFound = true; mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: }