IteratorDeclaration: ListIterator support

This commit is contained in:
Tagir Valeev
2017-02-09 17:28:57 +07:00
parent 5b3b6b0dcf
commit a221c53bbf
3 changed files with 34 additions and 6 deletions
@@ -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);
}
@@ -0,0 +1,8 @@
// "Replace the loop with Collection.removeIf" "true"
import java.util.*;
public class Test {
void test(List<String> list) {
list.removeIf(String::isEmpty);
}
}
@@ -0,0 +1,13 @@
// "Replace the loop with Collection.removeIf" "true"
import java.util.*;
public class Test {
void test(List<String> list) {
ListIterator<String> iterator = list.listIterator();
while(iterator<caret>.hasNext()) {
if(iterator.next().isEmpty()) {
iterator.remove();
}
}
}
}