Skip to content

Commit

Permalink
[Switch Expression] Internal compiler error: java.lang.NullPointerExc…
Browse files Browse the repository at this point in the history
…eption: Cannot read field "binding" because "this.methodDeclaration" is null (eclipse-jdt#2361)

* Fixes eclipse-jdt#2360
  • Loading branch information
srikanth-sankaran authored Apr 19, 2024
1 parent 35cdcda commit 543fb44
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7740,33 +7740,40 @@ private TypeBinding retrieveLocalType(int currentPC, int resolvedPosition) {
}
}
}
ReferenceBinding declaringClass = this.methodDeclaration.binding.declaringClass;
if (resolvedPosition == 0 && !this.methodDeclaration.isStatic()) {
return declaringClass;
}
int enumOffset = declaringClass.isEnum() ? 2 : 0; // String name, int ordinal
int argSlotSize = 1 + enumOffset; // this==aload0
if (this.methodDeclaration.binding.isConstructor()) {
if (declaringClass.isEnum()) {
switch (resolvedPosition) {
case 1:
return this.methodDeclaration.scope.getJavaLangString();
case 2:
return TypeBinding.INT;
default: break;
}
if (this.methodDeclaration != null) {
ReferenceBinding declaringClass = this.methodDeclaration.binding.declaringClass;
if (resolvedPosition == 0 && !this.methodDeclaration.isStatic()) {
return declaringClass;
}
if (declaringClass.isNestedType()) {
ReferenceBinding[] enclosingTypes = declaringClass.syntheticEnclosingInstanceTypes();
if (enclosingTypes != null) {
if (resolvedPosition < argSlotSize + enclosingTypes.length)
return enclosingTypes[resolvedPosition - argSlotSize];
int enumOffset = declaringClass.isEnum() ? 2 : 0; // String name, int ordinal
int argSlotSize = 1 + enumOffset; // this==aload0
if (this.methodDeclaration.binding.isConstructor()) {
if (declaringClass.isEnum()) {
switch (resolvedPosition) {
case 1:
return this.methodDeclaration.scope.getJavaLangString();
case 2:
return TypeBinding.INT;
default: break;
}
}
for (SyntheticArgumentBinding extraSyntheticArgument : declaringClass.syntheticOuterLocalVariables()) {
if (extraSyntheticArgument.resolvedPosition == resolvedPosition)
return extraSyntheticArgument.type;
if (declaringClass.isNestedType()) {
ReferenceBinding[] enclosingTypes = declaringClass.syntheticEnclosingInstanceTypes();
if (enclosingTypes != null) {
if (resolvedPosition < argSlotSize + enclosingTypes.length)
return enclosingTypes[resolvedPosition - argSlotSize];
}
for (SyntheticArgumentBinding extraSyntheticArgument : declaringClass.syntheticOuterLocalVariables()) {
if (extraSyntheticArgument.resolvedPosition == resolvedPosition)
return extraSyntheticArgument.type;
}
}
}
} else if (this.lambdaExpression != null) {
ReferenceBinding declaringClass = this.lambdaExpression.binding.declaringClass;
if (resolvedPosition == 0 && !this.lambdaExpression.binding.isStatic()) {
return declaringClass;
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7325,4 +7325,77 @@ public static void main(String[] args) {
},
"42");
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2360
// [Switch Expression] Internal compiler error: java.lang.NullPointerException: Cannot read field "binding" because "this.methodDeclaration" is null
public void testIssue2360() {
if (this.complianceLevel < ClassFileConstants.JDK14)
return;
this.runConformTest(
new String[] {
"X.java",
"""
interface I {
void foo(int p, int q);
}
public class X {
int f;
void foo(int a) {
int loc = 10;
I i = (int p, int q) -> {
I i2 = new I() { public void foo(int f, int p0) {};};
System.out.println(10 + switch (10) {
default -> { try { yield 32; } catch (NullPointerException npe) { yield -10; } }});
System.out.println(10 + switch (loc) {
default -> { try { yield 0; } catch (NullPointerException npe) { yield -10; } }});
System.out.println(10 + switch (p) {
default -> { try { yield p; } catch (NullPointerException npe) { yield -10; } }});
System.out.println(10 + switch (q) {
default -> { try { yield q; } catch (NullPointerException npe) { yield -10; } }});
};
i.foo(10, 20);
}
public static void main(String[] args) {
new X().foo(42);
}
}
"""
},
"42\n" +
"10\n" +
"20\n" +
"30");
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2360
// [Switch Expression] Internal compiler error: java.lang.NullPointerException: Cannot read field "binding" because "this.methodDeclaration" is null
public void testIssue2360_2() {
if (this.complianceLevel < ClassFileConstants.JDK14)
return;
this.runConformTest(
new String[] {
"X.java",
"""
interface I {
void foo(int p, int q);
}
public class X {
int f;
void foo(int a) {
int loc;
I i = (int p, int q) -> {
I i2 = new I() { public void foo(int f, int p0) {};};
System.out.println(10 + switch (10) {
default -> { try { yield 32; } catch (NullPointerException npe) { yield -10; } }});
};
i.foo(10, 20);
}
public static void main(String[] args) {
new X().foo(42);
}
}
"""
},
"42");
}
}

0 comments on commit 543fb44

Please sign in to comment.