changeset 1451:03bdded4112f

Fix instance context lambda with class definition in lambda body. Also some clean-up.
author Robert Field <Robert.Field@oracle.com>
date Sat, 11 Aug 2012 12:34:21 -0700
parents ebb4ad86a284
children 4ae8b4be4d33
files src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
diffstat 1 files changed, 15 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Thu Aug 09 18:18:44 2012 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Sat Aug 11 12:34:21 2012 -0700
@@ -191,9 +191,10 @@
 
         //add captured locals
         for (Symbol fv : localContext.getSymbolMap(CAPTURED_VAR).keySet()) {
-            if (fv == localContext.self) continue;
-            JCTree captured_local = make.Ident(fv).setType(fv.type);
-            syntheticInits.append((JCExpression)captured_local);
+            if (fv != localContext.self) {
+                JCTree captured_local = make.Ident(fv).setType(fv.type);
+                syntheticInits.append((JCExpression) captured_local);
+            }
         }
 
         //then, determine the arguments to the indy call
@@ -273,18 +274,6 @@
     @Override
     public void visitIdent(JCIdent tree) {
         LambdaToMethodTranslationContext lambdaContext = (LambdaToMethodTranslationContext)context;
-        if (((LambdaToMethodAnalyzer)analyzer).new LambdaSuperFilter(context).accepts(tree)) {
-            for (Map.Entry<Symbol, Symbol> encl_entry : lambdaContext.getSymbolMap(LambdaSymbolKind.CAPTURED_THIS).entrySet()) {
-                if (tree.sym.owner.isMemberOf((ClassSymbol)encl_entry.getKey(), types)) {
-                    JCExpression enclRef = make.Ident(encl_entry.getValue());
-                    JCFieldAccess sel = make.Select(enclRef, tree.name);
-                    sel.setType(tree.type);
-                    sel.sym = tree.sym;
-                    result = sel;
-                    return;
-                }
-            }
-        }
         if (context == null || !analyzer.lambdaSymbolFilter(tree.sym)) {
             super.visitIdent(tree);
         } else {
@@ -553,7 +542,7 @@
                                         indyType,
                                         staticArgs.toArray());
 
-  JCFieldAccess qualifier = make.Select(make.QualIdent(site.tsym), bsmName);
+        JCFieldAccess qualifier = make.Select(make.QualIdent(site.tsym), bsmName);
         qualifier.sym = dynSym;
         qualifier.type = indyType.getReturnType();
 
@@ -619,8 +608,13 @@
         }
 
         @Override
-        public void visitIdent(JCIdent tree) {
-            super.visitIdent(tree);
+        public void visitClassDef(JCClassDecl tree) {
+            super.visitClassDef(tree);
+            if (frameStack.nonEmpty() && enclosingLambda() != null) {
+                // Any class defined within a lambda is an implicit 'this' reference
+                // because its constructor will reference the enclosing class
+                ((LambdaTranslationContext) context()).addSymbol(tree.sym.type.getEnclosingType().tsym, CAPTURED_THIS);
+            }
         }
 
         @Override
@@ -635,7 +629,7 @@
         protected boolean lambdaSymbolFilter(Symbol sym) {
             return (sym.kind == VAR || sym.kind == MTH)
                     && !sym.isStatic()
-                 /* && sym.name != names._super */
+                 // && sym.name != names._super // Unlike LambdaTranslator, we need to know about super references
                     && sym.name != names.init;
         }
 
@@ -680,26 +674,6 @@
             }
         };
 
-        /**
-         * This is used to filter out those super expressions that need to be
-         * qualified with an enclosing tree
-         */
-        protected final class LambdaSuperFilter extends LambdaToMethodFilter<JCIdent> {
-
-            LambdaSuperFilter(TranslationContext<?> context) {
-                super(context);
-            }
-
-            @Override
-            public boolean accepts(JCIdent tree) {
-                return context != null
-                        && tree.name == names._super
-                        && tree.sym.owner.kind == Kinds.TYP
-                        && tree.sym.owner.type.getEnclosingType().tag != TypeTags.NONE
-                        && hasEnclosingInstance(tree.sym.owner.type);
-            }
-        };
-
         class LambdaToMethodTranslationContext extends LambdaTranslationContext {
 
             LambdaToMethodTranslationContext(JCLambda tree) {
@@ -718,8 +692,8 @@
 
             @Override
             protected Type enclosingType() {
-                //local inner classes defined inside lambda are always static
-                return Type.noType;
+                //local inner classes defined inside a lambda are always non-static
+                return owner.enclClass().type;
             }
 
             protected Type generatedLambdaSig() {