提交 148e3a9b 编写于 作者: S sadayapalam

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
上级 b930c3e8
...@@ -338,6 +338,11 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -338,6 +338,11 @@ public class LambdaToMethod extends TreeTranslator {
syntheticInits.append((JCExpression) captured_local); syntheticInits.append((JCExpression) captured_local);
} }
} }
// add captured outer this instances (used only when `this' capture itself is illegal)
for (Symbol fv : localContext.getSymbolMap(CAPTURED_OUTER_THIS).keySet()) {
JCTree captured_local = make.QualThis(fv.type);
syntheticInits.append((JCExpression) captured_local);
}
//then, determine the arguments to the indy call //then, determine the arguments to the indy call
List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev); List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev);
...@@ -434,6 +439,32 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -434,6 +439,32 @@ public class LambdaToMethod extends TreeTranslator {
} }
} }
/**
* Translate qualified `this' references within a lambda to the mapped identifier
* @param tree
*/
@Override
public void visitSelect(JCFieldAccess tree) {
if (context == null || !analyzer.lambdaFieldAccessFilter(tree)) {
super.visitSelect(tree);
} else {
int prevPos = make.pos;
try {
make.at(tree);
LambdaTranslationContext lambdaContext = (LambdaTranslationContext) context;
JCTree ltree = lambdaContext.translate(tree);
if (ltree != null) {
result = ltree;
} else {
super.visitSelect(tree);
}
} finally {
make.at(prevPos);
}
}
}
@Override @Override
public void visitVarDef(JCVariableDecl tree) { public void visitVarDef(JCVariableDecl tree) {
LambdaTranslationContext lambdaContext = (LambdaTranslationContext)context; LambdaTranslationContext lambdaContext = (LambdaTranslationContext)context;
...@@ -1125,6 +1156,11 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -1125,6 +1156,11 @@ public class LambdaToMethod extends TreeTranslator {
*/ */
private int lambdaCount = 0; private int lambdaCount = 0;
/**
* List of types undergoing construction via explicit constructor chaining.
*/
private List<ClassSymbol> typesUnderConstruction;
/** /**
* keep the count of lambda expression defined in given context (used to * keep the count of lambda expression defined in given context (used to
* generate unambiguous names for serializable lambdas) * generate unambiguous names for serializable lambdas)
...@@ -1156,10 +1192,35 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -1156,10 +1192,35 @@ public class LambdaToMethod extends TreeTranslator {
private JCClassDecl analyzeAndPreprocessClass(JCClassDecl tree) { private JCClassDecl analyzeAndPreprocessClass(JCClassDecl tree) {
frameStack = List.nil(); frameStack = List.nil();
typesUnderConstruction = List.nil();
localClassDefs = new HashMap<Symbol, JCClassDecl>(); localClassDefs = new HashMap<Symbol, JCClassDecl>();
return translate(tree); return translate(tree);
} }
@Override
public void visitApply(JCMethodInvocation tree) {
List<ClassSymbol> previousNascentTypes = typesUnderConstruction;
try {
Name methName = TreeInfo.name(tree.meth);
if (methName == names._this || methName == names._super) {
typesUnderConstruction = typesUnderConstruction.prepend(currentClass());
}
super.visitApply(tree);
} finally {
typesUnderConstruction = previousNascentTypes;
}
}
// where
private ClassSymbol currentClass() {
for (Frame frame : frameStack) {
if (frame.tree.hasTag(JCTree.Tag.CLASSDEF)) {
JCClassDecl cdef = (JCClassDecl) frame.tree;
return cdef.sym;
}
}
return null;
}
@Override @Override
public void visitBlock(JCBlock tree) { public void visitBlock(JCBlock tree) {
List<Frame> prevStack = frameStack; List<Frame> prevStack = frameStack;
...@@ -1623,6 +1684,22 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -1623,6 +1684,22 @@ public class LambdaToMethod extends TreeTranslator {
&& sym.name != names.init; && sym.name != names.init;
} }
/**
* This is used to filter out those select nodes that need to be adjusted
* when translating away lambda expressions - at the moment, this is the
* set of nodes that select `this' (qualified this)
*/
private boolean lambdaFieldAccessFilter(JCFieldAccess fAccess) {
LambdaTranslationContext lambdaContext =
context instanceof LambdaTranslationContext ?
(LambdaTranslationContext) context : null;
return lambdaContext != null
&& !fAccess.sym.isStatic()
&& fAccess.name == names._this
&& (fAccess.sym.owner.kind == TYP)
&& !lambdaContext.translatedSymbols.get(CAPTURED_OUTER_THIS).isEmpty();
}
/** /**
* This is used to filter out those new class expressions that need to * This is used to filter out those new class expressions that need to
* be qualified with an enclosing tree * be qualified with an enclosing tree
...@@ -1797,6 +1874,7 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -1797,6 +1874,7 @@ public class LambdaToMethod extends TreeTranslator {
translatedSymbols.put(LOCAL_VAR, new LinkedHashMap<Symbol, Symbol>()); translatedSymbols.put(LOCAL_VAR, new LinkedHashMap<Symbol, Symbol>());
translatedSymbols.put(CAPTURED_VAR, new LinkedHashMap<Symbol, Symbol>()); translatedSymbols.put(CAPTURED_VAR, new LinkedHashMap<Symbol, Symbol>());
translatedSymbols.put(CAPTURED_THIS, new LinkedHashMap<Symbol, Symbol>()); translatedSymbols.put(CAPTURED_THIS, new LinkedHashMap<Symbol, Symbol>());
translatedSymbols.put(CAPTURED_OUTER_THIS, new LinkedHashMap<Symbol, Symbol>());
translatedSymbols.put(TYPE_VAR, new LinkedHashMap<Symbol, Symbol>()); translatedSymbols.put(TYPE_VAR, new LinkedHashMap<Symbol, Symbol>());
freeVarProcessedLocalClasses = new HashSet<>(); freeVarProcessedLocalClasses = new HashSet<>();
...@@ -1909,6 +1987,16 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -1909,6 +1987,16 @@ public class LambdaToMethod extends TreeTranslator {
} }
}; };
break; break;
case CAPTURED_OUTER_THIS:
Name name = names.fromString(new String(sym.flatName().toString() + names.dollarThis));
ret = new VarSymbol(SYNTHETIC | FINAL | PARAMETER, name, types.erasure(sym.type), translatedSym) {
@Override
public Symbol baseSymbol() {
//keep mapping with original captured symbol
return sym;
}
};
break;
case LOCAL_VAR: case LOCAL_VAR:
ret = new VarSymbol(sym.flags() & FINAL, sym.name, sym.type, translatedSym); ret = new VarSymbol(sym.flags() & FINAL, sym.name, sym.type, translatedSym);
((VarSymbol) ret).pos = ((VarSymbol) sym).pos; ((VarSymbol) ret).pos = ((VarSymbol) sym).pos;
...@@ -1929,6 +2017,14 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -1929,6 +2017,14 @@ public class LambdaToMethod extends TreeTranslator {
} }
void addSymbol(Symbol sym, LambdaSymbolKind skind) { void addSymbol(Symbol sym, LambdaSymbolKind skind) {
if (skind == CAPTURED_THIS && sym != null && sym.kind == TYP && !typesUnderConstruction.isEmpty()) {
ClassSymbol currentClass = currentClass();
if (currentClass != null && typesUnderConstruction.contains(currentClass)) {
// reference must be to enclosing outer instance, mutate capture kind.
Assert.check(sym != currentClass); // should have been caught right in Attr
skind = CAPTURED_OUTER_THIS;
}
}
Map<Symbol, Symbol> transMap = getSymbolMap(skind); Map<Symbol, Symbol> transMap = getSymbolMap(skind);
if (!transMap.containsKey(sym)) { if (!transMap.containsKey(sym)) {
transMap.put(sym, translate(sym, skind)); transMap.put(sym, translate(sym, skind));
...@@ -1942,17 +2038,49 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -1942,17 +2038,49 @@ public class LambdaToMethod extends TreeTranslator {
} }
JCTree translate(JCIdent lambdaIdent) { JCTree translate(JCIdent lambdaIdent) {
for (Map<Symbol, Symbol> m : translatedSymbols.values()) { for (LambdaSymbolKind kind : LambdaSymbolKind.values()) {
if (m.containsKey(lambdaIdent.sym)) { Map<Symbol, Symbol> m = getSymbolMap(kind);
Symbol tSym = m.get(lambdaIdent.sym); switch(kind) {
JCTree t = make.Ident(tSym).setType(lambdaIdent.type); default:
tSym.setTypeAttributes(lambdaIdent.sym.getRawTypeAttributes()); if (m.containsKey(lambdaIdent.sym)) {
return t; Symbol tSym = m.get(lambdaIdent.sym);
JCTree t = make.Ident(tSym).setType(lambdaIdent.type);
tSym.setTypeAttributes(lambdaIdent.sym.getRawTypeAttributes());
return t;
}
break;
case CAPTURED_OUTER_THIS:
if (lambdaIdent.sym.owner.kind == TYP && m.containsKey(lambdaIdent.sym.owner)) {
// Transform outer instance variable references anchoring them to the captured synthetic.
Symbol tSym = m.get(lambdaIdent.sym.owner);
JCExpression t = make.Ident(tSym).setType(lambdaIdent.sym.owner.type);
tSym.setTypeAttributes(lambdaIdent.sym.owner.getRawTypeAttributes());
t = make.Select(t, lambdaIdent.name);
t.setType(lambdaIdent.type);
TreeInfo.setSymbol(t, lambdaIdent.sym);
return t;
}
break;
} }
} }
return null; return null;
} }
/* Translate away qualified this expressions, anchoring them to synthetic parameters that
capture the qualified this handle. `fieldAccess' is guaranteed to one such.
*/
public JCTree translate(JCFieldAccess fieldAccess) {
Assert.check(fieldAccess.name == names._this);
Map<Symbol, Symbol> m = translatedSymbols.get(LambdaSymbolKind.CAPTURED_OUTER_THIS);
if (m.containsKey(fieldAccess.sym.owner)) {
Symbol tSym = m.get(fieldAccess.sym.owner);
JCExpression t = make.Ident(tSym).setType(fieldAccess.sym.owner.type);
tSym.setTypeAttributes(fieldAccess.sym.owner.getRawTypeAttributes());
return t;
}
return null;
}
/** /**
* The translatedSym is not complete/accurate until the analysis is * The translatedSym is not complete/accurate until the analysis is
* finished. Once the analysis is finished, the translatedSym is * finished. Once the analysis is finished, the translatedSym is
...@@ -1990,6 +2118,10 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -1990,6 +2118,10 @@ public class LambdaToMethod extends TreeTranslator {
params.append(make.VarDef((VarSymbol) thisSym, null)); params.append(make.VarDef((VarSymbol) thisSym, null));
parameterSymbols.append((VarSymbol) thisSym); parameterSymbols.append((VarSymbol) thisSym);
} }
for (Symbol thisSym : getSymbolMap(CAPTURED_OUTER_THIS).values()) {
params.append(make.VarDef((VarSymbol) thisSym, null));
parameterSymbols.append((VarSymbol) thisSym);
}
for (Symbol thisSym : getSymbolMap(PARAM).values()) { for (Symbol thisSym : getSymbolMap(PARAM).values()) {
params.append(make.VarDef((VarSymbol) thisSym, null)); params.append(make.VarDef((VarSymbol) thisSym, null));
parameterSymbols.append((VarSymbol) thisSym); parameterSymbols.append((VarSymbol) thisSym);
...@@ -2138,6 +2270,7 @@ public class LambdaToMethod extends TreeTranslator { ...@@ -2138,6 +2270,7 @@ public class LambdaToMethod extends TreeTranslator {
LOCAL_VAR, // original to translated lambda locals LOCAL_VAR, // original to translated lambda locals
CAPTURED_VAR, // variables in enclosing scope to translated synthetic parameters CAPTURED_VAR, // variables in enclosing scope to translated synthetic parameters
CAPTURED_THIS, // class symbols to translated synthetic parameters (for captured member access) CAPTURED_THIS, // class symbols to translated synthetic parameters (for captured member access)
CAPTURED_OUTER_THIS, // used when `this' capture is illegal, but outer this capture is legit (JDK-8129740)
TYPE_VAR; // original to translated lambda type variables TYPE_VAR; // original to translated lambda type variables
} }
......
...@@ -621,6 +621,12 @@ public class TreeMaker implements JCTree.Factory { ...@@ -621,6 +621,12 @@ public class TreeMaker implements JCTree.Factory {
return Ident(new VarSymbol(FINAL, names._this, t, t.tsym)); return Ident(new VarSymbol(FINAL, names._this, t, t.tsym));
} }
/** Create a tree representing qualified `this' given its type
*/
public JCExpression QualThis(Type t) {
return Select(Type(t), new VarSymbol(FINAL, names._this, t, t.tsym));
}
/** Create a tree representing a class literal. /** Create a tree representing a class literal.
*/ */
public JCExpression ClassLiteral(ClassSymbol clazz) { public JCExpression ClassLiteral(ClassSymbol clazz) {
......
...@@ -176,6 +176,7 @@ public class Names { ...@@ -176,6 +176,7 @@ public class Names {
public final Name lambda; public final Name lambda;
public final Name metafactory; public final Name metafactory;
public final Name altMetafactory; public final Name altMetafactory;
public final Name dollarThis;
public final Name.Table table; public final Name.Table table;
...@@ -234,6 +235,7 @@ public class Names { ...@@ -234,6 +235,7 @@ public class Names {
value = fromString("value"); value = fromString("value");
valueOf = fromString("valueOf"); valueOf = fromString("valueOf");
values = fromString("values"); values = fromString("values");
dollarThis = fromString("$this");
// class names // class names
java_io_Serializable = fromString("java.io.Serializable"); java_io_Serializable = fromString("java.io.Serializable");
......
/*
* Copyright (c) 2015, 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.
*
* 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.
*/
/*
* @test
* @bug 8129740 8133111 8157142
* @summary Incorrect class file created when passing lambda in inner class constructor
* @run main AllowEnclosingVarCaptureTest
*/
public class AllowEnclosingVarCaptureTest {
int var = 0;
void foo() {
var *= 2;
}
public static void main(String[] args) {
new AllowEnclosingVarCaptureTest().new Inner(9764);
}
public class Inner {
public Inner(Runnable r) {
r.run();
if (var != 66704)
throw new AssertionError("Unexpected output: " + var);
}
public Inner(int x) {
this(() -> {
var = x + 1234;
AllowEnclosingVarCaptureTest.this.var += 5678;
foo();
AllowEnclosingVarCaptureTest.this.foo();
});
}
}
}
/*
* Copyright (c) 2015, 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.
*
* 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.
*/
/*
* @test
* @bug 8129740 8133111 8157142
* @summary Incorrect class file created when passing lambda in inner class constructor
* @run main CaptureInCtorChainingTest
*/
import java.util.function.Consumer;
import java.util.function.Function;
public class CaptureInCtorChainingTest {
CaptureInCtorChainingTest(Function<Function<Function<Consumer<Void>, Void>, Void>, Void> innerClass) {
new InnerClass(innerClass);
}
void foo(Void v) { }
class InnerClass {
InnerClass(Function<Function<Function<Consumer<Void>, Void>, Void>, Void> factory) {
this(factory.apply(o -> o.apply(CaptureInCtorChainingTest.this::foo)));
}
InnerClass(Void unused) { }
}
public static void main(String[] args) {
new CaptureInCtorChainingTest(o -> null);
}
}
/*
* Copyright (c) 2015, 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.
*
* 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.
*/
/*
* @test
* @bug 8129740 8133111 8157142
* @summary Incorrect class file created when passing lambda in inner class constructor
* @run main QualifiedThisAccessTest
*/
public class QualifiedThisAccessTest { // Not referenced by lambda, so should not be captured.
public class Universe { // Not referenced by lambda, so should not be captured.
public String name;
public int galaxiesCount;
public Universe(String name, int galaxiesCount) {
this.name = name;
this.galaxiesCount = galaxiesCount;
}
public String toString() {
return "Universe" + name + " of " + galaxiesCount + " galaxies";
}
class Galaxy {
String name;
private int starsCount;
Galaxy(String name, int starsCount) {
this.name = name;
this.starsCount = starsCount;
}
public String toString() {
return "galaxy " + name + " of " + starsCount + " solar systems";
}
int starsCount() {
return starsCount;
}
private String name() {
return name;
}
class SolarSystem {
String name;
int planetsCount;
SolarSystem(String name, int planetsCount) {
this.name = name;
this.planetsCount = planetsCount;
}
public String toString() {
return "Solar System of " + name + " with " + planetsCount + " planets";
}
int planetsCount() {
return planetsCount;
}
SolarSystem copy(SolarSystem s) {
return s;
}
class Planet {
String name;
int moonsCount;
Planet(String name, int moonsCount, Runnable r) {
this.name = name;
this.moonsCount = moonsCount;
r.run();
}
Planet (String name, int moonsCount) {
this(name, moonsCount, ()-> {
StringBuffer buf = new StringBuffer();
buf.append("This planet belongs to the galaxy "
+ Galaxy.this.name + " with " + starsCount + " stars\n");
buf.append("This planet belongs to the galaxy "
+ Universe.Galaxy.this.name + " with " + starsCount() + " stars\n");
buf.append("This planet belongs to the galaxy "
+ Galaxy.this.name() + " with " + starsCount() + " stars\n");
buf.append("This planet belongs to the galaxy "
+ Universe.Galaxy.this.name() + " with "
+ (Universe.Galaxy.this).starsCount() + " stars\n");
buf.append("This planet belongs to the solar system "
+ SolarSystem.this.name + " with " + planetsCount + " planets\n");
buf.append("This planet belongs to the solar system "
+ Galaxy.SolarSystem.this.name + " with " + planetsCount() + " planets\n");
buf.append("This planet belongs to the solar system "
+ (SolarSystem.this).name + " with " + planetsCount + " planets\n");
buf.append("This planet belongs to the solar system "
+ Universe.Galaxy.SolarSystem.this.name + " with "
+ Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
buf.append("This planet belongs to the solar system "
+ Universe.Galaxy.SolarSystem.this.name.toLowerCase().toUpperCase()
+ " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
buf.append("This planet belongs to the solar system "
+ copy(Universe.Galaxy.SolarSystem.this).name.toLowerCase().toUpperCase()
+ " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
if (!buf.toString().equals(output))
throw new AssertionError("Unexpected value\n" + buf);
});
}
static final String output =
"This planet belongs to the galaxy Mily way with 23456789 stars\n" +
"This planet belongs to the galaxy Mily way with 23456789 stars\n" +
"This planet belongs to the galaxy Mily way with 23456789 stars\n" +
"This planet belongs to the galaxy Mily way with 23456789 stars\n" +
"This planet belongs to the solar system Sun with 9 planets\n" +
"This planet belongs to the solar system Sun with 9 planets\n" +
"This planet belongs to the solar system Sun with 9 planets\n" +
"This planet belongs to the solar system Sun with 9 planets\n" +
"This planet belongs to the solar system SUN with 9 planets\n" +
"This planet belongs to the solar system SUN with 9 planets\n";
public String toString() {
return "Planet " + name + " with " + moonsCount + " moon(s)";
}
}
}
}
}
public static void main(String[] args) {
new QualifiedThisAccessTest().new Universe("Universe", 12345678).new Galaxy("Mily way", 23456789).new SolarSystem("Sun", 9).new Planet("Earth", 1);
}
}
class Universe { // Not referenced by lambda, so should not be captured.
public String name;
public int galaxiesCount;
public Universe(String name, int galaxiesCount) {
this.name = name;
this.galaxiesCount = galaxiesCount;
}
public String toString() {
return "Universe" + name + " of " + galaxiesCount + " galaxies";
}
class Galaxy {
String name;
private int starsCount;
Galaxy(String name, int starsCount) {
this.name = name;
this.starsCount = starsCount;
}
public String toString() {
return "galaxy " + name + " of " + starsCount + " solar systems";
}
int starsCount() {
return starsCount;
}
private String name() {
return name;
}
class SolarSystem {
String name;
int planetsCount;
SolarSystem(String name, int planetsCount) {
this.name = name;
this.planetsCount = planetsCount;
}
public String toString() {
return "Solar System of " + name + " with " + planetsCount + " planets";
}
int planetsCount() {
return planetsCount;
}
SolarSystem copy(SolarSystem s) {
return s;
}
class Planet {
String name;
int moonsCount;
Planet(String name, int moonsCount, Runnable r) {
this.name = name;
this.moonsCount = moonsCount;
r.run();
}
Planet (String name, int moonsCount) {
this(name, moonsCount, ()-> {
String n = name;
StringBuffer buf = new StringBuffer();
buf.append("This planet belongs to the galaxy "
+ Galaxy.this.name + " with " + starsCount + " stars\n");
buf.append("This planet belongs to the galaxy "
+ Universe.Galaxy.this.name + " with " + starsCount() + " stars\n");
buf.append("This planet belongs to the galaxy "
+ Galaxy.this.name() + " with " + starsCount() + " stars\n");
buf.append("This planet belongs to the galaxy "
+ Universe.Galaxy.this.name() + " with "
+ (Universe.Galaxy.this).starsCount() + " stars\n");
buf.append("This planet belongs to the solar system "
+ SolarSystem.this.name + " with " + planetsCount + " planets\n");
buf.append("This planet belongs to the solar system "
+ Galaxy.SolarSystem.this.name + " with " + planetsCount() + " planets\n");
buf.append("This planet belongs to the solar system "
+ (SolarSystem.this).name + " with " + planetsCount + " planets\n");
buf.append("This planet belongs to the solar system "
+ Universe.Galaxy.SolarSystem.this.name + " with "
+ Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
buf.append("This planet belongs to the solar system "
+ Universe.Galaxy.SolarSystem.this.name.toLowerCase().toUpperCase()
+ " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
buf.append("This planet belongs to the solar system "
+ copy(Universe.Galaxy.SolarSystem.this).name.toLowerCase().toUpperCase()
+ " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
if (!buf.toString().equals(output))
throw new AssertionError("Unexpected value\n" + buf);
});
}
static final String output =
"This planet belongs to the galaxy Mily way with 23456789 stars\n" +
"This planet belongs to the galaxy Mily way with 23456789 stars\n" +
"This planet belongs to the galaxy Mily way with 23456789 stars\n" +
"This planet belongs to the galaxy Mily way with 23456789 stars\n" +
"This planet belongs to the solar system Sun with 9 planets\n" +
"This planet belongs to the solar system Sun with 9 planets\n" +
"This planet belongs to the solar system Sun with 9 planets\n" +
"This planet belongs to the solar system Sun with 9 planets\n" +
"This planet belongs to the solar system SUN with 9 planets\n" +
"This planet belongs to the solar system SUN with 9 planets\n";
public String toString() {
return "Planet " + name + " with " + moonsCount + " moon(s)";
}
}
}
}
}
/*
* Copyright (c) 2015, 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.
*
* 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.
*/
/*
* @test
* @bug 8129740 8133111 8157142
* @summary Incorrect class file created when passing lambda in inner class constructor
* @library /tools/javac/lib
* @build ToolBox
* @run compile -XD-printsource SourceForTranslation.java
* @run main SourceToSourceTranslationTest
*/
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class SourceToSourceTranslationTest {
public static void main(String[] args) throws Exception {
Path path1 = Paths.get(System.getProperty("test.classes"), "Universe.java");
Path path2 = Paths.get(System.getProperty("test.src"), "Universe.java.out");
ToolBox.compareLines(path1, path2, null);
}
}
class Universe {
public String name;
public int galaxiesCount;
public Universe(String name, int galaxiesCount) {
super();
this.name = name;
this.galaxiesCount = galaxiesCount;
}
public String toString() {
return "Universe" + name + " of " + galaxiesCount + " galaxies";
}
class Galaxy {
String name;
private int starsCount;
Galaxy(String name, int starsCount) {
super();
this.name = name;
this.starsCount = starsCount;
}
public String toString() {
return "galaxy " + name + " of " + starsCount + " solar systems";
}
int starsCount() {
return starsCount;
}
private String name() {
return name;
}
class SolarSystem {
String name;
int planetsCount;
SolarSystem(String name, int planetsCount) {
super();
this.name = name;
this.planetsCount = planetsCount;
}
public String toString() {
return "Solar System of " + name + " with " + planetsCount + " planets";
}
int planetsCount() {
return planetsCount;
}
SolarSystem copy(SolarSystem s) {
return s;
}
class Planet {
String name;
int moonsCount;
Planet(String name, int moonsCount, Runnable r) {
super();
this.name = name;
this.moonsCount = moonsCount;
r.run();
}
Planet(String name, int moonsCount) {
this(name, moonsCount, java.lang.invoke.LambdaMetafactory.metafactory(name, Universe.Galaxy.this, Universe.Galaxy.SolarSystem.this));
}
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";
public String toString() {
return "Planet " + name + " with " + moonsCount + " moon(s)";
}
/*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) {
String n = name;
StringBuffer buf = new StringBuffer();
buf.append("This planet belongs to the galaxy " + Universe$Galaxy$this.name + " with " + Universe$Galaxy$this.starsCount + " stars\n");
buf.append("This planet belongs to the galaxy " + Universe$Galaxy$this.name + " with " + Universe$Galaxy$this.starsCount() + " stars\n");
buf.append("This planet belongs to the galaxy " + Universe$Galaxy$this.name() + " with " + Universe$Galaxy$this.starsCount() + " stars\n");
buf.append("This planet belongs to the galaxy " + Universe$Galaxy$this.name() + " with " + (Universe$Galaxy$this).starsCount() + " stars\n");
buf.append("This planet belongs to the solar system " + Universe$Galaxy$SolarSystem$this.name + " with " + Universe$Galaxy$SolarSystem$this.planetsCount + " planets\n");
buf.append("This planet belongs to the solar system " + Universe$Galaxy$SolarSystem$this.name + " with " + Universe$Galaxy$SolarSystem$this.planetsCount() + " planets\n");
buf.append("This planet belongs to the solar system " + (Universe$Galaxy$SolarSystem$this).name + " with " + Universe$Galaxy$SolarSystem$this.planetsCount + " planets\n");
buf.append("This planet belongs to the solar system " + Universe$Galaxy$SolarSystem$this.name + " with " + Universe$Galaxy$SolarSystem$this.planetsCount + " planets\n");
buf.append("This planet belongs to the solar system " + Universe$Galaxy$SolarSystem$this.name.toLowerCase().toUpperCase() + " with " + Universe$Galaxy$SolarSystem$this.planetsCount + " planets\n");
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");
if (!buf.toString().equals(output)) throw new AssertionError("Unexpected value\n" + buf);
}
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册