Skip to content

Commit cc2cec3

Browse files
committed
ArrayIndexOutOfBoundsException in Parser due to static init block
issue eclipse-jdt#3506
1 parent 2da6950 commit cc2cec3

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Parser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2024 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -3477,6 +3477,9 @@ private boolean isAFieldDeclarationInRecord() {
34773477
continue;
34783478
nestingTypeAndMethod = this.recordNestedMethodLevels.get(node);
34793479
if (nestingTypeAndMethod != null) { // record declaration is done yet
3480+
if (nestingTypeAndMethod[0] != this.nestedType
3481+
|| nestingTypeAndMethod[1] != this.nestedMethod[this.nestedType])
3482+
return false;
34803483
recordIndex = i;
34813484
break;
34823485
}

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019, 2024 IBM Corporation and others.
2+
* Copyright (c) 2019, 2025 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -28,7 +28,7 @@ public class RecordsRestrictedClassTest extends AbstractRegressionTest {
2828
static {
2929
// TESTS_NUMBERS = new int [] { 40 };
3030
// TESTS_RANGE = new int[] { 1, -1 };
31-
// TESTS_NAMES = new String[] { "testIssue1641"};
31+
// TESTS_NAMES = new String[] { "testBug3504_1"};
3232
}
3333

3434
public static Class<?> testClass() {
@@ -9591,4 +9591,29 @@ public static void main(String [] args) {
95919591
},
95929592
"OK!");
95939593
}
9594+
public void testBug3504_1() {
9595+
runNegativeTest(
9596+
new String[] {
9597+
"X.java",
9598+
"""
9599+
class X {
9600+
public static void main(String[] args) {
9601+
record R(int x) {
9602+
static {
9603+
static int i = 0;
9604+
}
9605+
}
9606+
R r = new R(100);
9607+
System.out.println(r.x());
9608+
}
9609+
}
9610+
"""
9611+
},
9612+
"----------\n" +
9613+
"1. ERROR in X.java (at line 5)\n" +
9614+
" static int i = 0;\n" +
9615+
" ^\n" +
9616+
"Illegal modifier for the variable i; only final is permitted\n" +
9617+
"----------\n");
9618+
}
95949619
}

0 commit comments

Comments
 (0)