From 6b7992b4ce619f1b368c0ffe04984b19cf05b3d8 Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Tue, 10 Jan 2017 12:36:09 +0300 Subject: [PATCH] Java: When generating return statement where the returned type is an array suggest Collection.toArray() if there's no better option (IDEA-163341) --- .../daemon/impl/quickfix/AddReturnFix.java | 27 +++++++++++++++++++ .../quickFix/addReturn/afterArray1.java | 9 +++++++ .../quickFix/addReturn/afterArray2.java | 9 +++++++ .../quickFix/addReturn/afterArray3.java | 11 ++++++++ .../quickFix/addReturn/beforeArray1.java | 8 ++++++ .../quickFix/addReturn/beforeArray2.java | 8 ++++++ .../quickFix/addReturn/beforeArray3.java | 10 +++++++ 7 files changed, 82 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray1.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray2.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray3.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray1.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray2.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray3.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddReturnFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddReturnFix.java index c388e31dc603..fe3d9c86a35b 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddReturnFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddReturnFix.java @@ -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 variables = new ArrayList<>(); PsiStatement[] statements = method.getBody().getStatements(); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray1.java new file mode 100644 index 000000000000..3ecab7a7d69e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray1.java @@ -0,0 +1,9 @@ +// "Add 'return' statement" "true" +import java.util.*; +class T { + String[] f() { + List list = new ArrayList<>(); + list.add("a"); + return list.toArray(new String[0]); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray2.java new file mode 100644 index 000000000000..85a2c7b5d31c --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray2.java @@ -0,0 +1,9 @@ +// "Add 'return' statement" "true" +import java.util.*; +class T { + Object[] f() { + Set set = new HashSet(); + set.add("a"); + return set.toArray(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray3.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray3.java new file mode 100644 index 000000000000..770fd1637a4d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray3.java @@ -0,0 +1,11 @@ +// "Add 'return' statement" "true" +import java.util.*; +class T { + A[] f() { + Queue queue = new ArrayDeque<>(); + queue.add(new B()); + return queue.toArray(new A[0]); + } +} +class A {} +class B extends A {} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray1.java new file mode 100644 index 000000000000..d820fc5b7ef8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray1.java @@ -0,0 +1,8 @@ +// "Add 'return' statement" "true" +import java.util.*; +class T { + String[] f() { + List list = new ArrayList<>(); + list.add("a"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray2.java new file mode 100644 index 000000000000..8c67d8f55ede --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray2.java @@ -0,0 +1,8 @@ +// "Add 'return' statement" "true" +import java.util.*; +class T { + Object[] f() { + Set set = new HashSet(); + set.add("a"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray3.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray3.java new file mode 100644 index 000000000000..0b9617ee31c0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray3.java @@ -0,0 +1,10 @@ +// "Add 'return' statement" "true" +import java.util.*; +class T { + A[] f() { + Queue queue = new ArrayDeque<>(); + queue.add(new B()); + } +} +class A {} +class B extends A {} \ No newline at end of file