# HG changeset patch # User mcimadamore # Date 1322141780 0 # Node ID c896d95e74690ebb9c0851e2301ef6280f95ec5b # Parent 07599bd780cab1f40da7915e1dc6774629b0cf8c 7115046: Add AST node for lambda expressions Summary: Add tree nodes for representing lambda expressions and update relevant visitors interfaces Reviewed-by: jjg diff -r 07599bd780ca -r c896d95e7469 src/share/classes/com/sun/source/tree/LambdaExpressionTree.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/share/classes/com/sun/source/tree/LambdaExpressionTree.java Thu Nov 24 13:36:20 2011 +0000 @@ -0,0 +1,56 @@ +/* + * 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ + +package com.sun.source.tree; + +import java.util.List; + +/** + * A tree node for a lambda expression. + * + * For example: + *
+ * ()->{} + * (List+ */ +public interface LambdaExpressionTree extends ExpressionTree { + + /** + * Lambda expressions come in two forms: (i) expression lambdas, whose body + * is an expression, and (ii) statement lambdas, whose body is a block + */ + public enum BodyKind { + /** enum constant for expression lambdas */ + EXPRESSION, + /** enum constant for statement lambdas */ + STATEMENT; + } + + List extends VariableTree> getParameters(); + Tree getBody(); + BodyKind getBodyKind(); +} diff -r 07599bd780ca -r c896d95e7469 src/share/classes/com/sun/source/tree/Tree.java --- a/src/share/classes/com/sun/source/tree/Tree.java Sat Nov 19 15:54:04 2011 -0800 +++ b/src/share/classes/com/sun/source/tree/Tree.java Thu Nov 24 13:36:20 2011 +0000 @@ -187,6 +187,11 @@ NEW_CLASS(NewClassTree.class), /** + * Used for instances of {@link LambdaExpressionTree}. + */ + LAMBDA_EXPRESSION(LambdaExpressionTree.class), + + /** * Used for instances of {@link ParenthesizedTree}. */ PARENTHESIZED(ParenthesizedTree.class), diff -r 07599bd780ca -r c896d95e7469 src/share/classes/com/sun/source/tree/TreeVisitor.java --- a/src/share/classes/com/sun/source/tree/TreeVisitor.java Sat Nov 19 15:54:04 2011 -0800 +++ b/src/share/classes/com/sun/source/tree/TreeVisitor.java Thu Nov 24 13:36:20 2011 +0000 @@ -85,6 +85,7 @@ R visitModifiers(ModifiersTree node, P p); R visitNewArray(NewArrayTree node, P p); R visitNewClass(NewClassTree node, P p); + R visitLambdaExpression(LambdaExpressionTree node, P p); R visitParenthesized(ParenthesizedTree node, P p); R visitReturn(ReturnTree node, P p); R visitMemberSelect(MemberSelectTree node, P p); diff -r 07599bd780ca -r c896d95e7469 src/share/classes/com/sun/source/util/SimpleTreeVisitor.java --- a/src/share/classes/com/sun/source/util/SimpleTreeVisitor.java Sat Nov 19 15:54:04 2011 -0800 +++ b/src/share/classes/com/sun/source/util/SimpleTreeVisitor.java Thu Nov 24 13:36:20 2011 +0000 @@ -172,6 +172,10 @@ return defaultAction(node, p); } + public R visitLambdaExpression(LambdaExpressionTree node, P p) { + return defaultAction(node, p); + } + public R visitParenthesized(ParenthesizedTree node, P p) { return defaultAction(node, p); } diff -r 07599bd780ca -r c896d95e7469 src/share/classes/com/sun/source/util/TreeScanner.java --- a/src/share/classes/com/sun/source/util/TreeScanner.java Sat Nov 19 15:54:04 2011 -0800 +++ b/src/share/classes/com/sun/source/util/TreeScanner.java Thu Nov 24 13:36:20 2011 +0000 @@ -285,6 +285,12 @@ return r; } + public R visitLambdaExpression(LambdaExpressionTree node, P p) { + R r = scan(node.getParameters(), p); + r = scanAndReduce(node.getBody(), p, r); + return r; + } + public R visitParenthesized(ParenthesizedTree node, P p) { return scan(node.getExpression(), p); } diff -r 07599bd780ca -r c896d95e7469 src/share/classes/com/sun/tools/javac/tree/JCTree.java --- a/src/share/classes/com/sun/tools/javac/tree/JCTree.java Sat Nov 19 15:54:04 2011 -0800 +++ b/src/share/classes/com/sun/tools/javac/tree/JCTree.java Thu Nov 24 13:36:20 2011 +0000 @@ -41,6 +41,7 @@ import com.sun.tools.javac.code.Symbol.*; import com.sun.tools.javac.parser.EndPosTable; import com.sun.source.tree.*; +import com.sun.source.tree.LambdaExpressionTree.BodyKind; import static com.sun.tools.javac.code.BoundKind.*; import static com.sun.tools.javac.tree.JCTree.Tag.*; @@ -198,6 +199,10 @@ */ NEWARRAY, + /** Lambda expression, of type Lambda. + */ + LAMBDA, + /** Parenthesized subexpressions, of type Parens. */ PARENS, @@ -1487,6 +1492,56 @@ } /** + * A lambda expression. + */ + public static class JCLambda extends JCExpression implements LambdaExpressionTree { + + public Listls)->ls.size() + * (x,y)-> { return x + y; } + *