Java: Add conditional return when replacing duplicate fragment (IDEA-180601)

This commit is contained in:
Pavel Dolgov
2017-10-16 17:41:29 +03:00
parent 5f1e6fc3fa
commit 16cbf02774
6 changed files with 123 additions and 1 deletions
@@ -0,0 +1,24 @@
class Conditional {
int[] bar(String[] s) {
<selection>
if (s != null) {
int[] n = new int[s.length];
for (int i = 0; i < s.length; i++) {
n[i] = s[i].length();
}
return n;
}</selection>
return new int[0];
}
int[] baz(String[] z) {
if (z != null) {
int[] n = new int[z.length];
for (int i = 0; i < z.length; i++) {
n[i] = z[i].length();
}
return n;
}
return new int[0];
}
}
@@ -0,0 +1,25 @@
class Conditional {
int[] bar(String[] s) {
int[] n = newMethod(s);
if (n != null) return n;
return new int[0];
}
private int[] newMethod(String[] s) {
if (s != null) {
int[] n = new int[s.length];
for (int i = 0; i < s.length; i++) {
n[i] = s[i].length();
}
return n;
}
return null;
}
int[] baz(String[] z) {
int[] n = newMethod(z);
if (n != null) return n;
return new int[0];
}
}
@@ -0,0 +1,18 @@
class Conditional {
int bar(String s) {<selection>
if (s != null) {
int n = s.length;
return n;
}</selection>
return 0;
}
int baz(String z) {
int x = -1;
if (z != null) {
int n = z.length;
x = n;
}
return 0;
}
}
@@ -0,0 +1,27 @@
import org.jetbrains.annotations.Nullable;
class Conditional {
int bar(String s) {
Integer n = newMethod(s);
if (n != null) return n;
return 0;
}
@Nullable
private Integer newMethod(String s) {
if (s != null) {
int n = s.length;
return n;
}
return null;
}
int baz(String z) {
int x = -1;
if (z != null) {
int n = z.length;
x = n;
}
return 0;
}
}
@@ -843,6 +843,15 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doDuplicatesTest();
}
public void testConditionalReturnInDuplicate() throws Exception {
doDuplicatesTest();
}
// todo DuplicatesFinder.canBeEquivalent() should see the difference between 'return' and assignment
public void _testConditionalReturnVsAssignDuplicate() 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");