diff --git a/java/java-impl/src/com/intellij/codeInspection/bulkOperation/BulkMethodInfo.java b/java/java-impl/src/com/intellij/codeInspection/bulkOperation/BulkMethodInfo.java index ae912bc12ad4..6d81322c3124 100644 --- a/java/java-impl/src/com/intellij/codeInspection/bulkOperation/BulkMethodInfo.java +++ b/java/java-impl/src/com/intellij/codeInspection/bulkOperation/BulkMethodInfo.java @@ -57,19 +57,31 @@ public class BulkMethodInfo { if (!(qualifierType instanceof PsiClassType)) return false; PsiType type = iterable.getType(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(iterable.getProject()); + JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(iterable.getProject()); String text = iterable.getText(); if (type instanceof PsiArrayType) { PsiType componentType = ((PsiArrayType)type).getComponentType(); if (!useArraysAsList || componentType instanceof PsiPrimitiveType) return false; - PsiClass listClass = - JavaPsiFacade.getInstance(iterable.getProject()).findClass(CommonClassNames.JAVA_UTIL_LIST, iterable.getResolveScope()); + PsiClass listClass = psiFacade.findClass(CommonClassNames.JAVA_UTIL_LIST, iterable.getResolveScope()); if (listClass == null) return false; - type = listClass.getTypeParameters().length == 1 ? factory.createType(listClass, componentType) : factory.createType(listClass); + if (!listClass.hasTypeParameters()){ + // Raw List class - Java 1.4? + type = factory.createType(listClass); + } else if (listClass.getTypeParameters().length == 1) { + type = factory.createType(listClass, componentType); + } else { + return false; + } text = CommonClassNames.JAVA_UTIL_ARRAYS + ".asList(" + text + ")"; } - boolean isIterable = InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ITERABLE); - boolean isCollection = InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_COLLECTION); - if (!isIterable && !isCollection) return false; + PsiClass aClass = PsiUtil.resolveClassInType(type); + if (aClass == null) return false; + PsiClass commonParent = psiFacade.findClass(CommonClassNames.JAVA_LANG_ITERABLE, aClass.getResolveScope()); + if (commonParent == null) { + // No Iterable class in Java 1.4 + commonParent = psiFacade.findClass(CommonClassNames.JAVA_UTIL_COLLECTION, aClass.getResolveScope()); + } + if(!InheritanceUtil.isInheritorOrSelf(aClass, commonParent, true)) return false; PsiExpression expression = factory.createExpressionFromText(qualifier.getText() + "." + myBulkName + "(" + text + ")", iterable); if (!(expression instanceof PsiMethodCallExpression)) return false; PsiMethodCallExpression call = (PsiMethodCallExpression)expression; @@ -81,8 +93,8 @@ public class BulkMethodInfo { parameterType = call.resolveMethodGenerics().getSubstitutor().substitute(parameterType); PsiClass parameterClass = PsiUtil.resolveClassInClassTypeOnly(parameterType); return parameterClass != null && - (CommonClassNames.JAVA_LANG_ITERABLE.equals(parameterClass.getQualifiedName()) && isIterable || - CommonClassNames.JAVA_UTIL_COLLECTION.equals(parameterClass.getQualifiedName()) && isCollection) && + (CommonClassNames.JAVA_LANG_ITERABLE.equals(parameterClass.getQualifiedName()) || + CommonClassNames.JAVA_UTIL_COLLECTION.equals(parameterClass.getQualifiedName())) && parameterType.isAssignableFrom(type); } diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/UseBulkOperationInspectionJava14Test.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/UseBulkOperationInspectionJava14Test.java new file mode 100644 index 000000000000..32055cd8ea42 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/UseBulkOperationInspectionJava14Test.java @@ -0,0 +1,37 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.daemon.quickFix; + +import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.pom.java.LanguageLevel; +import com.intellij.testFramework.IdeaTestUtil; + +public class UseBulkOperationInspectionJava14Test extends UseBulkOperationInspectionTest { + @Override + protected String getBasePath() { + return super.getBasePath()+"/java14"; + } + + @Override + protected LanguageLevel getLanguageLevel() { + return LanguageLevel.JDK_1_4; + } + + @Override + protected Sdk getProjectJDK() { + return IdeaTestUtil.getMockJdk14(); + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/UseBulkOperationInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/UseBulkOperationInspectionTest.java index 2c90a6302eed..b5def3490aa8 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/UseBulkOperationInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/UseBulkOperationInspectionTest.java @@ -20,9 +20,6 @@ import com.intellij.codeInspection.bulkOperation.BulkMethodInfo; import com.intellij.codeInspection.bulkOperation.BulkMethodInfoProvider; import com.intellij.codeInspection.bulkOperation.UseBulkOperationInspection; import com.intellij.openapi.extensions.Extensions; -import com.intellij.openapi.projectRoots.Sdk; -import com.intellij.pom.java.LanguageLevel; -import com.intellij.testFramework.IdeaTestUtil; import com.intellij.testFramework.PlatformTestUtil; import org.jetbrains.annotations.NotNull; @@ -61,21 +58,4 @@ public class UseBulkOperationInspectionTest extends LightQuickFixParameterizedTe protected String getBasePath() { return "/codeInsight/daemonCodeAnalyzer/quickFix/useBulkOperation"; } - - public static class UseBulkOperationJava14Test extends UseBulkOperationInspectionTest { - @Override - protected String getBasePath() { - return super.getBasePath()+"/java14"; - } - - @Override - protected LanguageLevel getLanguageLevel() { - return LanguageLevel.JDK_1_4; - } - - @Override - protected Sdk getProjectJDK() { - return IdeaTestUtil.getMockJdk14(); - } - } } \ No newline at end of file