mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
check super call is still meaninfull after inline method (IDEADEV-39914)
This commit is contained in:
@@ -24,6 +24,7 @@ import com.intellij.openapi.editor.LogicalPosition;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
@@ -46,6 +47,7 @@ import com.intellij.refactoring.util.*;
|
||||
import com.intellij.usageView.UsageInfo;
|
||||
import com.intellij.usageView.UsageViewDescriptor;
|
||||
import com.intellij.usageView.UsageViewUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
@@ -120,8 +122,8 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
}
|
||||
|
||||
protected boolean preprocessUsages(Ref<UsageInfo[]> refUsages) {
|
||||
UsageInfo[] usagesIn = refUsages.get();
|
||||
MultiMap<PsiElement, String> conflicts = new MultiMap<PsiElement, String>();
|
||||
final UsageInfo[] usagesIn = refUsages.get();
|
||||
final MultiMap<PsiElement, String> conflicts = new MultiMap<PsiElement, String>();
|
||||
|
||||
if (!myInlineThisOnly) {
|
||||
final PsiMethod[] superMethods = myMethod.findSuperMethods();
|
||||
@@ -135,6 +137,8 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
|
||||
addInaccessibleMemberConflicts(myMethod, usagesIn, new ReferencedElementsCollector(), conflicts);
|
||||
|
||||
addInaccessibleSuperCallsConflicts(usagesIn, conflicts);
|
||||
|
||||
if (!conflicts.isEmpty()) {
|
||||
ConflictsDialog dialog = new ConflictsDialog(myProject, conflicts);
|
||||
dialog.show();
|
||||
@@ -152,6 +156,39 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void addInaccessibleSuperCallsConflicts(final UsageInfo[] usagesIn, final MultiMap<PsiElement, String> conflicts) {
|
||||
myMethod.accept(new JavaRecursiveElementWalkingVisitor(){
|
||||
@Override
|
||||
public void visitSuperExpression(PsiSuperExpression expression) {
|
||||
super.visitSuperExpression(expression);
|
||||
final PsiType type = expression.getType();
|
||||
final PsiClass superClass = PsiUtil.resolveClassInType(type);
|
||||
if (superClass != null) {
|
||||
final Set<PsiClass> targetContainingClasses = new HashSet<PsiClass>();
|
||||
for (UsageInfo info : usagesIn) {
|
||||
final PsiElement element = info.getElement();
|
||||
if (element != null) {
|
||||
final PsiClass targetContainingClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
|
||||
if (targetContainingClass != null && !InheritanceUtil.isInheritorOrSelf(targetContainingClass, superClass, true)) {
|
||||
targetContainingClasses.add(targetContainingClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!targetContainingClasses.isEmpty()) {
|
||||
final PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(expression, PsiMethodCallExpression.class);
|
||||
LOG.assertTrue(methodCallExpression != null);
|
||||
conflicts.putValue(expression, "Inlined method calls " + methodCallExpression.getText() + " which won't be accessed in " +
|
||||
StringUtil.join(targetContainingClasses, new Function<PsiClass, String>() {
|
||||
public String fun(PsiClass psiClass) {
|
||||
return RefactoringUIUtil.getDescription(psiClass, false);
|
||||
}
|
||||
}, ","));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void addInaccessibleMemberConflicts(final PsiElement element,
|
||||
final UsageInfo[] usages,
|
||||
final ReferencedElementsCollector collector,
|
||||
@@ -448,7 +485,7 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
else {
|
||||
thisAccessExpr = null;
|
||||
}
|
||||
ChangeContextUtil.decodeContextInfo(anchorParent, thisClass, thisAccessExpr);//todo super should be encoded decoded as well
|
||||
ChangeContextUtil.decodeContextInfo(anchorParent, thisClass, thisAccessExpr);
|
||||
|
||||
if (thisVar != null) {
|
||||
inlineParmOrThisVariable(thisVar, false);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
class A {
|
||||
void foo(){
|
||||
//do smth in A
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
void <caret>bar() {
|
||||
super.foo();
|
||||
}
|
||||
|
||||
void test(){
|
||||
bar();
|
||||
}
|
||||
|
||||
void foo() {
|
||||
//do smth
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
class A {
|
||||
void foo(){
|
||||
//do smth in A
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
|
||||
void test(){
|
||||
super.foo();
|
||||
}
|
||||
|
||||
void foo() {
|
||||
//do smth
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,10 @@ public class InlineMethodTest extends LightCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testSuperInsideHierarchy() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testSideEffect() throws Exception { doTest(); }
|
||||
|
||||
public void testInlineWithTry() throws Exception { doTest(); }
|
||||
|
||||
Reference in New Issue
Block a user