[java-inspections] IDEA-374865 ClassCanBeRecord: fix edge case with calls to super methods

Now quick-fix is not shown when, in the class constructor, a method from superclass/superinterface is called before the class is ready for that. Basically, respect javac behavior.

In response to IJ-CR-167896

GitOrigin-RevId: 421fc995c478ec20e39292ed02a32cb249b6398b
This commit is contained in:
Bartek Pacia
2025-07-08 21:40:33 +00:00
committed by intellij-monorepo-bot
parent 944a6f1871
commit 90c2e9dc98
2 changed files with 30 additions and 4 deletions
@@ -265,10 +265,15 @@ final class ConstructorBodyProcessor {
if (resolved instanceof PsiField field && !field.hasModifierProperty(STATIC) && field.getContainingClass() == containingClass) {
hasReferenceToClassUnderConstruction.set(true);
}
else if (resolved instanceof PsiMethod method &&
!method.hasModifierProperty(STATIC) &&
method.getContainingClass() == containingClass) {
hasReferenceToClassUnderConstruction.set(true);
else if (resolved instanceof PsiMethod method) {
if (method.hasModifierProperty(STATIC)) return;
if (method.getContainingClass() == containingClass) {
hasReferenceToClassUnderConstruction.set(true);
return;
}
if (containingClass.findMethodBySignature(method, true) != null) {
hasReferenceToClassUnderConstruction.set(true);
}
}
}
});
@@ -0,0 +1,21 @@
// "Convert to record class" "false"
interface LivingAndBreathing {
default void performBackflip() {
System.out.println("Watch out, I'm gonna do a backflip");
}
}
class Person<caret> implements LivingAndBreathing {
final String name;
final int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
Person(String name) {
performBackflip(); // javac error: "cannot reference performBackflip() before supertype constructor has been called"
this(name, 0);
}
}