mcimadamore@1144: /* vromero@1482: * Copyright (c) 2011, 2013, 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@1503: * @bug 7115050 8003280 8005852 mcimadamore@1415: * @summary Add lambda tests mcimadamore@1415: * Add parser support for lambda expressions vromero@1482: * @library ../lib vromero@1482: * @build JavacTestingAbstractThreadedTest vromero@1482: * @run main LambdaParserTest mcimadamore@1144: */ mcimadamore@1144: mcimadamore@1144: import java.net.URI; mcimadamore@1144: import java.util.Arrays; mcimadamore@1144: import javax.tools.Diagnostic; mcimadamore@1144: import javax.tools.JavaFileObject; mcimadamore@1144: import javax.tools.SimpleJavaFileObject; vromero@1482: import com.sun.source.util.JavacTask; mcimadamore@1144: vromero@1482: public class LambdaParserTest vromero@1482: extends JavacTestingAbstractThreadedTest vromero@1482: implements Runnable { mcimadamore@1144: mcimadamore@1144: enum LambdaKind { mcimadamore@1144: NILARY_EXPR("()->x"), mcimadamore@1144: NILARY_STMT("()->{ return x; }"), mcimadamore@1503: ONEARY_SHORT_EXPR("#PN->x"), mcimadamore@1503: ONEARY_SHORT_STMT("#PN->{ return x; }"), mcimadamore@1503: ONEARY_EXPR("(#M1 #T1 #PN)->x"), mcimadamore@1503: ONEARY_STMT("(#M1 #T1 #PN)->{ return x; }"), mcimadamore@1503: TWOARY_EXPR("(#M1 #T1 #PN, #M2 #T2 y)->x"), mcimadamore@1503: TWOARY_STMT("(#M1 #T1 #PN, #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@1503: ModifierKind mk1, ModifierKind mk2, LambdaParameterName pn) { mcimadamore@1144: return lambdaTemplate.replaceAll("#M1", mk1.modifier) mcimadamore@1144: .replaceAll("#M2", mk2.modifier) mcimadamore@1144: .replaceAll("#T1", pk1.parameterType) mcimadamore@1503: .replaceAll("#T2", pk2.parameterType) mcimadamore@1503: .replaceAll("#PN", pn.nameStr); 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@1503: enum LambdaParameterName { mcimadamore@1503: IDENT("x"), mcimadamore@1503: UNDERSCORE("_"); mcimadamore@1503: mcimadamore@1503: String nameStr; mcimadamore@1503: mcimadamore@1503: LambdaParameterName(String nameStr) { mcimadamore@1503: this.nameStr = nameStr; mcimadamore@1503: } mcimadamore@1503: } mcimadamore@1503: mcimadamore@1144: enum LambdaParameterKind { mcimadamore@1144: IMPLICIT(""), mcimadamore@1144: EXPLIICT_SIMPLE("A"), mcimadamore@1436: EXPLIICT_SIMPLE_ARR1("A[]"), mcimadamore@1436: EXPLIICT_SIMPLE_ARR2("A[][]"), mcimadamore@1144: EXPLICIT_VARARGS("A..."), mcimadamore@1144: EXPLICIT_GENERIC1("A"), mcimadamore@1436: EXPLICIT_GENERIC2("A"), mcimadamore@1436: EXPLICIT_GENERIC2_VARARGS("A..."), mcimadamore@1436: EXPLICIT_GENERIC2_ARR1("A[]"), mcimadamore@1436: EXPLICIT_GENERIC2_ARR2("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@1436: mcimadamore@1436: boolean isVarargs() { mcimadamore@1436: return this == EXPLICIT_VARARGS || mcimadamore@1436: this == EXPLICIT_GENERIC2_VARARGS; mcimadamore@1436: } 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@1503: ModifierKind mk1, ModifierKind mk2, LambdaKind lk, LambdaParameterName pn, SubExprKind sk) { mcimadamore@1503: return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2, pn)) 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: for (LambdaKind lk : LambdaKind.values()) { mcimadamore@1503: for (LambdaParameterName pn : LambdaParameterName.values()) { mcimadamore@1503: for (LambdaParameterKind pk1 : LambdaParameterKind.values()) { mcimadamore@1503: if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT) vromero@1482: continue; mcimadamore@1503: for (LambdaParameterKind pk2 : LambdaParameterKind.values()) { mcimadamore@1503: if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT) vromero@1482: continue; mcimadamore@1503: for (ModifierKind mk1 : ModifierKind.values()) { mcimadamore@1503: if (mk1 != ModifierKind.NONE && lk.isShort()) vromero@1482: continue; mcimadamore@1503: if (lk.arity() < 1 && mk1 != ModifierKind.NONE) mcimadamore@1503: continue; mcimadamore@1503: for (ModifierKind mk2 : ModifierKind.values()) { mcimadamore@1503: if (lk.arity() < 2 && mk2 != ModifierKind.NONE) mcimadamore@1503: continue; mcimadamore@1503: for (SubExprKind sk : SubExprKind.values()) { mcimadamore@1503: for (ExprKind ek : ExprKind.values()) { mcimadamore@1503: pool.execute( mcimadamore@1503: new LambdaParserTest(pk1, pk2, mk1, mcimadamore@1503: mk2, lk, sk, ek, pn)); mcimadamore@1503: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } mcimadamore@1144: } vromero@1482: vromero@1482: checkAfterExec(); 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@1503: LambdaParameterName pn; mcimadamore@1144: SubExprKind sk; mcimadamore@1144: ExprKind ek; mcimadamore@1144: JavaSource source; mcimadamore@1144: DiagnosticChecker diagChecker; mcimadamore@1144: vromero@1482: LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2, vromero@1482: ModifierKind mk1, ModifierKind mk2, LambdaKind lk, mcimadamore@1503: SubExprKind sk, ExprKind ek, LambdaParameterName pn) { 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@1503: this.pn = pn; 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); vromero@1482: source = template.replaceAll("#E", mcimadamore@1503: ek.expressionString(pk1, pk2, mk1, mk2, lk, pn, 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: vromero@1482: public void run() { vromero@1482: JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker, mcimadamore@1415: null, null, Arrays.asList(source)); mcimadamore@1144: try { mcimadamore@1144: ct.parse(); mcimadamore@1144: } catch (Throwable ex) { vromero@1482: processException(ex); vromero@1482: return; mcimadamore@1144: } mcimadamore@1144: check(); mcimadamore@1144: } mcimadamore@1144: mcimadamore@1144: void check() { vromero@1482: checkCount.incrementAndGet(); 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@1436: pk1.isVarargs())) { mcimadamore@1144: errorExpected = true; mcimadamore@1144: } mcimadamore@1144: mcimadamore@1503: errorExpected |= pn == LambdaParameterName.UNDERSCORE && mcimadamore@1503: lk.arity() > 0; mcimadamore@1503: 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: vromero@1482: static class DiagnosticChecker vromero@1482: 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: } vromero@1482: mcimadamore@1144: }