changeset 1136:73baee568e9e

Clean up code for detecting whether a lambda expression should be allowed in given context
author mcimadamore
date Thu, 21 Jul 2011 23:32:35 -0700
parents 655f4ec01337
children b81b670567c4
files src/share/classes/com/sun/tools/javac/comp/Attr.java
diffstat 1 files changed, 6 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Jul 21 16:26:50 2011 -0700
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Jul 21 23:32:35 2011 -0700
@@ -2175,52 +2175,14 @@
 
     /**
      * Check whether a lambda expression/method reference is allowed in the current
-     * attribution environment - lambda/method references are only allowed in
-     * (i) cast conversion expressions, (ii) assignment expressions, (iii) method
-     * invocation expressions, (iv) return statements.
+     * attribution environment - lambda/method references are only allowed when
+     * the expected type is set or, alternatively, if we are in a nested poly
+     * context such as method calls and lambda return expressions
      */
     boolean lambdaOrReferenceAllowed(JCTree lambdaOrReference) {
-        if (env.tree.getTag() == JCTree.TYPECAST &&
-                ((JCTypeCast)env.tree).expr == lambdaOrReference) {
-            return true;
-        } else if (env.tree.getTag() == JCTree.APPLY) {
-            JCMethodInvocation apply = (JCMethodInvocation)env.tree;
-            for (JCTree arg : apply.args) {
-                if (arg == lambdaOrReference) {
-                    return true;
-                }
-            }
-            return false;
-        } else if (env.tree.getTag() == JCTree.NEWCLASS) {
-            JCNewClass newClazz = (JCNewClass)env.tree;
-            for (JCTree arg : newClazz.args) {
-                if (arg == lambdaOrReference) {
-                    return true;
-                }
-            }
-            return false;
-        } else if (env.tree.getTag() == JCTree.METHODDEF) {
-            JCBlock block = ((JCMethodDecl)env.tree).getBody();
-            for (JCStatement stat : block.stats) {
-                if (stat.getTag() == JCTree.RETURN &&
-                        ((JCReturn)stat).getExpression() == lambdaOrReference) {
-                    return true;
-                }
-            }
-            return false;
-        } else if (env.tree.getTag() == JCTree.NEWARRAY) {
-            JCNewArray newArray = (JCNewArray)env.tree;
-            if (newArray.elems != null) {
-                for (JCExpression elem : newArray.elems) {
-                    if (elem == lambdaOrReference) {
-                        return true;
-                    }
-                }
-            }
-            return false;
-        } else {
-            return pt.tag == CLASS;
-        }
+        return (pt.tag != NONE ||
+                pt == Infer.anyPoly ||
+                pt == Infer.lambdaPoly);
     }
     
     /*