langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
changeset 31296 9a8e44b62c77
parent 29772 4e4e74b99d27
child 33706 0d21ecb55e6a
equal deleted inserted replaced
31216:43d0179ee9de 31296:9a8e44b62c77
     1 /*
     1 /*
     2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
  1195                     tree.sym.owner = owner();
  1195                     tree.sym.owner = owner();
  1196                     if (tree.sym.hasOuterInstance()) {
  1196                     if (tree.sym.hasOuterInstance()) {
  1197                         //if a class is defined within a lambda, the lambda must capture
  1197                         //if a class is defined within a lambda, the lambda must capture
  1198                         //its enclosing instance (if any)
  1198                         //its enclosing instance (if any)
  1199                         TranslationContext<?> localContext = context();
  1199                         TranslationContext<?> localContext = context();
  1200                         while (localContext != null) {
  1200                         final TypeSymbol outerInstanceSymbol = tree.sym.type.getEnclosingType().tsym;
  1201                             if (localContext.tree.getTag() == LAMBDA) {
  1201                         while (localContext != null && !localContext.owner.isStatic()) {
       
  1202                             if (localContext.tree.hasTag(LAMBDA)) {
       
  1203                                 JCTree block = capturedDecl(localContext.depth, outerInstanceSymbol);
       
  1204                                 if (block == null) break;
  1202                                 ((LambdaTranslationContext)localContext)
  1205                                 ((LambdaTranslationContext)localContext)
  1203                                         .addSymbol(tree.sym.type.getEnclosingType().tsym, CAPTURED_THIS);
  1206                                         .addSymbol(outerInstanceSymbol, CAPTURED_THIS);
  1204                             }
  1207                             }
  1205                             localContext = localContext.prev;
  1208                             localContext = localContext.prev;
  1206                         }
  1209                         }
  1207                     }
  1210                     }
  1208                 }
  1211                 }
  1234                         }
  1237                         }
  1235                         localContext = localContext.prev;
  1238                         localContext = localContext.prev;
  1236                     }
  1239                     }
  1237                 } else if (tree.sym.owner.kind == TYP) {
  1240                 } else if (tree.sym.owner.kind == TYP) {
  1238                     TranslationContext<?> localContext = context();
  1241                     TranslationContext<?> localContext = context();
  1239                     while (localContext != null) {
  1242                     while (localContext != null  && !localContext.owner.isStatic()) {
  1240                         if (localContext.tree.hasTag(LAMBDA)) {
  1243                         if (localContext.tree.hasTag(LAMBDA)) {
  1241                             JCTree block = capturedDecl(localContext.depth, tree.sym);
  1244                             JCTree block = capturedDecl(localContext.depth, tree.sym);
  1242                             if (block == null) break;
  1245                             if (block == null) break;
  1243                             switch (block.getTag()) {
  1246                             switch (block.getTag()) {
  1244                                 case CLASSDEF:
  1247                                 case CLASSDEF:
  1310             TypeSymbol def = tree.type.tsym;
  1313             TypeSymbol def = tree.type.tsym;
  1311             boolean inReferencedClass = currentlyInClass(def);
  1314             boolean inReferencedClass = currentlyInClass(def);
  1312             boolean isLocal = def.isLocal();
  1315             boolean isLocal = def.isLocal();
  1313             if ((inReferencedClass && isLocal || lambdaNewClassFilter(context(), tree))) {
  1316             if ((inReferencedClass && isLocal || lambdaNewClassFilter(context(), tree))) {
  1314                 TranslationContext<?> localContext = context();
  1317                 TranslationContext<?> localContext = context();
  1315                 while (localContext != null) {
  1318                 final TypeSymbol outerInstanceSymbol = tree.type.getEnclosingType().tsym;
  1316                     if (localContext.tree.getTag() == LAMBDA) {
  1319                 while (localContext != null  && !localContext.owner.isStatic()) {
       
  1320                     if (localContext.tree.hasTag(LAMBDA)) {
       
  1321                         if (outerInstanceSymbol != null) {
       
  1322                             JCTree block = capturedDecl(localContext.depth, outerInstanceSymbol);
       
  1323                             if (block == null) break;
       
  1324                         }
  1317                         ((LambdaTranslationContext)localContext)
  1325                         ((LambdaTranslationContext)localContext)
  1318                                 .addSymbol(tree.type.getEnclosingType().tsym, CAPTURED_THIS);
  1326                                 .addSymbol(outerInstanceSymbol, CAPTURED_THIS);
  1319                     }
  1327                     }
  1320                     localContext = localContext.prev;
  1328                     localContext = localContext.prev;
  1321                 }
  1329                 }
  1322             }
  1330             }
  1323             if (context() != null && !inReferencedClass && isLocal) {
  1331             if (context() != null && !inReferencedClass && isLocal) {
  1402                         (tree.sym.name == names._this ||
  1410                         (tree.sym.name == names._this ||
  1403                          tree.sym.name == names._super)) {
  1411                          tree.sym.name == names._super)) {
  1404                 // A select of this or super means, if we are in a lambda,
  1412                 // A select of this or super means, if we are in a lambda,
  1405                 // we much have an instance context
  1413                 // we much have an instance context
  1406                 TranslationContext<?> localContext = context();
  1414                 TranslationContext<?> localContext = context();
  1407                 while (localContext != null) {
  1415                 while (localContext != null  && !localContext.owner.isStatic()) {
  1408                     if (localContext.tree.hasTag(LAMBDA)) {
  1416                     if (localContext.tree.hasTag(LAMBDA)) {
  1409                         JCClassDecl clazz = (JCClassDecl)capturedDecl(localContext.depth, tree.sym);
  1417                         JCClassDecl clazz = (JCClassDecl)capturedDecl(localContext.depth, tree.sym);
  1410                         if (clazz == null) break;
  1418                         if (clazz == null) break;
  1411                         ((LambdaTranslationContext)localContext).addSymbol(clazz.sym, CAPTURED_THIS);
  1419                         ((LambdaTranslationContext)localContext).addSymbol(clazz.sym, CAPTURED_THIS);
  1412                     }
  1420                     }
  1577             int currentDepth = frameStack.size() - 1;
  1585             int currentDepth = frameStack.size() - 1;
  1578             for (Frame block : frameStack) {
  1586             for (Frame block : frameStack) {
  1579                 switch (block.tree.getTag()) {
  1587                 switch (block.tree.getTag()) {
  1580                     case CLASSDEF:
  1588                     case CLASSDEF:
  1581                         ClassSymbol clazz = ((JCClassDecl)block.tree).sym;
  1589                         ClassSymbol clazz = ((JCClassDecl)block.tree).sym;
  1582                         if (sym.isMemberOf(clazz, types)) {
  1590                         if (clazz.isSubClass(sym, types) || sym.isMemberOf(clazz, types)) {
  1583                             return currentDepth > depth ? null : block.tree;
  1591                             return currentDepth > depth ? null : block.tree;
  1584                         }
  1592                         }
  1585                         break;
  1593                         break;
  1586                     case VARDEF:
  1594                     case VARDEF:
  1587                         if (((JCVariableDecl)block.tree).sym == sym &&
  1595                         if (((JCVariableDecl)block.tree).sym == sym &&