Skip to content

Commit 4b07f97

Browse files
[Java 25] Bogus error: The blank final field o may not have been
initialized * Fixes #4416
1 parent becbd05 commit 4b07f97

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ public void analyseCode(ClassScope classScope, InitializationFlowContext initial
111111
constructorContext = this.prologueContext;
112112
flowInfo = this.prologueInfo.addInitializationsFrom(flowInfo);
113113
// skip the part already done during PROLOGUE analysis ...
114+
if (this.constructorCall != null) {
115+
if (this.constructorCall.accessMode == ExplicitConstructorCall.This) {
116+
FieldBinding[] fields = this.binding.declaringClass.fields();
117+
for (FieldBinding field : fields) {
118+
if (!field.isStatic()) {
119+
flowInfo.markAsDefinitelyAssigned(field);
120+
}
121+
}
122+
}
123+
}
114124
} else {
115125
flowInfo.setReachMode(initialReachMode);
116126

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818

1919
import java.util.Map;
2020
import junit.framework.Test;
21+
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
2122
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
2223

2324
@SuppressWarnings({ "unchecked", "rawtypes" })
2425
public class InitializationTests extends AbstractRegressionTest {
2526

27+
static {
28+
// TESTS_NAMES = new String [] { "testIssue4416" };
29+
}
2630
public InitializationTests(String name) {
2731
super(name);
2832
}
@@ -487,6 +491,93 @@ public void testBug383690() {
487491
"The blank final field oStatic may not have been initialized\n" +
488492
"----------\n");
489493
}
494+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4416
495+
// Bogus error: The blank final field o may not have been initialized
496+
public void testIssue4416() {
497+
runConformTest(new String[] {
498+
"WrongNotInitialized.java",
499+
"""
500+
public class WrongNotInitialized {
501+
private final Object o;
502+
503+
public WrongNotInitialized(final Object o) {
504+
super();
505+
this.o = o;
506+
}
507+
508+
public WrongNotInitialized() {
509+
this(new Object());
510+
System.out.println(o.toString());
511+
}
512+
}
513+
"""
514+
});
515+
}
516+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4416
517+
// Bogus error: The blank final field o may not have been initialized
518+
public void testIssue4416b() {
519+
runConformTest(new String[] {
520+
"Test.java",
521+
"""
522+
public class Test {
523+
524+
final int x;
525+
526+
Test(int x) {
527+
this.x = x;
528+
}
529+
530+
Test(Test a) {
531+
this(a.x);
532+
//this(1);
533+
534+
System.out.println(x);
535+
}
536+
}
537+
"""
538+
});
539+
}
540+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4416
541+
// Bogus error: The blank final field o may not have been initialized
542+
public void testIssue4416c() {
543+
if (this.complianceLevel < ClassFileConstants.JDK16)
544+
return;
545+
runConformTest(new String[] {
546+
"TestRecord.java",
547+
"""
548+
public record TestRecord(String a)
549+
{
550+
public TestRecord(int b)
551+
{
552+
this(String.valueOf(b));
553+
System.out.println(a.length());
554+
}
555+
}
556+
"""
557+
});
558+
}
559+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4416
560+
// Bogus error: The blank final field o may not have been initialized
561+
public void testIssue4416d() {
562+
runConformTest(new String[] {
563+
"Test.java",
564+
"""
565+
public class Test {
566+
567+
private final Object r;
568+
569+
Test() {
570+
this(Integer.valueOf(1));
571+
r.hashCode();
572+
}
573+
574+
Test(Object r) {
575+
this.r = r;
576+
}
577+
}
578+
"""
579+
});
580+
}
490581
public static Class testClass() {
491582
return InitializationTests.class;
492583
}

0 commit comments

Comments
 (0)