[extract method] IDEA-318013: check explicit parent of identifier instead of search

GitOrigin-RevId: d632f6dfffdbfe8912882f30c9bd203e95362a54
This commit is contained in:
Alexandr Suhinin
2023-04-14 09:46:34 +00:00
committed by intellij-monorepo-bot
parent fe6f93dcc9
commit f2ff1ff151
4 changed files with 56 additions and 5 deletions
@@ -97,14 +97,13 @@ object InplaceExtractUtils {
if (!checkIdentifierName(editor, file, variableRange)) {
return false
}
val reference = PsiTreeUtil.findElementOfClassAtOffset(file, variableRange.startOffset, PsiReferenceExpression::class.java, false)
if (reference != null && reference.multiResolve(false).size != 1) {
val identifier = PsiTreeUtil.findElementOfClassAtOffset(file, variableRange.startOffset, PsiIdentifier::class.java, false)
val parent = identifier?.parent
if (parent is PsiReferenceExpression && parent.multiResolve(false).size != 1) {
showErrorHint(editor, variableRange.endOffset, JavaRefactoringBundle.message("extract.method.error.method.conflict"))
return false
}
val member = PsiTreeUtil.findElementOfClassAtOffset(file, variableRange.startOffset, PsiMember::class.java, false)
val parentClass = member?.containingClass
if (member is PsiMethod && parentClass != null && parentClass.findMethodsBySignature(member, true).size > 1) {
if (parent is PsiMethod && parent.containingClass?.findMethodsBySignature(parent, true).orEmpty().size > 1) {
showErrorHint(editor, variableRange.endOffset, JavaRefactoringBundle.message("extract.method.error.method.conflict"))
return false
}
@@ -0,0 +1,21 @@
package org.example;
import java.io.File;
import java.util.Collection;
class Parent {
public boolean test(Collection<File> files) {
return false;
}
}
class Child extends Parent {
@Override
public boolean test(Collection<File> files) {
return files.stream().allMatch(file -> {
String name = file.getName();
return <selection>name.endsWith("jar") || name.endsWith("tar")</selection>;
});
}
}
@@ -0,0 +1,25 @@
package org.example;
import java.io.File;
import java.util.Collection;
class Parent {
public boolean test(Collection<File> files) {
return false;
}
}
class Child extends Parent {
@Override
public boolean test(Collection<File> files) {
return files.stream().allMatch(file -> {
String name = file.getName();
return isaBoolean(name);
});
}
private static boolean isaBoolean(String name) {
return name.endsWith("jar") || name.endsWith("tar");
}
}
@@ -509,6 +509,12 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
doTest()
}
fun testCheckNameExtractedFromLambda(){
doTest {
nextTemplateVariable()
}
}
fun testRefactoringListener(){
templateTest {
configureByFile("$BASE_PATH/${getTestName(false)}.java")