8129740: Incorrect class file created when passing lambda in inner class constructor

Wed, 25 May 2016 15:09:13 +0530

author
sadayapalam
date
Wed, 25 May 2016 15:09:13 +0530
changeset 3172
921a7d6ab90d
parent 3171
d203dcc5cd96
child 3173
60a0572cd449

8129740: Incorrect class file created when passing lambda in inner class constructor
Summary: Lambda implementation method must be static when lambda is an argument to an explicit constructor call.
Reviewed-by: mcimadamore

src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/tree/TreeMaker.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/util/Names.java file | annotate | diff | comparison | revisions
test/tools/javac/lambda/T8129740/AllowEnclosingVarCaptureTest.java file | annotate | diff | comparison | revisions
test/tools/javac/lambda/T8129740/CaptureInCtorChainingTest.java file | annotate | diff | comparison | revisions
test/tools/javac/lambda/T8129740/QualifiedThisAccessTest.java file | annotate | diff | comparison | revisions
test/tools/javac/lambda/T8129740/SourceForTranslation.java file | annotate | diff | comparison | revisions
test/tools/javac/lambda/T8129740/SourceToSourceTranslationTest.java file | annotate | diff | comparison | revisions
test/tools/javac/lambda/T8129740/Universe.java.out file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Tue Apr 26 13:31:41 2016 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Wed May 25 15:09:13 2016 +0530
     1.3 @@ -338,6 +338,11 @@
     1.4                  syntheticInits.append((JCExpression) captured_local);
     1.5              }
     1.6          }
     1.7 +        // add captured outer this instances (used only when `this' capture itself is illegal)
     1.8 +        for (Symbol fv : localContext.getSymbolMap(CAPTURED_OUTER_THIS).keySet()) {
     1.9 +            JCTree captured_local = make.QualThis(fv.type);
    1.10 +            syntheticInits.append((JCExpression) captured_local);
    1.11 +        }
    1.12  
    1.13          //then, determine the arguments to the indy call
    1.14          List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev);
    1.15 @@ -434,6 +439,32 @@
    1.16          }
    1.17      }
    1.18  
    1.19 +    /**
    1.20 +     * Translate qualified `this' references within a lambda to the mapped identifier
    1.21 +     * @param tree
    1.22 +     */
    1.23 +    @Override
    1.24 +    public void visitSelect(JCFieldAccess tree) {
    1.25 +        if (context == null || !analyzer.lambdaFieldAccessFilter(tree)) {
    1.26 +            super.visitSelect(tree);
    1.27 +        } else {
    1.28 +            int prevPos = make.pos;
    1.29 +            try {
    1.30 +                make.at(tree);
    1.31 +
    1.32 +                LambdaTranslationContext lambdaContext = (LambdaTranslationContext) context;
    1.33 +                JCTree ltree = lambdaContext.translate(tree);
    1.34 +                if (ltree != null) {
    1.35 +                    result = ltree;
    1.36 +                } else {
    1.37 +                    super.visitSelect(tree);
    1.38 +                }
    1.39 +            } finally {
    1.40 +                make.at(prevPos);
    1.41 +            }
    1.42 +        }
    1.43 +    }
    1.44 +
    1.45      @Override
    1.46      public void visitVarDef(JCVariableDecl tree) {
    1.47          LambdaTranslationContext lambdaContext = (LambdaTranslationContext)context;
    1.48 @@ -1126,6 +1157,11 @@
    1.49          private int lambdaCount = 0;
    1.50  
    1.51          /**
    1.52 +         * List of types undergoing construction via explicit constructor chaining.
    1.53 +         */
    1.54 +        private List<ClassSymbol> typesUnderConstruction;
    1.55 +
    1.56 +        /**
    1.57           * keep the count of lambda expression defined in given context (used to
    1.58           * generate unambiguous names for serializable lambdas)
    1.59           */
    1.60 @@ -1156,11 +1192,36 @@
    1.61  
    1.62          private JCClassDecl analyzeAndPreprocessClass(JCClassDecl tree) {
    1.63              frameStack = List.nil();
    1.64 +            typesUnderConstruction = List.nil();
    1.65              localClassDefs = new HashMap<Symbol, JCClassDecl>();
    1.66              return translate(tree);
    1.67          }
    1.68  
    1.69          @Override
    1.70 +        public void visitApply(JCMethodInvocation tree) {
    1.71 +            List<ClassSymbol> previousNascentTypes = typesUnderConstruction;
    1.72 +            try {
    1.73 +                Name methName = TreeInfo.name(tree.meth);
    1.74 +                if (methName == names._this || methName == names._super) {
    1.75 +                    typesUnderConstruction = typesUnderConstruction.prepend(currentClass());
    1.76 +                }
    1.77 +                super.visitApply(tree);
    1.78 +            } finally {
    1.79 +                typesUnderConstruction = previousNascentTypes;
    1.80 +            }
    1.81 +        }
    1.82 +            // where
    1.83 +            private ClassSymbol currentClass() {
    1.84 +                for (Frame frame : frameStack) {
    1.85 +                    if (frame.tree.hasTag(JCTree.Tag.CLASSDEF)) {
    1.86 +                        JCClassDecl cdef = (JCClassDecl) frame.tree;
    1.87 +                        return cdef.sym;
    1.88 +                    }
    1.89 +                }
    1.90 +                return null;
    1.91 +            }
    1.92 +
    1.93 +        @Override
    1.94          public void visitBlock(JCBlock tree) {
    1.95              List<Frame> prevStack = frameStack;
    1.96              try {
    1.97 @@ -1624,6 +1685,22 @@
    1.98          }
    1.99  
   1.100          /**
   1.101 +         *  This is used to filter out those select nodes that need to be adjusted
   1.102 +         *  when translating away lambda expressions - at the moment, this is the
   1.103 +         *  set of nodes that select `this' (qualified this)
   1.104 +         */
   1.105 +        private boolean lambdaFieldAccessFilter(JCFieldAccess fAccess) {
   1.106 +            LambdaTranslationContext lambdaContext =
   1.107 +                    context instanceof LambdaTranslationContext ?
   1.108 +                            (LambdaTranslationContext) context : null;
   1.109 +            return lambdaContext != null
   1.110 +                    && !fAccess.sym.isStatic()
   1.111 +                    && fAccess.name == names._this
   1.112 +                    && (fAccess.sym.owner.kind == TYP)
   1.113 +                    && !lambdaContext.translatedSymbols.get(CAPTURED_OUTER_THIS).isEmpty();
   1.114 +        }
   1.115 +
   1.116 +        /**
   1.117           * This is used to filter out those new class expressions that need to
   1.118           * be qualified with an enclosing tree
   1.119           */
   1.120 @@ -1797,6 +1874,7 @@
   1.121                  translatedSymbols.put(LOCAL_VAR, new LinkedHashMap<Symbol, Symbol>());
   1.122                  translatedSymbols.put(CAPTURED_VAR, new LinkedHashMap<Symbol, Symbol>());
   1.123                  translatedSymbols.put(CAPTURED_THIS, new LinkedHashMap<Symbol, Symbol>());
   1.124 +                translatedSymbols.put(CAPTURED_OUTER_THIS, new LinkedHashMap<Symbol, Symbol>());
   1.125                  translatedSymbols.put(TYPE_VAR, new LinkedHashMap<Symbol, Symbol>());
   1.126  
   1.127                  freeVarProcessedLocalClasses = new HashSet<>();
   1.128 @@ -1909,6 +1987,16 @@
   1.129                              }
   1.130                          };
   1.131                          break;
   1.132 +                    case CAPTURED_OUTER_THIS:
   1.133 +                        Name name = names.fromString(new String(sym.flatName().toString() + names.dollarThis));
   1.134 +                        ret = new VarSymbol(SYNTHETIC | FINAL | PARAMETER, name, types.erasure(sym.type), translatedSym) {
   1.135 +                            @Override
   1.136 +                            public Symbol baseSymbol() {
   1.137 +                                //keep mapping with original captured symbol
   1.138 +                                return sym;
   1.139 +                            }
   1.140 +                        };
   1.141 +                        break;
   1.142                      case LOCAL_VAR:
   1.143                          ret = new VarSymbol(sym.flags() & FINAL, sym.name, sym.type, translatedSym);
   1.144                          ((VarSymbol) ret).pos = ((VarSymbol) sym).pos;
   1.145 @@ -1929,6 +2017,14 @@
   1.146              }
   1.147  
   1.148              void addSymbol(Symbol sym, LambdaSymbolKind skind) {
   1.149 +                if (skind == CAPTURED_THIS && sym != null && sym.kind == TYP && !typesUnderConstruction.isEmpty()) {
   1.150 +                    ClassSymbol currentClass = currentClass();
   1.151 +                    if (currentClass != null && typesUnderConstruction.contains(currentClass)) {
   1.152 +                        // reference must be to enclosing outer instance, mutate capture kind.
   1.153 +                        Assert.check(sym != currentClass); // should have been caught right in Attr
   1.154 +                        skind = CAPTURED_OUTER_THIS;
   1.155 +                    }
   1.156 +                }
   1.157                  Map<Symbol, Symbol> transMap = getSymbolMap(skind);
   1.158                  if (!transMap.containsKey(sym)) {
   1.159                      transMap.put(sym, translate(sym, skind));
   1.160 @@ -1942,17 +2038,49 @@
   1.161              }
   1.162  
   1.163              JCTree translate(JCIdent lambdaIdent) {
   1.164 -                for (Map<Symbol, Symbol> m : translatedSymbols.values()) {
   1.165 -                    if (m.containsKey(lambdaIdent.sym)) {
   1.166 -                        Symbol tSym = m.get(lambdaIdent.sym);
   1.167 -                        JCTree t = make.Ident(tSym).setType(lambdaIdent.type);
   1.168 -                        tSym.setTypeAttributes(lambdaIdent.sym.getRawTypeAttributes());
   1.169 -                        return t;
   1.170 +                for (LambdaSymbolKind kind : LambdaSymbolKind.values()) {
   1.171 +                    Map<Symbol, Symbol> m = getSymbolMap(kind);
   1.172 +                    switch(kind) {
   1.173 +                        default:
   1.174 +                            if (m.containsKey(lambdaIdent.sym)) {
   1.175 +                                Symbol tSym = m.get(lambdaIdent.sym);
   1.176 +                                JCTree t = make.Ident(tSym).setType(lambdaIdent.type);
   1.177 +                                tSym.setTypeAttributes(lambdaIdent.sym.getRawTypeAttributes());
   1.178 +                                return t;
   1.179 +                            }
   1.180 +                            break;
   1.181 +                        case CAPTURED_OUTER_THIS:
   1.182 +                            if (lambdaIdent.sym.owner.kind == TYP && m.containsKey(lambdaIdent.sym.owner)) {
   1.183 +                                // Transform outer instance variable references anchoring them to the captured synthetic.
   1.184 +                                Symbol tSym = m.get(lambdaIdent.sym.owner);
   1.185 +                                JCExpression t = make.Ident(tSym).setType(lambdaIdent.sym.owner.type);
   1.186 +                                tSym.setTypeAttributes(lambdaIdent.sym.owner.getRawTypeAttributes());
   1.187 +                                t = make.Select(t, lambdaIdent.name);
   1.188 +                                t.setType(lambdaIdent.type);
   1.189 +                                TreeInfo.setSymbol(t, lambdaIdent.sym);
   1.190 +                                return t;
   1.191 +                            }
   1.192 +                            break;
   1.193                      }
   1.194                  }
   1.195                  return null;
   1.196              }
   1.197  
   1.198 +            /* Translate away qualified this expressions, anchoring them to synthetic parameters that
   1.199 +               capture the qualified this handle. `fieldAccess' is guaranteed to one such.
   1.200 +            */
   1.201 +            public JCTree translate(JCFieldAccess fieldAccess) {
   1.202 +                Assert.check(fieldAccess.name == names._this);
   1.203 +                Map<Symbol, Symbol> m = translatedSymbols.get(LambdaSymbolKind.CAPTURED_OUTER_THIS);
   1.204 +                if (m.containsKey(fieldAccess.sym.owner)) {
   1.205 +                    Symbol tSym = m.get(fieldAccess.sym.owner);
   1.206 +                    JCExpression t = make.Ident(tSym).setType(fieldAccess.sym.owner.type);
   1.207 +                    tSym.setTypeAttributes(fieldAccess.sym.owner.getRawTypeAttributes());
   1.208 +                    return t;
   1.209 +                }
   1.210 +                return null;
   1.211 +            }
   1.212 +
   1.213              /**
   1.214               * The translatedSym is not complete/accurate until the analysis is
   1.215               * finished.  Once the analysis is finished, the translatedSym is
   1.216 @@ -1990,6 +2118,10 @@
   1.217                      params.append(make.VarDef((VarSymbol) thisSym, null));
   1.218                      parameterSymbols.append((VarSymbol) thisSym);
   1.219                  }
   1.220 +                for (Symbol thisSym : getSymbolMap(CAPTURED_OUTER_THIS).values()) {
   1.221 +                    params.append(make.VarDef((VarSymbol) thisSym, null));
   1.222 +                    parameterSymbols.append((VarSymbol) thisSym);
   1.223 +                }
   1.224                  for (Symbol thisSym : getSymbolMap(PARAM).values()) {
   1.225                      params.append(make.VarDef((VarSymbol) thisSym, null));
   1.226                      parameterSymbols.append((VarSymbol) thisSym);
   1.227 @@ -2138,6 +2270,7 @@
   1.228          LOCAL_VAR,      // original to translated lambda locals
   1.229          CAPTURED_VAR,   // variables in enclosing scope to translated synthetic parameters
   1.230          CAPTURED_THIS,  // class symbols to translated synthetic parameters (for captured member access)
   1.231 +        CAPTURED_OUTER_THIS, // used when `this' capture is illegal, but outer this capture is legit (JDK-8129740)
   1.232          TYPE_VAR;       // original to translated lambda type variables
   1.233      }
   1.234  
     2.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java	Tue Apr 26 13:31:41 2016 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java	Wed May 25 15:09:13 2016 +0530
     2.3 @@ -621,6 +621,12 @@
     2.4          return Ident(new VarSymbol(FINAL, names._this, t, t.tsym));
     2.5      }
     2.6  
     2.7 +    /** Create a tree representing qualified `this' given its type
     2.8 +     */
     2.9 +    public JCExpression QualThis(Type t) {
    2.10 +        return Select(Type(t), new VarSymbol(FINAL, names._this, t, t.tsym));
    2.11 +    }
    2.12 +
    2.13      /** Create a tree representing a class literal.
    2.14       */
    2.15      public JCExpression ClassLiteral(ClassSymbol clazz) {
     3.1 --- a/src/share/classes/com/sun/tools/javac/util/Names.java	Tue Apr 26 13:31:41 2016 -0700
     3.2 +++ b/src/share/classes/com/sun/tools/javac/util/Names.java	Wed May 25 15:09:13 2016 +0530
     3.3 @@ -176,6 +176,7 @@
     3.4      public final Name lambda;
     3.5      public final Name metafactory;
     3.6      public final Name altMetafactory;
     3.7 +    public final Name dollarThis;
     3.8  
     3.9      public final Name.Table table;
    3.10  
    3.11 @@ -234,6 +235,7 @@
    3.12          value = fromString("value");
    3.13          valueOf = fromString("valueOf");
    3.14          values = fromString("values");
    3.15 +        dollarThis = fromString("$this");
    3.16  
    3.17          // class names
    3.18          java_io_Serializable = fromString("java.io.Serializable");
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/lambda/T8129740/AllowEnclosingVarCaptureTest.java	Wed May 25 15:09:13 2016 +0530
     4.3 @@ -0,0 +1,59 @@
     4.4 +/*
     4.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.
    4.11 + *
    4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.15 + * version 2 for more details (a copy is included in the LICENSE file that
    4.16 + * accompanied this code).
    4.17 + *
    4.18 + * You should have received a copy of the GNU General Public License version
    4.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.21 + *
    4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.23 + * or visit www.oracle.com if you need additional information or have any
    4.24 + * questions.
    4.25 + */
    4.26 +
    4.27 +/*
    4.28 + * @test
    4.29 + * @bug 8129740 8133111 8157142
    4.30 + * @summary Incorrect class file created when passing lambda in inner class constructor
    4.31 + * @run main AllowEnclosingVarCaptureTest
    4.32 + */
    4.33 +
    4.34 +public class AllowEnclosingVarCaptureTest {
    4.35 +
    4.36 +    int var = 0;
    4.37 +
    4.38 +    void foo() {
    4.39 +        var *= 2;
    4.40 +    }
    4.41 +
    4.42 +    public static void main(String[] args) {
    4.43 +        new AllowEnclosingVarCaptureTest().new Inner(9764);
    4.44 +    }
    4.45 +
    4.46 +    public class Inner {
    4.47 +        public Inner(Runnable r) {
    4.48 +            r.run();
    4.49 +            if (var != 66704)
    4.50 +                throw new AssertionError("Unexpected output: " + var);
    4.51 +        }
    4.52 +
    4.53 +        public Inner(int x) {
    4.54 +            this(() -> {
    4.55 +                var = x + 1234;
    4.56 +                AllowEnclosingVarCaptureTest.this.var += 5678;
    4.57 +                foo();
    4.58 +                AllowEnclosingVarCaptureTest.this.foo();
    4.59 +            });
    4.60 +        }
    4.61 +    }
    4.62 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/tools/javac/lambda/T8129740/CaptureInCtorChainingTest.java	Wed May 25 15:09:13 2016 +0530
     5.3 @@ -0,0 +1,54 @@
     5.4 +/*
     5.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5.7 + *
     5.8 + * This code is free software; you can redistribute it and/or modify it
     5.9 + * under the terms of the GNU General Public License version 2 only, as
    5.10 + * published by the Free Software Foundation.
    5.11 + *
    5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    5.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    5.15 + * version 2 for more details (a copy is included in the LICENSE file that
    5.16 + * accompanied this code).
    5.17 + *
    5.18 + * You should have received a copy of the GNU General Public License version
    5.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    5.21 + *
    5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    5.23 + * or visit www.oracle.com if you need additional information or have any
    5.24 + * questions.
    5.25 + */
    5.26 +
    5.27 +/*
    5.28 + * @test
    5.29 + * @bug 8129740 8133111 8157142
    5.30 + * @summary Incorrect class file created when passing lambda in inner class constructor
    5.31 + * @run main CaptureInCtorChainingTest
    5.32 + */
    5.33 +
    5.34 +import java.util.function.Consumer;
    5.35 +import java.util.function.Function;
    5.36 +
    5.37 +public class CaptureInCtorChainingTest {
    5.38 +
    5.39 +    CaptureInCtorChainingTest(Function<Function<Function<Consumer<Void>, Void>, Void>, Void> innerClass) {
    5.40 +        new InnerClass(innerClass);
    5.41 +    }
    5.42 +
    5.43 +    void foo(Void v) { }
    5.44 +
    5.45 +    class InnerClass {
    5.46 +
    5.47 +        InnerClass(Function<Function<Function<Consumer<Void>, Void>, Void>, Void> factory) {
    5.48 +            this(factory.apply(o -> o.apply(CaptureInCtorChainingTest.this::foo)));
    5.49 +        }
    5.50 +
    5.51 +        InnerClass(Void unused) { }
    5.52 +    }
    5.53 +
    5.54 +    public static void main(String[] args) {
    5.55 +        new CaptureInCtorChainingTest(o -> null);
    5.56 +    }
    5.57 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/tools/javac/lambda/T8129740/QualifiedThisAccessTest.java	Wed May 25 15:09:13 2016 +0530
     6.3 @@ -0,0 +1,157 @@
     6.4 +/*
     6.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     6.7 + *
     6.8 + * This code is free software; you can redistribute it and/or modify it
     6.9 + * under the terms of the GNU General Public License version 2 only, as
    6.10 + * published by the Free Software Foundation.
    6.11 + *
    6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    6.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    6.15 + * version 2 for more details (a copy is included in the LICENSE file that
    6.16 + * accompanied this code).
    6.17 + *
    6.18 + * You should have received a copy of the GNU General Public License version
    6.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    6.21 + *
    6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    6.23 + * or visit www.oracle.com if you need additional information or have any
    6.24 + * questions.
    6.25 + */
    6.26 +
    6.27 +/*
    6.28 + * @test
    6.29 + * @bug 8129740 8133111 8157142
    6.30 + * @summary Incorrect class file created when passing lambda in inner class constructor
    6.31 + * @run main QualifiedThisAccessTest
    6.32 + */
    6.33 +
    6.34 +public class QualifiedThisAccessTest { // Not referenced by lambda, so should not be captured.
    6.35 +
    6.36 +    public class Universe { // Not referenced by lambda, so should not be captured.
    6.37 +
    6.38 +        public String name;
    6.39 +        public int galaxiesCount;
    6.40 +
    6.41 +        public Universe(String name, int galaxiesCount) {
    6.42 +            this.name = name;
    6.43 +            this.galaxiesCount = galaxiesCount;
    6.44 +        }
    6.45 +
    6.46 +        public String toString() {
    6.47 +            return "Universe" + name + " of " + galaxiesCount + " galaxies";
    6.48 +        }
    6.49 +
    6.50 +        class Galaxy {
    6.51 +
    6.52 +            String name;
    6.53 +            private int starsCount;
    6.54 +
    6.55 +            Galaxy(String name, int starsCount) {
    6.56 +                this.name = name;
    6.57 +                this.starsCount = starsCount;
    6.58 +            }
    6.59 +
    6.60 +            public String toString() {
    6.61 +                return "galaxy " + name + " of " + starsCount + " solar systems";
    6.62 +            }
    6.63 +
    6.64 +            int starsCount() {
    6.65 +                return starsCount;
    6.66 +            }
    6.67 +
    6.68 +            private String name() {
    6.69 +                return name;
    6.70 +            }
    6.71 +
    6.72 +            class SolarSystem {
    6.73 +
    6.74 +                String name;
    6.75 +                int planetsCount;
    6.76 +
    6.77 +                SolarSystem(String name, int planetsCount) {
    6.78 +                    this.name = name;
    6.79 +                    this.planetsCount = planetsCount;
    6.80 +                }
    6.81 +
    6.82 +                public String toString() {
    6.83 +                    return "Solar System of " + name + " with " + planetsCount + " planets";
    6.84 +                }
    6.85 +
    6.86 +                int planetsCount() {
    6.87 +                    return planetsCount;
    6.88 +                }
    6.89 +
    6.90 +                SolarSystem copy(SolarSystem s) {
    6.91 +                    return s;
    6.92 +                }
    6.93 +
    6.94 +                class Planet {
    6.95 +
    6.96 +                    String name;
    6.97 +                    int moonsCount;
    6.98 +
    6.99 +                    Planet(String name, int moonsCount, Runnable r) {
   6.100 +                        this.name = name;
   6.101 +                        this.moonsCount = moonsCount;
   6.102 +                        r.run();
   6.103 +                    }
   6.104 +                    Planet (String name, int moonsCount) {
   6.105 +                        this(name, moonsCount, ()-> {
   6.106 +                            StringBuffer buf = new StringBuffer();
   6.107 +                            buf.append("This planet belongs to the galaxy "
   6.108 +                                        + Galaxy.this.name + " with " + starsCount + " stars\n");
   6.109 +                            buf.append("This planet belongs to the galaxy "
   6.110 +                                        + Universe.Galaxy.this.name + " with " + starsCount() + " stars\n");
   6.111 +                            buf.append("This planet belongs to the galaxy "
   6.112 +                                        + Galaxy.this.name() + " with " + starsCount() + " stars\n");
   6.113 +                            buf.append("This planet belongs to the galaxy "
   6.114 +                                        + Universe.Galaxy.this.name() + " with "
   6.115 +                                        + (Universe.Galaxy.this).starsCount() + " stars\n");
   6.116 +
   6.117 +                            buf.append("This planet belongs to the solar system "
   6.118 +                                        + SolarSystem.this.name + " with " + planetsCount + " planets\n");
   6.119 +                            buf.append("This planet belongs to the solar system "
   6.120 +                                        + Galaxy.SolarSystem.this.name + " with " + planetsCount() + " planets\n");
   6.121 +                            buf.append("This planet belongs to the solar system "
   6.122 +                                        + (SolarSystem.this).name + " with " + planetsCount + " planets\n");
   6.123 +                            buf.append("This planet belongs to the solar system "
   6.124 +                                        + Universe.Galaxy.SolarSystem.this.name + " with "
   6.125 +                                        + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
   6.126 +                            buf.append("This planet belongs to the solar system "
   6.127 +                                        + Universe.Galaxy.SolarSystem.this.name.toLowerCase().toUpperCase()
   6.128 +                                        + " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
   6.129 +                            buf.append("This planet belongs to the solar system "
   6.130 +                                        + copy(Universe.Galaxy.SolarSystem.this).name.toLowerCase().toUpperCase()
   6.131 +                                        + " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
   6.132 +                            if (!buf.toString().equals(output))
   6.133 +                                throw new AssertionError("Unexpected value\n" + buf);
   6.134 +                        });
   6.135 +                    }
   6.136 +
   6.137 +                    static final String output =
   6.138 +                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
   6.139 +                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
   6.140 +                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
   6.141 +                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
   6.142 +                        "This planet belongs to the solar system Sun with 9 planets\n" +
   6.143 +                        "This planet belongs to the solar system Sun with 9 planets\n" +
   6.144 +                        "This planet belongs to the solar system Sun with 9 planets\n" +
   6.145 +                        "This planet belongs to the solar system Sun with 9 planets\n" +
   6.146 +                        "This planet belongs to the solar system SUN with 9 planets\n" +
   6.147 +                        "This planet belongs to the solar system SUN with 9 planets\n";
   6.148 +
   6.149 +
   6.150 +                    public String toString() {
   6.151 +                        return "Planet " + name + " with " + moonsCount + " moon(s)";
   6.152 +                    }
   6.153 +                }
   6.154 +            }
   6.155 +        }
   6.156 +    }
   6.157 +    public static void main(String[] args) {
   6.158 +        new QualifiedThisAccessTest().new Universe("Universe", 12345678).new Galaxy("Mily way", 23456789).new SolarSystem("Sun", 9).new Planet("Earth", 1);
   6.159 +    }
   6.160 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/tools/javac/lambda/T8129740/SourceForTranslation.java	Wed May 25 15:09:13 2016 +0530
     7.3 @@ -0,0 +1,122 @@
     7.4 +    class Universe { // Not referenced by lambda, so should not be captured.
     7.5 +
     7.6 +        public String name;
     7.7 +        public int galaxiesCount;
     7.8 +
     7.9 +        public Universe(String name, int galaxiesCount) {
    7.10 +            this.name = name;
    7.11 +            this.galaxiesCount = galaxiesCount;
    7.12 +        }
    7.13 +
    7.14 +        public String toString() {
    7.15 +            return "Universe" + name + " of " + galaxiesCount + " galaxies";
    7.16 +        }
    7.17 +
    7.18 +        class Galaxy {
    7.19 +
    7.20 +            String name;
    7.21 +            private int starsCount;
    7.22 +
    7.23 +            Galaxy(String name, int starsCount) {
    7.24 +                this.name = name;
    7.25 +                this.starsCount = starsCount;
    7.26 +            }
    7.27 +
    7.28 +            public String toString() {
    7.29 +                return "galaxy " + name + " of " + starsCount + " solar systems";
    7.30 +            }
    7.31 +
    7.32 +            int starsCount() {
    7.33 +                return starsCount;
    7.34 +            }
    7.35 +
    7.36 +            private String name() {
    7.37 +                return name;
    7.38 +            }
    7.39 +
    7.40 +            class SolarSystem {
    7.41 +
    7.42 +                String name;
    7.43 +                int planetsCount;
    7.44 +
    7.45 +                SolarSystem(String name, int planetsCount) {
    7.46 +                    this.name = name;
    7.47 +                    this.planetsCount = planetsCount;
    7.48 +                }
    7.49 +
    7.50 +                public String toString() {
    7.51 +                    return "Solar System of " + name + " with " + planetsCount + " planets";
    7.52 +                }
    7.53 +
    7.54 +                int planetsCount() {
    7.55 +                    return planetsCount;
    7.56 +                }
    7.57 +
    7.58 +                SolarSystem copy(SolarSystem s) {
    7.59 +                    return s;
    7.60 +                }
    7.61 +
    7.62 +                class Planet {
    7.63 +
    7.64 +                    String name;
    7.65 +                    int moonsCount;
    7.66 +
    7.67 +                    Planet(String name, int moonsCount, Runnable r) {
    7.68 +                        this.name = name;
    7.69 +                        this.moonsCount = moonsCount;
    7.70 +                        r.run();
    7.71 +                    }
    7.72 +                    Planet (String name, int moonsCount) {
    7.73 +                        this(name, moonsCount, ()-> {
    7.74 +                            String n = name;
    7.75 +                            StringBuffer buf = new StringBuffer();
    7.76 +                            buf.append("This planet belongs to the galaxy "
    7.77 +                                        + Galaxy.this.name + " with " + starsCount + " stars\n");
    7.78 +                            buf.append("This planet belongs to the galaxy "
    7.79 +                                        + Universe.Galaxy.this.name + " with " + starsCount() + " stars\n");
    7.80 +                            buf.append("This planet belongs to the galaxy "
    7.81 +                                        + Galaxy.this.name() + " with " + starsCount() + " stars\n");
    7.82 +                            buf.append("This planet belongs to the galaxy "
    7.83 +                                        + Universe.Galaxy.this.name() + " with "
    7.84 +                                        + (Universe.Galaxy.this).starsCount() + " stars\n");
    7.85 +
    7.86 +                            buf.append("This planet belongs to the solar system "
    7.87 +                                        + SolarSystem.this.name + " with " + planetsCount + " planets\n");
    7.88 +                            buf.append("This planet belongs to the solar system "
    7.89 +                                        + Galaxy.SolarSystem.this.name + " with " + planetsCount() + " planets\n");
    7.90 +                            buf.append("This planet belongs to the solar system "
    7.91 +                                        + (SolarSystem.this).name + " with " + planetsCount + " planets\n");
    7.92 +                            buf.append("This planet belongs to the solar system "
    7.93 +                                        + Universe.Galaxy.SolarSystem.this.name + " with "
    7.94 +                                        + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
    7.95 +                            buf.append("This planet belongs to the solar system "
    7.96 +                                        + Universe.Galaxy.SolarSystem.this.name.toLowerCase().toUpperCase()
    7.97 +                                        + " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
    7.98 +                            buf.append("This planet belongs to the solar system "
    7.99 +                                        + copy(Universe.Galaxy.SolarSystem.this).name.toLowerCase().toUpperCase()
   7.100 +                                        + " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
   7.101 +                            if (!buf.toString().equals(output))
   7.102 +                                throw new AssertionError("Unexpected value\n" + buf);
   7.103 +                        });
   7.104 +                    }
   7.105 +
   7.106 +                    static final String output =
   7.107 +                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
   7.108 +                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
   7.109 +                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
   7.110 +                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
   7.111 +                        "This planet belongs to the solar system Sun with 9 planets\n" +
   7.112 +                        "This planet belongs to the solar system Sun with 9 planets\n" +
   7.113 +                        "This planet belongs to the solar system Sun with 9 planets\n" +
   7.114 +                        "This planet belongs to the solar system Sun with 9 planets\n" +
   7.115 +                        "This planet belongs to the solar system SUN with 9 planets\n" +
   7.116 +                        "This planet belongs to the solar system SUN with 9 planets\n";
   7.117 +
   7.118 +
   7.119 +                    public String toString() {
   7.120 +                        return "Planet " + name + " with " + moonsCount + " moon(s)";
   7.121 +                    }
   7.122 +                }
   7.123 +            }
   7.124 +        }
   7.125 +    }
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/test/tools/javac/lambda/T8129740/SourceToSourceTranslationTest.java	Wed May 25 15:09:13 2016 +0530
     8.3 @@ -0,0 +1,46 @@
     8.4 +/*
     8.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     8.7 + *
     8.8 + * This code is free software; you can redistribute it and/or modify it
     8.9 + * under the terms of the GNU General Public License version 2 only, as
    8.10 + * published by the Free Software Foundation.
    8.11 + *
    8.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    8.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    8.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    8.15 + * version 2 for more details (a copy is included in the LICENSE file that
    8.16 + * accompanied this code).
    8.17 + *
    8.18 + * You should have received a copy of the GNU General Public License version
    8.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    8.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    8.21 + *
    8.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    8.23 + * or visit www.oracle.com if you need additional information or have any
    8.24 + * questions.
    8.25 + */
    8.26 +
    8.27 +/*
    8.28 + * @test
    8.29 + * @bug 8129740 8133111 8157142
    8.30 + * @summary Incorrect class file created when passing lambda in inner class constructor
    8.31 + * @library /tools/javac/lib
    8.32 + * @build ToolBox
    8.33 + * @run compile -XD-printsource SourceForTranslation.java
    8.34 + * @run main SourceToSourceTranslationTest
    8.35 + */
    8.36 +
    8.37 +import java.nio.file.Path;
    8.38 +import java.nio.file.Paths;
    8.39 +import java.util.List;
    8.40 +
    8.41 +public class SourceToSourceTranslationTest {
    8.42 +
    8.43 +    public static void main(String[] args) throws Exception {
    8.44 +        Path path1 = Paths.get(System.getProperty("test.classes"), "Universe.java");
    8.45 +        Path path2 = Paths.get(System.getProperty("test.src"), "Universe.java.out");
    8.46 +        ToolBox.compareLines(path1, path2, null);
    8.47 +    }
    8.48 +
    8.49 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/test/tools/javac/lambda/T8129740/Universe.java.out	Wed May 25 15:09:13 2016 +0530
     9.3 @@ -0,0 +1,98 @@
     9.4 +
     9.5 +class Universe {
     9.6 +    public String name;
     9.7 +    public int galaxiesCount;
     9.8 +    
     9.9 +    public Universe(String name, int galaxiesCount) {
    9.10 +        super();
    9.11 +        this.name = name;
    9.12 +        this.galaxiesCount = galaxiesCount;
    9.13 +    }
    9.14 +    
    9.15 +    public String toString() {
    9.16 +        return "Universe" + name + " of " + galaxiesCount + " galaxies";
    9.17 +    }
    9.18 +    
    9.19 +    class Galaxy {
    9.20 +        String name;
    9.21 +        private int starsCount;
    9.22 +        
    9.23 +        Galaxy(String name, int starsCount) {
    9.24 +            super();
    9.25 +            this.name = name;
    9.26 +            this.starsCount = starsCount;
    9.27 +        }
    9.28 +        
    9.29 +        public String toString() {
    9.30 +            return "galaxy " + name + " of " + starsCount + " solar systems";
    9.31 +        }
    9.32 +        
    9.33 +        int starsCount() {
    9.34 +            return starsCount;
    9.35 +        }
    9.36 +        
    9.37 +        private String name() {
    9.38 +            return name;
    9.39 +        }
    9.40 +        
    9.41 +        class SolarSystem {
    9.42 +            String name;
    9.43 +            int planetsCount;
    9.44 +            
    9.45 +            SolarSystem(String name, int planetsCount) {
    9.46 +                super();
    9.47 +                this.name = name;
    9.48 +                this.planetsCount = planetsCount;
    9.49 +            }
    9.50 +            
    9.51 +            public String toString() {
    9.52 +                return "Solar System of " + name + " with " + planetsCount + " planets";
    9.53 +            }
    9.54 +            
    9.55 +            int planetsCount() {
    9.56 +                return planetsCount;
    9.57 +            }
    9.58 +            
    9.59 +            SolarSystem copy(SolarSystem s) {
    9.60 +                return s;
    9.61 +            }
    9.62 +            
    9.63 +            class Planet {
    9.64 +                String name;
    9.65 +                int moonsCount;
    9.66 +                
    9.67 +                Planet(String name, int moonsCount, Runnable r) {
    9.68 +                    super();
    9.69 +                    this.name = name;
    9.70 +                    this.moonsCount = moonsCount;
    9.71 +                    r.run();
    9.72 +                }
    9.73 +                
    9.74 +                Planet(String name, int moonsCount) {
    9.75 +                    this(name, moonsCount, java.lang.invoke.LambdaMetafactory.metafactory(name, Universe.Galaxy.this, Universe.Galaxy.SolarSystem.this));
    9.76 +                }
    9.77 +                static final String output = "This planet belongs to the galaxy Mily way with 23456789 stars\nThis planet belongs to the galaxy Mily way with 23456789 stars\nThis planet belongs to the galaxy Mily way with 23456789 stars\nThis planet belongs to the galaxy Mily way with 23456789 stars\nThis planet belongs to the solar system Sun with 9 planets\nThis planet belongs to the solar system Sun with 9 planets\nThis planet belongs to the solar system Sun with 9 planets\nThis planet belongs to the solar system Sun with 9 planets\nThis planet belongs to the solar system SUN with 9 planets\nThis planet belongs to the solar system SUN with 9 planets\n";
    9.78 +                
    9.79 +                public String toString() {
    9.80 +                    return "Planet " + name + " with " + moonsCount + " moon(s)";
    9.81 +                }
    9.82 +                
    9.83 +                /*synthetic*/ private static void lambda$new$0(/*synthetic*/ final String name, /*synthetic*/ final Universe.Galaxy Universe$Galaxy$this, /*synthetic*/ final Universe.Galaxy.SolarSystem Universe$Galaxy$SolarSystem$this) {
    9.84 +                    String n = name;
    9.85 +                    StringBuffer buf = new StringBuffer();
    9.86 +                    buf.append("This planet belongs to the galaxy " + Universe$Galaxy$this.name + " with " + Universe$Galaxy$this.starsCount + " stars\n");
    9.87 +                    buf.append("This planet belongs to the galaxy " + Universe$Galaxy$this.name + " with " + Universe$Galaxy$this.starsCount() + " stars\n");
    9.88 +                    buf.append("This planet belongs to the galaxy " + Universe$Galaxy$this.name() + " with " + Universe$Galaxy$this.starsCount() + " stars\n");
    9.89 +                    buf.append("This planet belongs to the galaxy " + Universe$Galaxy$this.name() + " with " + (Universe$Galaxy$this).starsCount() + " stars\n");
    9.90 +                    buf.append("This planet belongs to the solar system " + Universe$Galaxy$SolarSystem$this.name + " with " + Universe$Galaxy$SolarSystem$this.planetsCount + " planets\n");
    9.91 +                    buf.append("This planet belongs to the solar system " + Universe$Galaxy$SolarSystem$this.name + " with " + Universe$Galaxy$SolarSystem$this.planetsCount() + " planets\n");
    9.92 +                    buf.append("This planet belongs to the solar system " + (Universe$Galaxy$SolarSystem$this).name + " with " + Universe$Galaxy$SolarSystem$this.planetsCount + " planets\n");
    9.93 +                    buf.append("This planet belongs to the solar system " + Universe$Galaxy$SolarSystem$this.name + " with " + Universe$Galaxy$SolarSystem$this.planetsCount + " planets\n");
    9.94 +                    buf.append("This planet belongs to the solar system " + Universe$Galaxy$SolarSystem$this.name.toLowerCase().toUpperCase() + " with " + Universe$Galaxy$SolarSystem$this.planetsCount + " planets\n");
    9.95 +                    buf.append("This planet belongs to the solar system " + Universe$Galaxy$SolarSystem$this.copy(Universe$Galaxy$SolarSystem$this).name.toLowerCase().toUpperCase() + " with " + Universe$Galaxy$SolarSystem$this.planetsCount + " planets\n");
    9.96 +                    if (!buf.toString().equals(output)) throw new AssertionError("Unexpected value\n" + buf);
    9.97 +                }
    9.98 +            }
    9.99 +        }
   9.100 +    }
   9.101 +}

mercurial