mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: Don't fold parameters of extracted method when there are duplicates that might use unfolded parameters (IDEA-189023)
This commit is contained in:
+11
-2
@@ -53,16 +53,16 @@ public class JavaDuplicatesExtractMethodProcessor extends ExtractMethodProcessor
|
||||
myArtificialOutputVariable = variablesMapping.getOrDefault(from.myArtificialOutputVariable, from.myArtificialOutputVariable);
|
||||
|
||||
List<VariableData> variableDatum = new ArrayList<>();
|
||||
List<VariableData> inputVariables = getInputVariables().getInputVariables();
|
||||
for (int i = 0; i < from.myVariableDatum.length; i++) {
|
||||
VariableData fromData = from.myVariableDatum[i];
|
||||
PsiVariable mappedVariable = variablesMapping.get(fromData.variable);
|
||||
if (isReferenced(mappedVariable, fromData.variable)) {
|
||||
if (isReferenced(mappedVariable, fromData.variable) && isUnchanged(mappedVariable, fromData.type, inputVariables)) {
|
||||
VariableData newData = fromData.substitute(mappedVariable);
|
||||
variableDatum.add(newData);
|
||||
}
|
||||
}
|
||||
Set<PsiVariable> parameterVariables = ContainerUtil.map2Set(variableDatum, data -> data.variable);
|
||||
List<VariableData> inputVariables = getInputVariables().getInputVariables();
|
||||
for (VariableData data : inputVariables) {
|
||||
if (!parameterVariables.contains(data.variable)) {
|
||||
variableDatum.add(data);
|
||||
@@ -71,6 +71,15 @@ public class JavaDuplicatesExtractMethodProcessor extends ExtractMethodProcessor
|
||||
myVariableDatum = variableDatum.toArray(new VariableData[0]);
|
||||
}
|
||||
|
||||
private static boolean isUnchanged(PsiVariable fromVariable, PsiType fromType, @NotNull List<VariableData> inputVariables) {
|
||||
for (VariableData data : inputVariables) {
|
||||
if (data.variable == fromVariable) {
|
||||
return data.type != null && data.type.equalsToText(fromType.getCanonicalText());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void applyFromSnapshot(@NotNull ExtractMethodSnapshot from) {
|
||||
myMethodName = from.myMethodName;
|
||||
myStatic = from.myStatic;
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class DeclaredOutputVariable {
|
||||
void foo(String[] a) {
|
||||
<selection>
|
||||
String s = a[1];
|
||||
if (s == null) return;
|
||||
System.out.println(s.charAt(1));
|
||||
</selection>
|
||||
System.out.println(s.length());
|
||||
}
|
||||
|
||||
void bar(String[] a) {
|
||||
String s = a[2];
|
||||
if (s == null) return;
|
||||
System.out.println(s.charAt(2));
|
||||
System.out.println(s.length());
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class DeclaredOutputVariable {
|
||||
void foo(String[] a) {
|
||||
|
||||
String s = newMethod(a, 1);
|
||||
if (s == null) return;
|
||||
|
||||
System.out.println(s.length());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String newMethod(String[] a, int i) {
|
||||
String s = a[i];
|
||||
if (s == null) return null;
|
||||
System.out.println(s.charAt(i));
|
||||
return s;
|
||||
}
|
||||
|
||||
void bar(String[] a) {
|
||||
String s = newMethod(a, 2);
|
||||
if (s == null) return;
|
||||
System.out.println(s.length());
|
||||
}
|
||||
}
|
||||
@@ -889,6 +889,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doDuplicatesTest();
|
||||
}
|
||||
|
||||
public void testParametrizedDuplicateUnfoldArrayArgument() throws Exception {
|
||||
doDuplicatesTest();
|
||||
}
|
||||
|
||||
public void testSuggestChangeSignatureWithChangedParameterName() throws Exception {
|
||||
configureByFile(BASE_PATH + getTestName(false) + ".java");
|
||||
boolean success = performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, null, false, "p");
|
||||
|
||||
Reference in New Issue
Block a user