Java: fix Find Usages for Flexible Constructor Bodies super/this calls (IDEA-381039)

(cherry picked from commit e5710feac14035bd2a0193fa84a00f11c8bf8987)

IJ-CR-179855

GitOrigin-RevId: 3efc6055c3807d0db30a0b1fbb08663470bbccca
This commit is contained in:
Bas Leijdekkers
2025-10-31 21:08:38 +00:00
committed by intellij-monorepo-bot
parent ac85e72f4d
commit df4a9c949d
8 changed files with 100 additions and 46 deletions
@@ -17,6 +17,7 @@ import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.ClassInheritorsSearch;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.JavaPsiConstructorUtil;
import com.intellij.util.PairProcessor;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
@@ -97,17 +98,15 @@ class ConstructorReferencesSearchHelper {
// search usages like "this(..)"
if (!DumbService.getInstance(project).runReadActionInSmartMode(
() -> processSuperOrThis(containingClass, constructor, constructorCanBeCalledImplicitly[0], searchScope, project,
isStrictSignatureSearch,
JavaKeywords.THIS, JavaKeywords.SUPER, processor))) {
isStrictSignatureSearch, JavaKeywords.THIS, processor))) {
return false;
}
// search usages like "super(..)"
Processor<PsiClass> processor2 = inheritor -> {
final PsiElement navigationElement = inheritor.getNavigationElement();
if (navigationElement instanceof PsiClass) {
return processSuperOrThis((PsiClass)navigationElement, constructor, constructorCanBeCalledImplicitly[0], searchScope, project,
isStrictSignatureSearch, JavaKeywords.SUPER, JavaKeywords.THIS, processor);
if (inheritor.getNavigationElement() instanceof PsiClass aClass) {
return processSuperOrThis(aClass, constructor, constructorCanBeCalledImplicitly[0], searchScope, project,
isStrictSignatureSearch, JavaKeywords.SUPER, processor);
}
return true;
};
@@ -160,37 +159,28 @@ class ConstructorReferencesSearchHelper {
@NotNull Project project,
final boolean isStrictSignatureSearch,
@NotNull String superOrThisKeyword,
@NotNull String thisOrSuperKeyword,
@NotNull Processor<? super PsiReference> processor) {
PsiMethod[] constructors = inheritor.getConstructors();
if (constructors.length == 0 && constructorCanBeCalledImplicitly) {
if (!processImplicitConstructorCall(inheritor, processor, constructor, project, inheritor)) return false;
}
for (PsiMethod method : constructors) {
PsiCodeBlock body = method.getBody();
if (body == null || method == constructor && isStrictSignatureSearch || method instanceof SyntheticElement) {
if (method == constructor && isStrictSignatureSearch || method instanceof SyntheticElement) {
continue;
}
PsiStatement[] statements = body.getStatements();
if (statements.length != 0 && statements[0] instanceof PsiExpressionStatement exprStatement &&
exprStatement.getExpression() instanceof PsiMethodCallExpression call) {
PsiReferenceExpression refExpr = call.getMethodExpression();
if (PsiSearchScopeUtil.isInScope(searchScope, refExpr)) {
if (refExpr.textMatches(superOrThisKeyword)) {
PsiElement referencedElement = refExpr.resolve();
if (referencedElement instanceof PsiMethod constructor1) {
boolean match = isStrictSignatureSearch
? myManager.areElementsEquivalent(constructor1, constructor)
: myManager.areElementsEquivalent(constructor.getContainingClass(), constructor1.getContainingClass());
if (match && !processor.process(refExpr)) return false;
}
//as long as we've encountered super/this keyword, no implicit ctr calls are possible here
continue;
}
else if (refExpr.textMatches(thisOrSuperKeyword)) {
continue;
PsiMethodCallExpression thisOrSuperCall = JavaPsiConstructorUtil.findThisOrSuperCallInConstructor(method);
if (thisOrSuperCall != null) {
if (PsiSearchScopeUtil.isInScope(searchScope, thisOrSuperCall)) {
PsiReferenceExpression ref = thisOrSuperCall.getMethodExpression();
if (ref.textMatches(superOrThisKeyword) && ref.resolve() instanceof PsiMethod referencedConstructor) {
boolean match = isStrictSignatureSearch
? myManager.areElementsEquivalent(referencedConstructor, constructor)
: myManager.areElementsEquivalent(constructor.getContainingClass(), referencedConstructor.getContainingClass());
if (match && !processor.process(ref)) return false;
}
}
//when we've encountered a super/this call, no implicit ctr calls are possible here
continue;
}
if (constructorCanBeCalledImplicitly && PsiSearchScopeUtil.isInScope(searchScope, method)) {
if (!processImplicitConstructorCall(method, processor, constructor, project, inheritor)) return false;
@@ -0,0 +1,14 @@
// "Add 'int' as 1st parameter to constructor 'Friend()'" "true"
class Friend {
Friend(int i) {
System.out.println(0);
}
}
class Flexible extends Friend {
Flexible() {
System.out.println("before");
super(<caret>1);
System.out.println("after");
}
}
@@ -0,0 +1,14 @@
// "Add 'int' as 1st parameter to constructor 'Friend()'" "true"
class Friend {
Friend() {
System.out.println(0);
}
}
class Flexible extends Friend {
Flexible() {
System.out.println("before");
super(<caret>1);
System.out.println("after");
}
}
@@ -0,0 +1,13 @@
class C {
C<caret>() {
}
}
class C1 extends C {
int i;
C1(int i, int k) {
System.out.println(i);
super();
this.i = i;
}
}
@@ -0,0 +1,13 @@
class C {
C(int i) {
}
}
class C1 extends C {
int i;
C1(int i, int k) {
System.out.println(i);
super(0);
this.i = i;
}
}
@@ -1,12 +1,11 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import static com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase.JAVA_21_ANNOTATED;
public class ChangeMethodSignatureFromUsageModernTest extends LightQuickFixParameterizedTestCase {
@Override
@@ -16,6 +15,6 @@ public class ChangeMethodSignatureFromUsageModernTest extends LightQuickFixParam
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return JAVA_21_ANNOTATED;
return LightJavaCodeInsightFixtureTestCase.JAVA_25;
}
}
@@ -1,20 +1,7 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.psi.search
import com.intellij.psi.PsiReferenceExpression
import com.intellij.psi.search.searches.MethodReferencesSearch
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
@@ -41,4 +28,20 @@ class FindFixtureBasedTest : LightJavaCodeInsightFixtureTestCase() {
assertSize(3, constructors)
assertNull(MethodReferencesSearch.search(constructors[0]).findFirst())
}
fun testFlexibleConstructorBody() {
val aClass = myFixture.addClass("class Friend { Friend() {}}");
myFixture.addClass("class Flexible extends Friend {\n" +
" Flexible() {\n" +
" System.out.println(\"before\");\n" +
" super();\n" +
" System.out.println(\"after\");\n" +
" }\n" +
"}")
val constructors = aClass.constructors
assertSize(1, constructors)
val reference = MethodReferencesSearch.search(constructors[0]).findFirst()
assertTrue(reference?.element is PsiReferenceExpression)
}
}
@@ -99,6 +99,14 @@ public class ChangeSignatureTest extends ChangeSignatureBaseTest {
);
}
public void testFlexibleConstructorBody() {
doTest(null,
new ParameterInfoImpl[]{
ParameterInfoImpl.createNew().withName("i").withType(PsiTypes.intType()).withDefaultValue("0")
}, false
);
}
public void testGenerateDelegate() {
doTest(null,
new ParameterInfoImpl[]{