Java: When generating return statement where the returned type is a primitive array don't suggest Collection.toArray() (IDEA-163341)

This commit is contained in:
Pavel Dolgov
2017-01-11 12:33:51 +03:00
parent fa68de288b
commit af46d7d411
5 changed files with 40 additions and 5 deletions
@@ -97,11 +97,12 @@ public class AddReturnFix implements IntentionAction {
private String getConversionToType(@NotNull PsiVariable variable, @Nullable PsiType type) {
PsiType varType = variable.getType();
if (type instanceof PsiArrayType && InheritanceUtil.isInheritor(varType, CommonClassNames.JAVA_UTIL_COLLECTION)) {
PsiType collectionItemType = JavaGenericsUtil.getCollectionItemType(varType, myMethod.getResolveScope());
if (collectionItemType != null) {
PsiType arrayComponentType = ((PsiArrayType)type).getComponentType();
if (arrayComponentType.isAssignableFrom(collectionItemType)) {
if (type instanceof PsiArrayType) {
PsiType arrayComponentType = ((PsiArrayType)type).getComponentType();
if (!(arrayComponentType instanceof PsiPrimitiveType) &&
InheritanceUtil.isInheritor(varType, CommonClassNames.JAVA_UTIL_COLLECTION)) {
PsiType collectionItemType = JavaGenericsUtil.getCollectionItemType(varType, myMethod.getResolveScope());
if (collectionItemType != null && arrayComponentType.isAssignableFrom(collectionItemType)) {
if (arrayComponentType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
return variable.getName() + ".toArray()";
}
@@ -0,0 +1,9 @@
// "Add 'return' statement" "true"
import java.util.*;
class T {
String[] f() {
List<String> list = Arrays.asList("a", "b");
String[] arr = {"c", "d"};
return arr;
}
}
@@ -0,0 +1,9 @@
// "Add 'return' statement" "true"
import java.util.*;
class T {
int[] f() {
Set<Integer> set = new HashSet<>();
set.add(42);
return new int[0];
}
}
@@ -0,0 +1,8 @@
// "Add 'return' statement" "true"
import java.util.*;
class T {
String[] f() {
List<String> list = Arrays.asList("a", "b");
String[] arr = {"c", "d"};
<caret>}
}
@@ -0,0 +1,8 @@
// "Add 'return' statement" "true"
import java.util.*;
class T {
int[] f() {
Set<Integer> set = new HashSet<>();
set.add(42);
<caret>}
}