v, D d) {
+ return v.visitMemberReference(this, d);
+ }
+ @Override
+ public Tag getTag() {
+ return REFERENCE;
+ }
+ }
+
/**
* An identifier
* @param idname the name
@@ -2271,6 +2371,7 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
public void visitApply(JCMethodInvocation that) { visitTree(that); }
public void visitNewClass(JCNewClass that) { visitTree(that); }
public void visitNewArray(JCNewArray that) { visitTree(that); }
+ public void visitLambda(JCLambda that) { visitTree(that); }
public void visitParens(JCParens that) { visitTree(that); }
public void visitAssign(JCAssign that) { visitTree(that); }
public void visitAssignop(JCAssignOp that) { visitTree(that); }
@@ -2280,6 +2381,7 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
public void visitTypeTest(JCInstanceOf that) { visitTree(that); }
public void visitIndexed(JCArrayAccess that) { visitTree(that); }
public void visitSelect(JCFieldAccess that) { visitTree(that); }
+ public void visitReference(JCMemberReference that) { visitTree(that); }
public void visitIdent(JCIdent that) { visitTree(that); }
public void visitLiteral(JCLiteral that) { visitTree(that); }
public void visitTypeIdent(JCPrimitiveTypeTree that) { visitTree(that); }
diff --git a/src/share/classes/com/sun/tools/javac/tree/Pretty.java b/src/share/classes/com/sun/tools/javac/tree/Pretty.java
index e781fd130e6d37fa8ef2b5148a44026ee7ff7e81..824fa599ff7ea0634a41ca4c89880135e606f165 100644
--- a/src/share/classes/com/sun/tools/javac/tree/Pretty.java
+++ b/src/share/classes/com/sun/tools/javac/tree/Pretty.java
@@ -28,6 +28,8 @@ package com.sun.tools.javac.tree;
import java.io.*;
import java.util.*;
+import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
+
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.code.*;
@@ -907,6 +909,17 @@ public class Pretty extends JCTree.Visitor {
}
}
+ public void visitLambda(JCLambda tree) {
+ try {
+ print("(");
+ printExprs(tree.params);
+ print(")->");
+ printExpr(tree.body);
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
public void visitParens(JCParens tree) {
try {
print("(");
@@ -1052,6 +1065,21 @@ public class Pretty extends JCTree.Visitor {
}
}
+ public void visitReference(JCMemberReference tree) {
+ try {
+ printExpr(tree.expr);
+ print("#");
+ if (tree.typeargs != null) {
+ print("<");
+ printExprs(tree.typeargs);
+ print(">");
+ }
+ print(tree.getMode() == ReferenceMode.INVOKE ? tree.name : "new");
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
public void visitIdent(JCIdent tree) {
try {
print(tree.name);
diff --git a/src/share/classes/com/sun/tools/javac/tree/TreeCopier.java b/src/share/classes/com/sun/tools/javac/tree/TreeCopier.java
index 158abc4f161d41d9d5f664ff2a32f816e47bebe7..f8efaa320189edf2ed48b6b679ff04957462596d 100644
--- a/src/share/classes/com/sun/tools/javac/tree/TreeCopier.java
+++ b/src/share/classes/com/sun/tools/javac/tree/TreeCopier.java
@@ -271,6 +271,13 @@ public class TreeCopier implements TreeVisitor {
return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def);
}
+ public JCTree visitLambdaExpression(LambdaExpressionTree node, P p) {
+ JCLambda t = (JCLambda) node;
+ List params = copy(t.params, p);
+ JCTree body = copy(t.body, p);
+ return M.at(t.pos).Lambda(params, body);
+ }
+
public JCTree visitParenthesized(ParenthesizedTree node, P p) {
JCParens t = (JCParens) node;
JCExpression expr = copy(t.expr, p);
@@ -289,6 +296,13 @@ public class TreeCopier implements TreeVisitor {
return M.at(t.pos).Select(selected, t.name);
}
+ public JCTree visitMemberReference(MemberReferenceTree node, P p) {
+ JCMemberReference t = (JCMemberReference) node;
+ JCExpression expr = copy(t.expr, p);
+ List typeargs = copy(t.typeargs, p);
+ return M.at(t.pos).Reference(t.mode, t.name, expr, typeargs);
+ }
+
public JCTree visitEmptyStatement(EmptyStatementTree node, P p) {
JCSkip t = (JCSkip) node;
return M.at(t.pos).Skip();
diff --git a/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java b/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java
index d47d7a79ff1235ed77cecc9cd326f7eb7b7f2462..00eef07509f93d71cc8fac379f7024d6d6d95266 100644
--- a/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java
+++ b/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java
@@ -227,6 +227,34 @@ public class TreeInfo {
}
}
+ /**
+ * Return true if the AST corresponds to a static select of the kind A.B
+ */
+ public static boolean isStaticSelector(JCTree base, Names names) {
+ if (base == null)
+ return false;
+ switch (base.getTag()) {
+ case IDENT:
+ JCIdent id = (JCIdent)base;
+ return id.name != names._this &&
+ id.name != names._super &&
+ isStaticSym(base);
+ case SELECT:
+ return isStaticSym(base) &&
+ isStaticSelector(((JCFieldAccess)base).selected, names);
+ case TYPEAPPLY:
+ return true;
+ default:
+ return false;
+ }
+ }
+ //where
+ private static boolean isStaticSym(JCTree tree) {
+ Symbol sym = symbol(tree);
+ return (sym.kind == Kinds.TYP ||
+ sym.kind == Kinds.PCK);
+ }
+
/** Return true if a tree represents the null literal. */
public static boolean isNull(JCTree tree) {
if (!tree.hasTag(LITERAL))
diff --git a/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java b/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java
index 7f673023def828ff705d609be0cfc7dc988ab257..6191f4b88757367825bb433581475b06fb1dbae1 100644
--- a/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java
+++ b/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java
@@ -351,6 +351,14 @@ public class TreeMaker implements JCTree.Factory {
return tree;
}
+ public JCLambda Lambda(List params,
+ JCTree body)
+ {
+ JCLambda tree = new JCLambda(params, body);
+ tree.pos = pos;
+ return tree;
+ }
+
public JCParens Parens(JCExpression expr) {
JCParens tree = new JCParens(expr);
tree.pos = pos;
@@ -405,6 +413,13 @@ public class TreeMaker implements JCTree.Factory {
return tree;
}
+ public JCMemberReference Reference(JCMemberReference.ReferenceMode mode, Name name,
+ JCExpression expr, List typeargs) {
+ JCMemberReference tree = new JCMemberReference(mode, name, expr, typeargs);
+ tree.pos = pos;
+ return tree;
+ }
+
public JCIdent Ident(Name name) {
JCIdent tree = new JCIdent(name, null);
tree.pos = pos;
diff --git a/src/share/classes/com/sun/tools/javac/tree/TreeScanner.java b/src/share/classes/com/sun/tools/javac/tree/TreeScanner.java
index 19619665d768ea6c59af014284db2782ad8c8df2..9a7c321f368bf8366df8492421df42294cdf2e6b 100644
--- a/src/share/classes/com/sun/tools/javac/tree/TreeScanner.java
+++ b/src/share/classes/com/sun/tools/javac/tree/TreeScanner.java
@@ -212,6 +212,11 @@ public class TreeScanner extends Visitor {
scan(tree.elems);
}
+ public void visitLambda(JCLambda tree) {
+ scan(tree.body);
+ scan(tree.params);
+ }
+
public void visitParens(JCParens tree) {
scan(tree.expr);
}
@@ -254,6 +259,11 @@ public class TreeScanner extends Visitor {
scan(tree.selected);
}
+ public void visitReference(JCMemberReference tree) {
+ scan(tree.expr);
+ scan(tree.typeargs);
+ }
+
public void visitIdent(JCIdent tree) {
}
diff --git a/src/share/classes/com/sun/tools/javac/tree/TreeTranslator.java b/src/share/classes/com/sun/tools/javac/tree/TreeTranslator.java
index f024a287bd9f1ae90333d1e658f902e54c080be5..2bc9c7a1290592fe2155fc6b8e7fd82aae3618ad 100644
--- a/src/share/classes/com/sun/tools/javac/tree/TreeTranslator.java
+++ b/src/share/classes/com/sun/tools/javac/tree/TreeTranslator.java
@@ -282,6 +282,12 @@ public class TreeTranslator extends JCTree.Visitor {
result = tree;
}
+ public void visitLambda(JCLambda tree) {
+ tree.params = translate(tree.params);
+ tree.body = translate(tree.body);
+ result = tree;
+ }
+
public void visitNewArray(JCNewArray tree) {
tree.elemtype = translate(tree.elemtype);
tree.dims = translate(tree.dims);
@@ -340,6 +346,11 @@ public class TreeTranslator extends JCTree.Visitor {
result = tree;
}
+ public void visitReference(JCMemberReference tree) {
+ tree.expr = translate(tree.expr);
+ result = tree;
+ }
+
public void visitIdent(JCIdent tree) {
result = tree;
}
diff --git a/test/tools/javac/diags/examples/CatchWithoutTry.java b/test/tools/javac/diags/examples/CatchWithoutTry.java
index 2fa707006369795b4c9fd5141566573080fe43eb..51eebedb014d63163ca2c50b23b58789fa6960cf 100644
--- a/test/tools/javac/diags/examples/CatchWithoutTry.java
+++ b/test/tools/javac/diags/examples/CatchWithoutTry.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,6 @@
// key: compiler.err.catch.without.try
// key: compiler.err.expected
-// key: compiler.err.not.stmt
class CatchWithoutTry {
void m() {
diff --git a/test/tools/javac/diags/examples/IllegalChar.java b/test/tools/javac/diags/examples/IllegalChar.java
index 003a7b0ece15cbab9b9aabb9dc0652f53eb43ad6..28bf82700e02d9b09ce9f692896b1b46b3906047 100644
--- a/test/tools/javac/diags/examples/IllegalChar.java
+++ b/test/tools/javac/diags/examples/IllegalChar.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,5 +24,5 @@
// key: compiler.err.illegal.char
class IllegalChar {
- int i = #;
+ int i = `;
}
diff --git a/test/tools/javac/diags/examples/LambdaNotSupported.java b/test/tools/javac/diags/examples/LambdaNotSupported.java
new file mode 100644
index 0000000000000000000000000000000000000000..ea57fe4740d2020fe1364284f2cab620092339e7
--- /dev/null
+++ b/test/tools/javac/diags/examples/LambdaNotSupported.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.err.lambda.not.supported.in.source
+// options: -source 7 -Xlint:-options
+
+class LambdaNotSupported {
+ S s = ()->{};
+}
diff --git a/test/tools/javac/diags/examples/MethodReferencesNotSupported.java b/test/tools/javac/diags/examples/MethodReferencesNotSupported.java
new file mode 100644
index 0000000000000000000000000000000000000000..df3198008b03f4b7cd21016f944f212a801a55f0
--- /dev/null
+++ b/test/tools/javac/diags/examples/MethodReferencesNotSupported.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.err.method.references.not.supported.in.source
+// options: -source 7 -Xlint:-options
+
+class MethodReferencesNotSupported {
+ S s = A#foo;
+}
diff --git a/test/tools/javac/diags/examples/NotAStatement.java b/test/tools/javac/diags/examples/NotAStatement.java
new file mode 100644
index 0000000000000000000000000000000000000000..39af85a83350c11ae8006206852a46e47630a4b5
--- /dev/null
+++ b/test/tools/javac/diags/examples/NotAStatement.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.err.not.stmt
+
+class NotAStatement {
+ void m() {
+ x + 1;
+ }
+}
diff --git a/test/tools/javac/generics/rare/6665356/T6665356.out b/test/tools/javac/generics/rare/6665356/T6665356.out
index 62a62e639256f5836fbc6c8946f55461da3f8dea..4994352df4bb632f8e293bf5933b3997dbefe8f8 100644
--- a/test/tools/javac/generics/rare/6665356/T6665356.out
+++ b/test/tools/javac/generics/rare/6665356/T6665356.out
@@ -1,5 +1,5 @@
T6665356.java:17:37: compiler.err.improperly.formed.type.param.missing
T6665356.java:18:40: compiler.err.improperly.formed.type.inner.raw.param
-T6665356.java:26:23: compiler.err.improperly.formed.type.param.missing
+T6665356.java:26:22: compiler.err.improperly.formed.type.param.missing
T6665356.java:27:25: compiler.err.improperly.formed.type.inner.raw.param
4 errors
diff --git a/test/tools/javac/lambda/LambdaParserTest.java b/test/tools/javac/lambda/LambdaParserTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..b686013aa5b05af06afd5df1f73fc16d72759659
--- /dev/null
+++ b/test/tools/javac/lambda/LambdaParserTest.java
@@ -0,0 +1,276 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 7115050
+ * @summary Add parser support for lambda expressions
+ */
+
+import com.sun.source.util.JavacTask;
+import java.net.URI;
+import java.util.Arrays;
+import javax.tools.Diagnostic;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+
+public class LambdaParserTest {
+
+ static int checkCount = 0;
+
+ enum LambdaKind {
+ NILARY_EXPR("()->x"),
+ NILARY_STMT("()->{ return x; }"),
+ ONEARY_SHORT_EXPR("x->x"),
+ ONEARY_SHORT_STMT("x->{ return x; }"),
+ ONEARY_EXPR("(#M1 #T1 x)->x"),
+ ONEARY_STMT("(#M1 #T1 x)->{ return x; }"),
+ TWOARY_EXPR("(#M1 #T1 x, #M2 #T2 y)->x"),
+ TWOARY_STMT("(#M1 #T1 x, #M2 #T2 y)->{ return x; }");
+
+ String lambdaTemplate;
+
+ LambdaKind(String lambdaTemplate) {
+ this.lambdaTemplate = lambdaTemplate;
+ }
+
+ String getLambdaString(LambdaParameterKind pk1, LambdaParameterKind pk2,
+ ModifierKind mk1, ModifierKind mk2) {
+ return lambdaTemplate.replaceAll("#M1", mk1.modifier)
+ .replaceAll("#M2", mk2.modifier)
+ .replaceAll("#T1", pk1.parameterType)
+ .replaceAll("#T2", pk2.parameterType);
+ }
+
+ int arity() {
+ switch (this) {
+ case NILARY_EXPR:
+ case NILARY_STMT: return 0;
+ case ONEARY_SHORT_EXPR:
+ case ONEARY_SHORT_STMT:
+ case ONEARY_EXPR:
+ case ONEARY_STMT: return 1;
+ case TWOARY_EXPR:
+ case TWOARY_STMT: return 2;
+ default: throw new AssertionError("Invalid lambda kind " + this);
+ }
+ }
+
+ boolean isShort() {
+ return this == ONEARY_SHORT_EXPR ||
+ this == ONEARY_SHORT_STMT;
+ }
+ }
+
+ enum LambdaParameterKind {
+ IMPLICIT(""),
+ EXPLIICT_SIMPLE("A"),
+ EXPLICIT_VARARGS("A..."),
+ EXPLICIT_GENERIC1("A"),
+ EXPLICIT_GENERIC3("A extends X, ? super Y>");
+
+ String parameterType;
+
+ LambdaParameterKind(String parameterType) {
+ this.parameterType = parameterType;
+ }
+
+ boolean explicit() {
+ return this != IMPLICIT;
+ }
+ }
+
+ enum ModifierKind {
+ NONE(""),
+ FINAL("final"),
+ PUBLIC("public");
+
+ String modifier;
+
+ ModifierKind(String modifier) {
+ this.modifier = modifier;
+ }
+
+ boolean compatibleWith(LambdaParameterKind pk) {
+ switch (this) {
+ case PUBLIC: return false;
+ case FINAL: return pk != LambdaParameterKind.IMPLICIT;
+ case NONE: return true;
+ default: throw new AssertionError("Invalid modifier kind " + this);
+ }
+ }
+ }
+
+ enum ExprKind {
+ NONE("#L#S"),
+ SINGLE_PAREN1("(#L#S)"),
+ SINGLE_PAREN2("(#L)#S"),
+ DOUBLE_PAREN1("((#L#S))"),
+ DOUBLE_PAREN2("((#L)#S)"),
+ DOUBLE_PAREN3("((#L))#S");
+
+ String expressionTemplate;
+
+ ExprKind(String expressionTemplate) {
+ this.expressionTemplate = expressionTemplate;
+ }
+
+ String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2,
+ ModifierKind mk1, ModifierKind mk2, LambdaKind lk, SubExprKind sk) {
+ return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2))
+ .replaceAll("#S", sk.subExpression);
+ }
+ }
+
+ enum SubExprKind {
+ NONE(""),
+ SELECT_FIELD(".f"),
+ SELECT_METHOD(".f()"),
+ SELECT_NEW(".new Foo()"),
+ POSTINC("++"),
+ POSTDEC("--");
+
+ String subExpression;
+
+ SubExprKind(String subExpression) {
+ this.subExpression = subExpression;
+ }
+ }
+
+ public static void main(String... args) throws Exception {
+
+ //create default shared JavaCompiler - reused across multiple compilations
+ JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
+ StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
+
+ for (LambdaKind lk : LambdaKind.values()) {
+ for (LambdaParameterKind pk1 : LambdaParameterKind.values()) {
+ if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT) continue;
+ for (LambdaParameterKind pk2 : LambdaParameterKind.values()) {
+ if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT) continue;
+ for (ModifierKind mk1 : ModifierKind.values()) {
+ if (mk1 != ModifierKind.NONE && lk.isShort()) continue;
+ if (lk.arity() < 1 && mk1 != ModifierKind.NONE) continue;
+ for (ModifierKind mk2 : ModifierKind.values()) {
+ if (lk.arity() < 2 && mk2 != ModifierKind.NONE) continue;
+ for (SubExprKind sk : SubExprKind.values()) {
+ for (ExprKind ek : ExprKind.values()) {
+ new LambdaParserTest(pk1, pk2, mk1, mk2, lk, sk, ek)
+ .run(comp, fm);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ System.out.println("Total check executed: " + checkCount);
+ }
+
+ LambdaParameterKind pk1;
+ LambdaParameterKind pk2;
+ ModifierKind mk1;
+ ModifierKind mk2;
+ LambdaKind lk;
+ SubExprKind sk;
+ ExprKind ek;
+ JavaSource source;
+ DiagnosticChecker diagChecker;
+
+ LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2, ModifierKind mk1,
+ ModifierKind mk2, LambdaKind lk, SubExprKind sk, ExprKind ek) {
+ this.pk1 = pk1;
+ this.pk2 = pk2;
+ this.mk1 = mk1;
+ this.mk2 = mk2;
+ this.lk = lk;
+ this.sk = sk;
+ this.ek = ek;
+ this.source = new JavaSource();
+ this.diagChecker = new DiagnosticChecker();
+ }
+
+ class JavaSource extends SimpleJavaFileObject {
+
+ String template = "class Test {\n" +
+ " SAM s = #E;\n" +
+ "}";
+
+ String source;
+
+ public JavaSource() {
+ super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
+ source = template.replaceAll("#E", ek.expressionString(pk1, pk2, mk1, mk2, lk, sk));
+ }
+
+ @Override
+ public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+ return source;
+ }
+ }
+
+ void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
+ JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
+ Arrays.asList("-XDallowLambda"), null, Arrays.asList(source));
+ try {
+ ct.parse();
+ } catch (Throwable ex) {
+ throw new AssertionError("Error thron when parsing the following source:\n" + source.getCharContent(true));
+ }
+ check();
+ }
+
+ void check() {
+ checkCount++;
+
+ boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) ||
+ (lk.arity() > 1 && !mk2.compatibleWith(pk2));
+
+ if (lk.arity() == 2 &&
+ (pk1.explicit() != pk2.explicit() ||
+ pk1 == LambdaParameterKind.EXPLICIT_VARARGS)) {
+ errorExpected = true;
+ }
+
+ if (errorExpected != diagChecker.errorFound) {
+ throw new Error("invalid diagnostics for source:\n" +
+ source.getCharContent(true) +
+ "\nFound error: " + diagChecker.errorFound +
+ "\nExpected error: " + errorExpected);
+ }
+ }
+
+ static class DiagnosticChecker implements javax.tools.DiagnosticListener {
+
+ boolean errorFound;
+
+ public void report(Diagnostic extends JavaFileObject> diagnostic) {
+ if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
+ errorFound = true;
+ }
+ }
+ }
+}
diff --git a/test/tools/javac/lambda/MethodReferenceParserTest.java b/test/tools/javac/lambda/MethodReferenceParserTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..0455820063b865f9a216ffd508b57cd49c53d108
--- /dev/null
+++ b/test/tools/javac/lambda/MethodReferenceParserTest.java
@@ -0,0 +1,233 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 7115052
+ * @summary Add parser support for method references
+ */
+
+import com.sun.source.util.JavacTask;
+import java.net.URI;
+import java.util.Arrays;
+import javax.tools.Diagnostic;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+
+public class MethodReferenceParserTest {
+
+ static int checkCount = 0;
+
+ enum ReferenceKind {
+ METHOD_REF("#Q##Gm"),
+ CONSTRUCTOR_REF("#Q##Gnew"),
+ ERR_SUPER("#Q##Gsuper"),
+ ERR_METH0("#Q##Gm()"),
+ ERR_METH1("#Q##Gm(X)"),
+ ERR_CONSTR0("#Q##Gnew()"),
+ ERR_CONSTR1("#Q##Gnew(X)");
+
+ String referenceTemplate;
+
+ ReferenceKind(String referenceTemplate) {
+ this.referenceTemplate = referenceTemplate;
+ }
+
+ String getReferenceString(QualifierKind qk, GenericKind gk) {
+ return referenceTemplate
+ .replaceAll("#Q", qk.qualifier)
+ .replaceAll("#G", gk.typeParameters);
+ }
+
+ boolean erroneous() {
+ switch (this) {
+ case ERR_SUPER:
+ case ERR_METH0:
+ case ERR_METH1:
+ case ERR_CONSTR0:
+ case ERR_CONSTR1:
+ return true;
+ default: return false;
+ }
+ }
+ }
+
+ enum GenericKind {
+ NONE(""),
+ ONE(""),
+ TWO("");
+
+ String typeParameters;
+
+ GenericKind(String typeParameters) {
+ this.typeParameters = typeParameters;
+ }
+ }
+
+ enum QualifierKind {
+ THIS("this"),
+ SUPER("super"),
+ NEW("new Foo()"),
+ METHOD("m()"),
+ FIELD("a.f"),
+ UBOUND_SIMPLE("A"),
+ UNBOUND_GENERIC1("A"),
+ UNBOUND_GENERIC2("A"),
+ UNBOUND_GENERIC3("A extends X, ? super Y>");
+
+ String qualifier;
+
+ QualifierKind(String qualifier) {
+ this.qualifier = qualifier;
+ }
+ }
+
+ enum ExprKind {
+ NONE("#R#S"),
+ SINGLE_PAREN1("(#R#S)"),
+ SINGLE_PAREN2("(#R)#S"),
+ DOUBLE_PAREN1("((#R#S))"),
+ DOUBLE_PAREN2("((#R)#S)"),
+ DOUBLE_PAREN3("((#R))#S");
+
+ String expressionTemplate;
+
+ ExprKind(String expressionTemplate) {
+ this.expressionTemplate = expressionTemplate;
+ }
+
+ String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
+ return expressionTemplate
+ .replaceAll("#R", rk.getReferenceString(qk, gk))
+ .replaceAll("#S", sk.subExpression);
+ }
+ }
+
+ enum SubExprKind {
+ NONE(""),
+ SELECT_FIELD(".f"),
+ SELECT_METHOD(".f()"),
+ SELECT_NEW(".new Foo()"),
+ POSTINC("++"),
+ POSTDEC("--");
+
+ String subExpression;
+
+ SubExprKind(String subExpression) {
+ this.subExpression = subExpression;
+ }
+ }
+
+ public static void main(String... args) throws Exception {
+
+ //create default shared JavaCompiler - reused across multiple compilations
+ JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
+ StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
+
+ for (ReferenceKind rk : ReferenceKind.values()) {
+ for (QualifierKind qk : QualifierKind.values()) {
+ for (GenericKind gk : GenericKind.values()) {
+ for (SubExprKind sk : SubExprKind.values()) {
+ for (ExprKind ek : ExprKind.values()) {
+ new MethodReferenceParserTest(rk, qk, gk, sk, ek).run(comp, fm);
+ }
+ }
+ }
+ }
+ }
+ System.out.println("Total check executed: " + checkCount);
+ }
+
+ ReferenceKind rk;
+ QualifierKind qk;
+ GenericKind gk;
+ SubExprKind sk;
+ ExprKind ek;
+ JavaSource source;
+ DiagnosticChecker diagChecker;
+
+ MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek) {
+ this.rk = rk;
+ this.qk = qk;
+ this.gk = gk;
+ this.sk = sk;
+ this.ek = ek;
+ this.source = new JavaSource();
+ this.diagChecker = new DiagnosticChecker();
+ }
+
+ class JavaSource extends SimpleJavaFileObject {
+
+ String template = "class Test {\n" +
+ " SAM s = #E;\n" +
+ "}";
+
+ String source;
+
+ public JavaSource() {
+ super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
+ source = template.replaceAll("#E", ek.expressionString(rk, qk, gk, sk));
+ }
+
+ @Override
+ public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+ return source;
+ }
+ }
+
+ void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
+ JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
+ Arrays.asList("-XDallowMethodReferences"), null, Arrays.asList(source));
+ try {
+ ct.parse();
+ } catch (Throwable ex) {
+ throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
+ }
+ check();
+ }
+
+ void check() {
+ checkCount++;
+
+ if (diagChecker.errorFound != rk.erroneous()) {
+ throw new Error("invalid diagnostics for source:\n" +
+ source.getCharContent(true) +
+ "\nFound error: " + diagChecker.errorFound +
+ "\nExpected error: " + rk.erroneous());
+ }
+ }
+
+ static class DiagnosticChecker implements javax.tools.DiagnosticListener {
+
+ boolean errorFound;
+
+ public void report(Diagnostic extends JavaFileObject> diagnostic) {
+ if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
+ errorFound = true;
+ }
+ }
+ }
+}
diff --git a/test/tools/javac/quid/T6999438.out b/test/tools/javac/quid/T6999438.out
index cd6266895d196d2b412862cdcce1aef4aeab6fce..04fdcfae97ff6a5ef12f0bfdf4f085615dd71f4c 100644
--- a/test/tools/javac/quid/T6999438.out
+++ b/test/tools/javac/quid/T6999438.out
@@ -1,4 +1,4 @@
-T6999438.java:8:9: compiler.err.illegal.char: 35
+T6999438.java:8:8: compiler.err.expected: token.identifier
T6999438.java:8:10: compiler.err.illegal.start.of.type
T6999438.java:8:25: compiler.err.expected: token.identifier
T6999438.java:8:26: compiler.err.expected: ';'