mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
method duplicates: qualify match in order to prevent name conflict ( IDEA-24764 )
This commit is contained in:
+23
-6
@@ -232,7 +232,9 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler {
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory();
|
||||
final boolean needQualifier = match.getInstanceExpression() != null;
|
||||
final boolean needStaticQualifier = isExternal(match);
|
||||
@NonNls final String text = needQualifier || needStaticQualifier ? "q." + myMethod.getName() + "()": myMethod.getName() + "()";
|
||||
final boolean nameConflicts = nameConflicts(match);
|
||||
@NonNls final String text = needQualifier || needStaticQualifier || nameConflicts
|
||||
? "q." + myMethod.getName() + "()": myMethod.getName() + "()";
|
||||
PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)factory.createExpressionFromText(text, null);
|
||||
methodCallExpression = (PsiMethodCallExpression)CodeStyleManager.getInstance(myMethod.getManager()).reformat(methodCallExpression);
|
||||
final PsiParameter[] parameters = myMethod.getParameterList().getParameters();
|
||||
@@ -247,13 +249,15 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler {
|
||||
methodCallExpression.getArgumentList().add(factory.createExpressionFromText(PsiTypesUtil.getDefaultValueOfType(parameter.getType()), parameter));
|
||||
}
|
||||
}
|
||||
if (needQualifier || needStaticQualifier) {
|
||||
if (needQualifier || needStaticQualifier || nameConflicts) {
|
||||
final PsiExpression qualifierExpression = methodCallExpression.getMethodExpression().getQualifierExpression();
|
||||
LOG.assertTrue(qualifierExpression != null);
|
||||
if (needQualifier) {
|
||||
qualifierExpression.replace(match.getInstanceExpression());
|
||||
} else {
|
||||
} else if (needStaticQualifier || myMethod.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
qualifierExpression.replace(factory.createReferenceExpression(containingClass));
|
||||
} else {
|
||||
qualifierExpression.replace(RefactoringUtil.createThisExpression(containingClass.getManager(), containingClass));
|
||||
}
|
||||
}
|
||||
VisibilityUtil.escalateVisibility(myMethod, match.getMatchStart());
|
||||
@@ -276,16 +280,29 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler {
|
||||
|
||||
|
||||
private boolean isExternal(final Match match) {
|
||||
if (PsiTreeUtil.isAncestor(myMethod.getContainingClass(), match.getMatchStart(), false)) {
|
||||
final PsiElement matchStart = match.getMatchStart();
|
||||
final PsiClass containingClass = myMethod.getContainingClass();
|
||||
if (PsiTreeUtil.isAncestor(containingClass, matchStart, false)) {
|
||||
return false;
|
||||
}
|
||||
final PsiClass psiClass = PsiTreeUtil.getParentOfType(match.getMatchStart(), PsiClass.class);
|
||||
final PsiClass psiClass = PsiTreeUtil.getParentOfType(matchStart, PsiClass.class);
|
||||
if (psiClass != null) {
|
||||
if (InheritanceUtil.isInheritorOrSelf(psiClass, myMethod.getContainingClass(), true)) return false;
|
||||
if (InheritanceUtil.isInheritorOrSelf(psiClass, containingClass, true)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean nameConflicts(Match match) {
|
||||
PsiClass matchClass = PsiTreeUtil.getParentOfType(match.getMatchStart(), PsiClass.class);
|
||||
while (matchClass != null && matchClass != myMethod.getContainingClass()) {
|
||||
if (matchClass.findMethodsBySignature(myMethod, false).length > 0) {
|
||||
return true;
|
||||
}
|
||||
matchClass = PsiTreeUtil.getParentOfType(matchClass, PsiClass.class);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isEssentialStaticContextAbsent(final Match match) {
|
||||
if (!myMethod.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
final PsiExpression instanceExpression = match.getInstanceExpression();
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
class Test {
|
||||
public Foo createFoo() {
|
||||
return new Foo() {
|
||||
public void bar() {
|
||||
System.out.println(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void b<caret>ar() {
|
||||
System.out.println(1);
|
||||
}
|
||||
|
||||
public interface Foo {
|
||||
void bar();
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class Test {
|
||||
public Foo createFoo() {
|
||||
return new Foo() {
|
||||
public void bar() {
|
||||
Test.this.bar();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void bar() {
|
||||
System.out.println(1);
|
||||
}
|
||||
|
||||
public interface Foo {
|
||||
void bar();
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class Test {
|
||||
public Foo createFoo() {
|
||||
return new Foo() {
|
||||
public void bar() {
|
||||
System.out.println(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void b<caret>ar() {
|
||||
System.out.println(1);
|
||||
}
|
||||
|
||||
public interface Foo {
|
||||
void bar();
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class Test {
|
||||
public Foo createFoo() {
|
||||
return new Foo() {
|
||||
public void bar() {
|
||||
Test.bar();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void bar() {
|
||||
System.out.println(1);
|
||||
}
|
||||
|
||||
public interface Foo {
|
||||
void bar();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
class Test {
|
||||
public Foo createFoo() {
|
||||
return new Foo() {
|
||||
public void bar() {
|
||||
System.out.println(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void b<caret>ar(int i) {
|
||||
System.out.println(i);
|
||||
}
|
||||
|
||||
public interface Foo {
|
||||
void bar();
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class Test {
|
||||
public Foo createFoo() {
|
||||
return new Foo() {
|
||||
public void bar() {
|
||||
bar(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void bar(int i) {
|
||||
System.out.println(i);
|
||||
}
|
||||
|
||||
public interface Foo {
|
||||
void bar();
|
||||
}
|
||||
}
|
||||
@@ -37,4 +37,16 @@ public class FindMethodDuplicatesMiscTest extends FindMethodDuplicatesBaseTest {
|
||||
public void testPassArray2VarargMethodCall() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testDetectNameConflicts() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNoDetectNameConflicts() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testDetectNameConflictsWithStatic() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user