diff --git a/java/java-impl/src/com/intellij/codeInspection/util/IteratorDeclaration.java b/java/java-impl/src/com/intellij/codeInspection/util/IteratorDeclaration.java index 7f01b85c6ed7..7f63ac37ab24 100644 --- a/java/java-impl/src/com/intellij/codeInspection/util/IteratorDeclaration.java +++ b/java/java-impl/src/com/intellij/codeInspection/util/IteratorDeclaration.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * 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. @@ -105,12 +105,19 @@ public class IteratorDeclaration { PsiMethodCallExpression call = (PsiMethodCallExpression)initializer; if (call.getArgumentList().getExpressions().length != 0) return null; PsiReferenceExpression methodExpression = call.getMethodExpression(); - if (!"iterator".equals(methodExpression.getReferenceName())) return null; + boolean listIterator = "listIterator".equals(methodExpression.getReferenceName()); + if (!"iterator".equals(methodExpression.getReferenceName()) && !listIterator) return null; PsiMethod method = call.resolveMethod(); - if (method == null || !InheritanceUtil.isInheritor(method.getContainingClass(), CommonClassNames.JAVA_LANG_ITERABLE)) return null; - boolean isCollection = InheritanceUtil.isInheritor(method.getContainingClass(), CommonClassNames.JAVA_UTIL_COLLECTION); - PsiType type = variable.getType(); - if (!(type instanceof PsiClassType) || !((PsiClassType)type).rawType().equalsToText(CommonClassNames.JAVA_UTIL_ITERATOR)) return null; + if (method == null) return null; + boolean isCollection; + if (listIterator) { + if (!InheritanceUtil.isInheritor(method.getContainingClass(), CommonClassNames.JAVA_UTIL_LIST)) return null; + isCollection = true; + } else { + if (!InheritanceUtil.isInheritor(method.getContainingClass(), CommonClassNames.JAVA_LANG_ITERABLE)) return null; + isCollection = InheritanceUtil.isInheritor(method.getContainingClass(), CommonClassNames.JAVA_UTIL_COLLECTION); + } + if (!InheritanceUtil.isInheritor(variable.getType(), CommonClassNames.JAVA_UTIL_ITERATOR)) return null; return new IteratorDeclaration(variable, methodExpression.getQualifierExpression(), isCollection); } diff --git a/java/java-tests/testData/inspection/java8CollectionRemoveIf/afterIteratorRemoveListIterator.java b/java/java-tests/testData/inspection/java8CollectionRemoveIf/afterIteratorRemoveListIterator.java new file mode 100644 index 000000000000..95ed02398e16 --- /dev/null +++ b/java/java-tests/testData/inspection/java8CollectionRemoveIf/afterIteratorRemoveListIterator.java @@ -0,0 +1,8 @@ +// "Replace the loop with Collection.removeIf" "true" +import java.util.*; + +public class Test { + void test(List list) { + list.removeIf(String::isEmpty); + } +} diff --git a/java/java-tests/testData/inspection/java8CollectionRemoveIf/beforeIteratorRemoveListIterator.java b/java/java-tests/testData/inspection/java8CollectionRemoveIf/beforeIteratorRemoveListIterator.java new file mode 100644 index 000000000000..bcb73597ed63 --- /dev/null +++ b/java/java-tests/testData/inspection/java8CollectionRemoveIf/beforeIteratorRemoveListIterator.java @@ -0,0 +1,13 @@ +// "Replace the loop with Collection.removeIf" "true" +import java.util.*; + +public class Test { + void test(List list) { + ListIterator iterator = list.listIterator(); + while(iterator.hasNext()) { + if(iterator.next().isEmpty()) { + iterator.remove(); + } + } + } +}