[extract duplicates] fix: don't suggest change signature for expressions with local refs

GitOrigin-RevId: 58b7c2debe3960bbe2c64a1cbc521a8ebd872a1a
This commit is contained in:
Alexandr Suhinin
2021-11-12 12:02:34 +02:00
committed by intellij-monorepo-bot
parent 5371a9a6f6
commit 1210f80627
4 changed files with 40 additions and 3 deletions

View File

@@ -112,7 +112,8 @@ class JavaDuplicatesFinder(pattern: List<PsiElement>, private val predefinedChan
return@filterNot true
}
if (ExtractMethodHelper.hasReferencesToScope(duplicate.pattern, changedExpressions.map{ change -> change.pattern })){
if (ExtractMethodHelper.hasReferencesToScope(duplicate.pattern, changedExpressions.map{ change -> change.pattern }) ||
ExtractMethodHelper.hasReferencesToScope(duplicate.candidate, changedExpressions.map { change -> change.candidate })){
return null
}
@@ -152,13 +153,13 @@ class JavaDuplicatesFinder(pattern: List<PsiElement>, private val predefinedChan
return when {
pattern.parent is PsiExpressionStatement -> false
pattern is PsiReferenceExpression && pattern.parent is PsiCall -> false
pattern is PsiExpression && candidate is PsiExpression -> canBeReplaced(pattern.type, candidate.type)
pattern is PsiExpression && candidate is PsiExpression -> pattern.type != PsiType.VOID && canBeReplaced(pattern.type, candidate.type)
else -> false
}
}
private fun canBeReplaced(pattern: PsiType?, candidate: PsiType?): Boolean {
return pattern != null && pattern != PsiType.VOID && candidate != null && pattern.isAssignableFrom(candidate)
return pattern != null && candidate != null && pattern.isAssignableFrom(candidate)
}
private fun isOvercomplicated(duplicate: Duplicate): Boolean {

View File

@@ -0,0 +1,14 @@
public class Test{
void test1(){
<selection>String local = "local";
System.out.println("one");
System.out.println("two");</selection>
}
void test2(){
String local = "local";
System.out.println("one");
System.out.println(local.toLowerCase());
}
}

View File

@@ -0,0 +1,18 @@
public class Test{
void test1(){
extracted();
}
private void extracted() {
String local = "local";
System.out.println("one");
System.out.println("two");
}
void test2(){
String local = "local";
System.out.println("one");
System.out.println(local.toLowerCase());
}
}

View File

@@ -186,6 +186,10 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
doTest()
}
fun testAvoidChangeSignatureForLocalRefs(){
doTest()
}
fun testRefactoringListener(){
templateTest {
configureByFile("$BASE_PATH/${getTestName(false)}.java")