Skip to content

Commit 0ada239

Browse files
[Nestmates] Unnecessary access widening bridge method generated since eclipse-jdt#4197 (eclipse-jdt#4206)
* Fixes eclipse-jdt#4204
1 parent c738041 commit 0ada239

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,14 +1794,14 @@ private final void updateNestRelations() {
17941794

17951795
ClassScope outerMostClassScope = this.scope;
17961796
Scope skope = this.scope;
1797-
1797+
boolean concreteType = true;
17981798
while (skope != null && skope.kind != Scope.COMPILATION_UNIT_SCOPE) {
17991799
switch (skope.kind) {
18001800
case Scope.METHOD_SCOPE :
18011801
ReferenceContext context = ((MethodScope) skope).referenceContext;
18021802
if (context instanceof LambdaExpression lambdaExpression) {
18031803
if (lambdaExpression != lambdaExpression.original) // transient unreal universe.
1804-
return;
1804+
concreteType = false;
18051805
}
18061806
break;
18071807
case Scope.CLASS_SCOPE:
@@ -1813,8 +1813,11 @@ private final void updateNestRelations() {
18131813

18141814
SourceTypeBinding nestHost = outerMostClassScope != null ? outerMostClassScope.referenceContext.binding : null;
18151815
if (nestHost != null && !this.binding.equals(nestHost)) {
1816+
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=572190 && https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4204:
1817+
// The nest host is valid even for a transient type, but a transient type from a lambda copy is not a member of the nest host (its non-transient original is/will be)
18161818
this.binding.setNestHost(nestHost);
1817-
nestHost.addNestMember(this.binding);
1819+
if (concreteType)
1820+
nestHost.addNestMember(this.binding);
18181821
}
18191822
}
18201823

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/LambdaExpressionsTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.eclipse.jdt.core.tests.compiler.regression.AbstractRegressionTest.JavacTestOptions.EclipseJustification;
2727
import org.eclipse.jdt.core.tests.compiler.regression.AbstractRegressionTest.JavacTestOptions.JavacHasABug;
2828
import org.eclipse.jdt.core.tests.util.Util;
29+
import org.eclipse.jdt.core.util.ClassFileBytesDisassembler;
2930
import org.eclipse.jdt.core.util.IAttributeNamesConstants;
3031
import org.eclipse.jdt.core.util.IClassFileAttribute;
3132
import org.eclipse.jdt.core.util.IClassFileReader;
@@ -8903,6 +8904,54 @@ public static void main(String [] args) {
89038904
},
89048905
"Callable");
89058906
}
8907+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4204
8908+
// negative testing that the synthetic method - access - not present in the class declaring the private method accessed by a nestmate
8909+
public void testIssue4204() throws Exception {
8910+
8911+
this.runConformTest(
8912+
new String[] {
8913+
"X.java",
8914+
"""
8915+
interface Base {
8916+
Base get(int x);
8917+
}
8918+
public class X {
8919+
private void privateMethod(int p) {
8920+
System.out.println("Private method called with " + p);
8921+
}
8922+
<T> Base foo(Base b) {
8923+
return b;
8924+
}
8925+
void bar(Base b) {
8926+
b.get(42);
8927+
}
8928+
void testCase() {
8929+
bar(foo((int p)-> {
8930+
new Object() {
8931+
void foo() {
8932+
privateMethod(p);
8933+
}
8934+
}.foo();
8935+
return null;}
8936+
));
8937+
}
8938+
8939+
public static void main(String[] args) {
8940+
new X().testCase();
8941+
}
8942+
}
8943+
""",
8944+
},
8945+
"Private method called with 42");
8946+
8947+
if (this.complianceLevel >= ClassFileConstants.JDK11) {
8948+
String unExpectedPartialOutput = "access$";
8949+
verifyNegativeClassFile(unExpectedPartialOutput, "X.class", ClassFileBytesDisassembler.SYSTEM);
8950+
} else {
8951+
String expectedPartialOutput = "static synthetic void access$0(X arg0, int arg1);";
8952+
verifyClassFile(expectedPartialOutput, "X.class", ClassFileBytesDisassembler.SYSTEM);
8953+
}
8954+
}
89068955
public static Class testClass() {
89078956
return LambdaExpressionsTest.class;
89088957
}

0 commit comments

Comments
 (0)