Java: When generating return statement where the returned type is an array suggest Collection.toArray() if there's no better option (IDEA-163341)

This commit is contained in:
Pavel Dolgov
2017-01-10 12:37:42 +03:00
parent f83dec7979
commit 6b7992b4ce
7 changed files with 82 additions and 0 deletions
@@ -16,13 +16,16 @@
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
@@ -82,9 +85,33 @@ public class AddReturnFix implements IntentionAction {
return variable.getName();
}
}
// then try to find a conversion of local variable to the required type
for (PsiVariable variable : variables) {
String conversion = getConversionToType(variable, type);
if (conversion != null) {
return conversion;
}
}
return PsiTypesUtil.getDefaultValueOfType(type);
}
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 (arrayComponentType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
return variable.getName() + ".toArray()";
}
return variable.getName() + ".toArray(new " + arrayComponentType.getCanonicalText() + "[0])";
}
}
}
return null;
}
private static PsiVariable[] getDeclaredVariables(PsiMethod method) {
List<PsiVariable> variables = new ArrayList<>();
PsiStatement[] statements = method.getBody().getStatements();
@@ -0,0 +1,9 @@
// "Add 'return' statement" "true"
import java.util.*;
class T {
String[] f() {
List<String> list = new ArrayList<>();
list.add("a");
return <caret><selection>list.toArray(new String[0])</selection>;
}
}
@@ -0,0 +1,9 @@
// "Add 'return' statement" "true"
import java.util.*;
class T {
Object[] f() {
Set set = new HashSet();
set.add("a");
return <caret><selection>set.toArray()</selection>;
}
}
@@ -0,0 +1,11 @@
// "Add 'return' statement" "true"
import java.util.*;
class T {
A[] f() {
Queue<B> queue = new ArrayDeque<>();
queue.add(new B());
return <caret><selection>queue.toArray(new A[0])</selection>;
}
}
class A {}
class B extends A {}
@@ -0,0 +1,8 @@
// "Add 'return' statement" "true"
import java.util.*;
class T {
String[] f() {
List<String> list = new ArrayList<>();
list.add("a");
<caret>}
}
@@ -0,0 +1,8 @@
// "Add 'return' statement" "true"
import java.util.*;
class T {
Object[] f() {
Set set = new HashSet();
set.add("a");
<caret>}
}
@@ -0,0 +1,10 @@
// "Add 'return' statement" "true"
import java.util.*;
class T {
A[] f() {
Queue<B> queue = new ArrayDeque<>();
queue.add(new B());
<caret>}
}
class A {}
class B extends A {}