IDEA-110255 [java]: handle default constructors and implicit super constructor calls better in Call Hierarchy

GitOrigin-RevId: bb2cf1e13cfc40f39fd5a26ced304cd1256efb8a
This commit is contained in:
Bas Leijdekkers
2025-11-15 09:19:05 +00:00
committed by intellij-monorepo-bot
parent b997e6c911
commit e32298185d
22 changed files with 125 additions and 92 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2024 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.ide.hierarchy.call;
import com.intellij.codeInsight.highlighting.HighlightManager;
@@ -156,9 +156,9 @@ public final class CallHierarchyNodeDescriptor extends HierarchyNodeDescriptor i
return;
}
PsiReference firstReference = myReferences.get(0);
PsiReference firstReference = myReferences.getFirst();
PsiElement element = firstReference.getElement();
PsiElement callElement = element.getParent();
PsiElement callElement = (element instanceof PsiNameIdentifierOwner) ? element : element.getParent();
if (callElement instanceof Navigatable navigatable && navigatable.canNavigate()) {
navigatable.navigate(requestFocus);
}
@@ -173,9 +173,10 @@ public final class CallHierarchyNodeDescriptor extends HierarchyNodeDescriptor i
if (editor != null) {
HighlightManager highlightManager = HighlightManager.getInstance(myProject);
List<RangeHighlighter> highlighters = new ArrayList<>();
for (PsiReference psiReference : myReferences) {
PsiElement eachElement = psiReference.getElement();
PsiElement eachMethodCall = eachElement.getParent();
for (PsiReference ref : myReferences) {
PsiElement eachElement = ref.getElement();
PsiElement eachMethodCall =
eachElement instanceof PsiNameIdentifierOwner owner ? owner.getNameIdentifier() : eachElement.getParent();
if (eachMethodCall != null) {
TextRange textRange = eachMethodCall.getTextRange();
highlightManager.addRangeHighlight(editor, textRange.getStartOffset(), textRange.getEndOffset(),
@@ -191,7 +192,7 @@ public final class CallHierarchyNodeDescriptor extends HierarchyNodeDescriptor i
return getPsiElement() instanceof Navigatable navigatable && navigatable.canNavigate();
}
if (myReferences.isEmpty()) return false;
PsiReference firstReference = myReferences.get(0);
PsiReference firstReference = myReferences.getFirst();
PsiElement callElement = firstReference.getElement().getParent();
if (callElement == null || !callElement.isValid()) return false;
if (!(callElement instanceof Navigatable navigatable) || !navigatable.canNavigate()) {
@@ -5,11 +5,15 @@ import com.intellij.ide.hierarchy.HierarchyNodeDescriptor;
import com.intellij.ide.hierarchy.HierarchyTreeStructure;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.light.LightDefaultConstructor;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.OverridingMethodsSearch;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ArrayUtilRt;
import com.intellij.util.JavaPsiConstructorUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
@@ -29,7 +33,15 @@ public final class CalleeMethodsTreeStructure extends HierarchyTreeStructure {
@Override
protected Object @NotNull [] buildChildren(@NotNull HierarchyNodeDescriptor descriptor) {
PsiElement targetElement = ((CallHierarchyNodeDescriptor)getBaseDescriptor()).getTargetElement();
PsiElement base = (targetElement instanceof PsiMethod baseMethod) ? baseMethod.getContainingClass() : targetElement;
PsiMember enclosingElement = ((CallHierarchyNodeDescriptor)descriptor).getEnclosingElement();
if (enclosingElement instanceof LightDefaultConstructor constructor) {
PsiMethod superConstructor = findNoArgSuperConstructor(constructor);
return superConstructor != null && isInScope(base, superConstructor, myScopeType)
? new Object[]{new CallHierarchyNodeDescriptor(myProject, descriptor, superConstructor, false, false)}
: ArrayUtilRt.EMPTY_OBJECT_ARRAY;
}
if (!(enclosingElement instanceof PsiMethod method) || enclosingElement instanceof SyntheticElement) {
return ArrayUtilRt.EMPTY_OBJECT_ARRAY;
}
@@ -39,18 +51,19 @@ public final class CalleeMethodsTreeStructure extends HierarchyTreeStructure {
if (body != null) {
collectCallees(body, methods);
}
PsiElement targetElement = ((CallHierarchyNodeDescriptor)getBaseDescriptor()).getTargetElement();
PsiClass baseClass = (targetElement instanceof PsiMethod baseMethod) ? baseMethod.getContainingClass() : null;
if (method.isConstructor() && JavaPsiConstructorUtil.findThisOrSuperCallInConstructor(method) == null) {
PsiMethod superConstructor = findNoArgSuperConstructor(method);
if (superConstructor != null) methods.add(superConstructor);
}
Map<PsiMethod, CallHierarchyNodeDescriptor> methodToDescriptorMap = new HashMap<>();
List<CallHierarchyNodeDescriptor> result = new ArrayList<>();
// also add overriding methods as children when possible
Iterable<PsiMethod> allMethods = (baseClass == null) ? methods : ContainerUtil.concat(methods, OverridingMethodsSearch.search(method).asIterable());
// also add overriding methods as children
SearchScope scope = getSearchScope(myScopeType, base);
Iterable<PsiMethod> allMethods = ContainerUtil.concat(methods, OverridingMethodsSearch.search(method, scope, true).findAll());
for (PsiMethod callee : allMethods) {
if (baseClass != null && !isInScope(baseClass, callee, myScopeType)
|| JavaCallReferenceProcessor.isRecursiveNode(callee, descriptor)) {
if (!isInScope(base, callee, myScopeType) || JavaCallReferenceProcessor.isRecursiveNode(callee, descriptor)) {
continue;
}
@@ -68,10 +81,27 @@ public final class CalleeMethodsTreeStructure extends HierarchyTreeStructure {
return ArrayUtil.toObjectArray(result);
}
private static @Nullable PsiMethod findNoArgSuperConstructor(PsiMethod method) {
PsiClass aClass = method.getContainingClass();
if (aClass == null) return null;
PsiClass superClass = aClass.getSuperClass();
if (superClass == null) return null;
PsiMethod[] constructors = superClass.getConstructors();
if (constructors.length == 0) {
return LightDefaultConstructor.create(superClass);
}
else {
for (PsiMethod constructor : constructors) {
if (constructor.getParameterList().isEmpty()) {
return constructor;
}
}
}
return null;
}
private static void collectCallees(@NotNull PsiElement element, @NotNull List<? super PsiMethod> methods) {
PsiElement[] children = element.getChildren();
for (PsiElement child : children) {
for (PsiElement child : element.getChildren()) {
collectCallees(child, methods);
if (child instanceof PsiMethodCallExpression callExpression) {
PsiReferenceExpression methodExpression = callExpression.getMethodExpression();
@@ -1,24 +1,11 @@
/*
* Copyright 2000-2013 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.ide.hierarchy.call;
import com.intellij.ide.hierarchy.HierarchyNodeDescriptor;
import com.intellij.ide.util.treeView.NodeDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.light.LightDefaultConstructor;
import com.intellij.psi.impl.light.LightMemberReference;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
@@ -85,6 +72,9 @@ public final class JavaCallReferenceProcessor implements CallReferenceProcessor
}
PsiElement element = reference.getElement();
if (element instanceof PsiClass aClass) {
element = LightDefaultConstructor.create(aClass);
}
PsiMember key = CallHierarchyNodeDescriptor.getEnclosingElement(element);
CallHierarchyNodeDescriptor parentDescriptor = (CallHierarchyNodeDescriptor) data.getNodeDescriptor();
if (isRecursiveNode(method, parentDescriptor)) return false;
@@ -0,0 +1,9 @@
class Sweet {}
class Sour extends Sweet {}
class Salty extends Sour {}
class Bitter extends Salty {
public static void main(String[] args) {
new Bitter();
}
}
@@ -0,0 +1,9 @@
<node text="Sweet.Sweet() ()" base="true">
<node text="Sour.Sour() ()">
<node text="Salty.Salty() ()">
<node text="Bitter.Bitter() ()">
<node text="Bitter.main(String[]) ()"/>
</node>
</node>
</node>
</node>
@@ -0,0 +1,17 @@
class Sweet {}
class Sour extends Sweet {
Sour() {
}
}
class Salty extends Sour {
Salty() {
super();
}
}
class Bitter extends Salty {
public static void main(String[] args) {
new Bitter();
new Bitter();
}
}
@@ -0,0 +1,9 @@
<node text="Bitter.main(String[]) ()" base="true">
<node text="Bitter.Bitter()(2 usages) ()">
<node text="Salty.Salty() ()">
<node text="Sour.Sour() ()">
<node text="Sweet.Sweet() ()"/>
</node>
</node>
</node>
</node>
@@ -1,18 +1,3 @@
<node text="A.testMethod() ()" base="true">
<node text="Arrays.asList(T...) (java.util)"/>
<node text="Collection.stream() (java.util)">
<node text="stream() in SynchronizedCollection in Collections (java.util)"/>
<node text="stream() in CheckedCollection in Collections (java.util)"/>
<node text="stream() in UnmodifiableCollection in Collections (java.util)">
<node text="stream() in UnmodifiableEntrySet in UnmodifiableMap in Collections (java.util)"/>
</node>
<node text="stream() in SetFromMap in Collections (java.util)"/>
<node text="stream() in AsLIFOQueue in Collections (java.util)"/>
<node text="stream() in CopiesList in Collections (java.util)"/>
<node text="stream() in UnmodifiableEntrySet in UnmodifiableMap in Collections (java.util)"/>
</node>
<node text="A.returnAString(String) ()"/>
<node text="Stream.map(Function&lt;? super T, ? extends R&gt;) (java.util.stream)">
<node text="ReferencePipeline.map(Function&lt;? super P_OUT, ? extends R&gt;) (java.util.stream)"/>
</node>
</node>
@@ -13,6 +13,7 @@ import com.intellij.openapi.actionSystem.ex.ActionUtil;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.impl.light.LightDefaultConstructor;
import com.intellij.psi.search.ProjectScope;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.TestActionEvent;
@@ -97,6 +98,17 @@ public class JavaCallHierarchyTest extends HierarchyViewTestBase {
public void testDefaultConstructor() {
doCallerHierarchyTest("A", "A", "A.java");
}
public void testDefaultConstructors() {
doHierarchyTest(() -> {
PsiClass aClass = JavaPsiFacade.getInstance(getProject()).findClass("Sweet", ProjectScope.getProjectScope(getProject()));
return new CallerMethodsTreeStructure(getProject(), LightDefaultConstructor.create(aClass), HierarchyBrowserBaseEx.SCOPE_PROJECT);
}, JavaHierarchyUtil.getComparator(myProject), "Tastes.java");
}
public void testDefaultConstructorsReverse() {
doCalleeHierarchyTest("Bitter", "main", "Tastes.java");
}
public void testRecordCanonicalConstructor() {
doHierarchyTest(() -> {
@@ -115,7 +127,7 @@ public class JavaCallHierarchyTest extends HierarchyViewTestBase {
public void testRecordCanonicalConstructorReverse2() {
doHierarchyTest(() -> {
PsiClass aClass = JavaPsiFacade.getInstance(getProject()).findClass("Value", ProjectScope.getProjectScope(getProject()));
return new CalleeMethodsTreeStructure(getProject(), aClass, HierarchyBrowserBaseEx.SCOPE_PROJECT);
return new CalleeMethodsTreeStructure(getProject(), aClass, HierarchyBrowserBaseEx.SCOPE_ALL);
}, JavaHierarchyUtil.getComparator(myProject), "Value.java");
}
@@ -1,5 +1,4 @@
// Copyright 2000-2024 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.ide.hierarchy;
import com.intellij.ide.scratch.ScratchUtil;
@@ -12,10 +11,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.TestSourcesFilter;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.ElementDescriptionUtil;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.GlobalSearchScopesCore;
import com.intellij.psi.search.LocalSearchScope;
@@ -167,6 +163,7 @@ public abstract class HierarchyTreeStructure extends AbstractTreeStructure {
return module != null && module.getModuleScope().contains(virtualFile);
}
if (HierarchyBrowserBaseEx.SCOPE_PROJECT.equals(scopeType)) {
if (srcElement.getContainingFile() instanceof PsiCompiledElement) return false;
VirtualFile virtualFile = srcElement.getContainingFile().getVirtualFile();
return virtualFile == null || !TestSourcesFilter.isTestSources(virtualFile, myProject);
}
@@ -4,7 +4,5 @@
<node text="KA.foo(String) ()"/>
<node text="KA.name ()"/>
<node text="JA.JA()(2 usages) ()"/>
<node text="JA.foo(String) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String) ()"/>
</node>
@@ -6,9 +6,7 @@
<node text="packageVal ()"/>
<node text="KA.foo(String)(2 usages) ()"/>
<node text="JA.JA()(4 usages) ()"/>
<node text="JA.foo(String)(2 usages) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String)(2 usages) ()"/>
<node text="KA(4 usages) ()"/>
<node text="KClient.localFun(String) ()">
<node text="packageFun(String) ()"/>
@@ -1,9 +1,7 @@
<node text="KClient ()" base="true">
<node text="KA.name ()"/>
<node text="JA.JA()(2 usages) ()"/>
<node text="JA.foo(String) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String) ()"/>
<node text="packageFun(String)(2 usages) ()"/>
<node text="KA.foo(String) ()"/>
<node text="KA(2 usages) ()"/>
@@ -1,8 +1,6 @@
<node text="MyEnum.FIRST ()" base="true">
<node text="KA.name ()"/>
<node text="JA.foo(String) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String) ()"/>
<node text="KA(2 usages) ()"/>
<node text="KA.foo(String) ()"/>
<node text="packageFun(String)(2 usages) ()"/>
@@ -1,8 +1,6 @@
<node text="KClient.bar() ()" base="true">
<node text="JA.JA()(4 usages) ()"/>
<node text="JA.foo(String)(2 usages) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String)(2 usages) ()"/>
<node text="KA(4 usages) ()"/>
<node text="KA.foo(String)(2 usages) ()"/>
<node text="KA.name(2 usages) ()"/>
@@ -1,8 +1,6 @@
<node text="client.T ()" base="true">
<node text="JA.JA()(2 usages) ()"/>
<node text="JA.foo(String) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String) ()"/>
<node text="KA(2 usages) ()"/>
<node text="KA.foo(String) ()"/>
<node text="KA.name ()"/>
@@ -1,8 +1,6 @@
<node text="client.localFun(String) ()" base="true">
<node text="JA.JA()(2 usages) ()"/>
<node text="JA.foo(String) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String) ()"/>
<node text="KA(2 usages) ()"/>
<node text="KA.foo(String) ()"/>
<node text="KA.name ()"/>
@@ -11,9 +9,7 @@
<node text="KA.foo(String) ()"/>
<node text="KA.name ()"/>
<node text="JA.JA()(2 usages) ()"/>
<node text="JA.foo(String) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String) ()"/>
</node>
<node text="packageFun(String)(2 usages) ()"/>
</node>
@@ -1,9 +1,7 @@
<node text="KClient.T ()" base="true">
<node text="KA.name ()"/>
<node text="JA.JA()(2 usages) ()"/>
<node text="JA.foo(String) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String) ()"/>
<node text="packageFun(String)(2 usages) ()"/>
<node text="KA.foo(String) ()"/>
<node text="KA(2 usages) ()"/>
@@ -1,7 +1,5 @@
<node text="KClient ()" base="true">
<node text="JA.foo(String)(2 usages) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String)(2 usages) ()"/>
<node text="KA.name(2 usages) ()"/>
<node text="packageFun(String)(2 usages) ()"/>
<node text="packageVal ()"/>
@@ -1,9 +1,7 @@
<node text="packageFun() ()" base="true">
<node text="KA.name(2 usages) ()"/>
<node text="packageFun.localFun(String)(2 usages) ()"/>
<node text="JA.foo(String)(2 usages) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String)(2 usages) ()"/>
<node text="KA.foo(String)(3 usages) ()"/>
<node text="packageVal ()"/>
<node text="JA.JA()(4 usages) ()"/>
@@ -5,9 +5,7 @@
<node text="packageFun(String) ()"/>
</node>
<node text="KA.name(2 usages) ()"/>
<node text="JA.foo(String)(2 usages) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String)(2 usages) ()"/>
<node text="packageFun(String) ()"/>
<node text="KA(4 usages) ()"/>
</node>
@@ -8,7 +8,5 @@
<node text="packageFun(String) ()"/>
<node text="packageVal ()"/>
<node text="JA.JA()(4 usages) ()"/>
<node text="JA.foo(String)(2 usages) ()">
<node text="PrintStream.println(String) (java.io)"/>
</node>
<node text="JA.foo(String)(2 usages) ()"/>
</node>