From 14b3a1b38fbf26abfeaeba5c717d70044c81f675 Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Mon, 30 Jan 2017 13:36:50 +0300 Subject: [PATCH] Java: When generating return statement where the returned type is an array use raw type in array creation (IDEA-163341) --- .../daemon/impl/quickfix/AddReturnFix.java | 24 +++++++++++++------ .../com/intellij/psi/util/PsiTypesUtil.java | 3 ++- .../quickFix/addReturn/afterArray7.java | 8 +++++++ .../quickFix/addReturn/afterArray8.java | 7 ++++++ .../quickFix/addReturn/afterArray9.java | 8 +++++++ .../quickFix/addReturn/beforeArray7.java | 7 ++++++ .../quickFix/addReturn/beforeArray8.java | 6 +++++ .../quickFix/addReturn/beforeArray9.java | 7 ++++++ 8 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray7.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray8.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray9.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray7.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray8.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray9.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 f5898c5bb04c..e145d2a3239e 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 @@ -24,6 +24,7 @@ import com.intellij.psi.*; import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PsiTypesUtil; import com.intellij.psi.util.PsiUtil; +import com.intellij.psi.util.TypeConversionUtil; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -88,7 +89,13 @@ public class AddReturnFix implements IntentionAction { } // then try to find a conversion of local variable to the required type for (PsiVariable variable : variables) { - String conversion = getConversionToType(variable, type); + String conversion = getConversionToType(variable, type, true); + if (conversion != null) { + return conversion; + } + } + for (PsiVariable variable : variables) { + String conversion = getConversionToType(variable, type, false); if (conversion != null) { return conversion; } @@ -96,19 +103,22 @@ public class AddReturnFix implements IntentionAction { return PsiTypesUtil.getDefaultValueOfType(type); } - private String getConversionToType(@NotNull PsiVariable variable, @Nullable PsiType type) { + private String getConversionToType(@NotNull PsiVariable variable, @Nullable PsiType type, boolean preciseTypeReqired) { PsiType varType = variable.getType(); if (type instanceof PsiArrayType) { PsiType arrayComponentType = ((PsiArrayType)type).getComponentType(); if (!(arrayComponentType instanceof PsiPrimitiveType) && !(PsiUtil.resolveClassInType(arrayComponentType) instanceof PsiTypeParameter) && 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()"; + PsiType erasedComponentType = TypeConversionUtil.erasure(arrayComponentType); + if (!preciseTypeReqired || arrayComponentType.equals(erasedComponentType)) { + PsiType collectionItemType = JavaGenericsUtil.getCollectionItemType(varType, myMethod.getResolveScope()); + if (collectionItemType != null && erasedComponentType.isAssignableFrom(collectionItemType)) { + if (erasedComponentType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) { + return variable.getName() + ".toArray()"; + } + return variable.getName() + ".toArray(new " + erasedComponentType.getCanonicalText() + "[0])"; } - return variable.getName() + ".toArray(new " + arrayComponentType.getCanonicalText() + "[0])"; } } } diff --git a/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java b/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java index 3a33e1b834b0..0c6e35c17eb8 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java @@ -74,10 +74,11 @@ public class PsiTypesUtil { } } + PsiType erasedComponentType = TypeConversionUtil.erasure(componentType); StringBuilder buffer = new StringBuilder(); buffer.append(PsiKeyword.NEW); buffer.append(" "); - buffer.append(componentType.getCanonicalText()); + buffer.append(erasedComponentType.getCanonicalText()); buffer.append("[0]"); for (int i = 0; i < count; i++) { buffer.append("[]"); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray7.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray7.java new file mode 100644 index 000000000000..d914e631cc60 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray7.java @@ -0,0 +1,8 @@ +// "Add 'return' statement" "true" +import java.util.*; +class A { + List[] f(T a, T b) { + List> list = Arrays.asList(Collections.singletonList(a), Collections.singletonList(b)); + return list.toArray(new List[0]); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray8.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray8.java new file mode 100644 index 000000000000..cfe4c224da7f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray8.java @@ -0,0 +1,7 @@ +// "Add 'return' statement" "true" +import java.util.*; +class A { + List[] f() { + return new List[0]; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray9.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray9.java new file mode 100644 index 000000000000..ab7016dbe43b --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/afterArray9.java @@ -0,0 +1,8 @@ +// "Add 'return' statement" "true" +import java.util.*; +class A { + Object[] f() { + List list = new ArrayList<>(); + return list.toArray(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray7.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray7.java new file mode 100644 index 000000000000..4edc1013c2a4 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray7.java @@ -0,0 +1,7 @@ +// "Add 'return' statement" "true" +import java.util.*; +class A { + List[] f(T a, T b) { + List> list = Arrays.asList(Collections.singletonList(a), Collections.singletonList(b)); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray8.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray8.java new file mode 100644 index 000000000000..1dad438eda67 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray8.java @@ -0,0 +1,6 @@ +// "Add 'return' statement" "true" +import java.util.*; +class A { + List[] f() { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray9.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray9.java new file mode 100644 index 000000000000..550781bdd6ac --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addReturn/beforeArray9.java @@ -0,0 +1,7 @@ +// "Add 'return' statement" "true" +import java.util.*; +class A { + Object[] f() { + List list = new ArrayList<>(); + } +} \ No newline at end of file