IDEA-165942 Inspection to replace method call in a loop with bulk operation

This commit is contained in:
Tagir Valeev
2016-12-30 13:01:31 +07:00
parent 53ebd17f38
commit 01d7ee7d3c
39 changed files with 813 additions and 76 deletions
@@ -1,12 +1,13 @@
// "Replace with addAll" "true"
// "Replace with collect" "false"
import java.util.*;
public class Collect {
class Person {}
void collectNames(List<Person> persons){
// Handled by UseBulkOperationInspection
List<Person> names = new ArrayList<>();
for (Person person : pers<caret>ons) {
for (Person person : p<caret>ersons) {
names.add(person);
}
}
@@ -0,0 +1,10 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.*;
public class Main {
public void test(Integer[] arr) {
List<Integer> result = new ArrayList<>();
result.add(1);
result.addAll(Arrays.asList(arr));
}
}
@@ -0,0 +1,11 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.*;
public class Collect {
class Person {}
void collectNames(List<Person> persons){
List<Person> names = new ArrayList<>();
names.addAll(persons);
}
}
@@ -1,4 +1,4 @@
// "Replace with addAll" "true"
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.*;
@@ -0,0 +1,11 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.*;
public class Collect {
class Person {}
void collectNames(List<Person> persons){
List<Person> names = new ArrayList<>();
names.addAll(persons);
}
}
@@ -1,4 +1,4 @@
// "Replace with addAll" "true"
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.ArrayList;
import java.util.List;
@@ -1,4 +1,4 @@
// "Replace with addAll" "true"
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -0,0 +1,12 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public void test(Integer[] arr) {
List<Integer> result = new ArrayList<>();
result.add(1);
result.addAll(Arrays.asList(arr));
}
}
@@ -0,0 +1,13 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Main {
public void test(Integer[] arr) {
List<Integer> result = new ArrayList<>();
result.add(1);
result.addAll(Arrays.asList(arr));
}
}
@@ -1,4 +1,4 @@
// "Replace with addAll" "true"
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.*;
public class Collect {
@@ -0,0 +1,15 @@
// "Replace iteration with bulk 'CrudRepository.save' call" "true"
package org.springframework.data.repository;
import java.io.Serializable;
interface CrudRepository<T,ID extends Serializable> {
<S extends T> Iterable<S> save(Iterable<S> entities);
<S extends T> S save(S entity);
}
public class Main {
public void test(CrudRepository<CharSequence, Integer> repo, Iterable<String> stringsToSave) {
repo.save(stringsToSave);
}
}
@@ -0,0 +1,16 @@
// "Replace iteration with bulk 'CrudRepository.save' call" "true"
package org.springframework.data.repository;
import java.io.Serializable;
import java.util.List;
interface CrudRepository<T,ID extends Serializable> {
<S extends T> Iterable<S> save(Iterable<S> entities);
<S extends T> S save(S entity);
}
public class Main {
public void test(CrudRepository<Iterable<String>, Integer> repo, Iterable<List<String>> stringsToSave) {
repo.save(stringsToSave);
}
}
@@ -0,0 +1,10 @@
// "Replace iteration with bulk 'List.removeAll' call" "true"
import java.util.*;
public class Collect {
static class Person {}
void collectNames(List<Person> persons, Collection<Person> toRemove){
persons.removeAll(toRemove);
}
}
@@ -0,0 +1,10 @@
// "Replace iteration with bulk 'List.removeAll' call" "true"
import java.util.*;
public class Collect {
static class Person {}
void collectNames(List<Person> persons, Collection<Person> toRemove){
persons.removeAll(toRemove);
}
}
@@ -0,0 +1,10 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.*;
public class Main {
public void test(Integer[] arr) {
List<Integer> result = new ArrayList<>();
result.add(1);
Arrays.stream(arr).forEach(result<caret>::add);
}
}
@@ -0,0 +1,14 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.*;
public class Collect {
class Person {}
void collectNames(List<Person> persons){
List<Person> names = new ArrayList<>();
for(int i = 0; i<persons.size(); i = i + 1) {
Person p = persons.get(i);
names.<caret>add(p);
}
}
}
@@ -1,4 +1,4 @@
// "Replace with addAll" "false"
// "Replace iteration with bulk 'Collection.addAll' call" "false"
import java.util.*;
@@ -16,8 +16,8 @@ public class Main {
@Override
public boolean addAll(Collection<? extends String> c) {
for (String e : <caret>c) {
add(e);
for (String e : c) {
a<caret>dd(e);
}
return true;
}
@@ -1,4 +1,4 @@
// "Replace with addAll" "true"
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.*;
@@ -15,8 +15,8 @@ public class Main {
}
public boolean myAdd(Collection<? extends String> c) {
for (String e : <caret>c) {
this.add(e);
for (String e : c) {
this.<caret>add(e);
}
return true;
}
@@ -0,0 +1,13 @@
// "Replace iteration with bulk 'Collection.addAll' call" "false"
import java.util.*;
public class Collect {
class Person {}
void collectNames(Iterable<Person> persons){
List<Person> names = new ArrayList<>();
persons.forEach(p -> {
names<caret>.add(p);
});
}
}
@@ -0,0 +1,13 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.*;
public class Collect {
class Person {}
void collectNames(List<Person> persons){
List<Person> names = new ArrayList<>();
persons.forEach(p -> {
names<caret>.add(p);
});
}
}
@@ -1,4 +1,4 @@
// "Replace with addAll" "true"
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.ArrayList;
import java.util.List;
@@ -6,8 +6,8 @@ class Sample {
List<String> foo = new ArrayList<>();
String foo(){
Sample sm = new Sample();
for (String s : fo<caret>o) {
sm.foo.add(s);
for (String s : foo) {
<caret>sm.foo.add(s);
}
return null;
}
@@ -1,4 +1,4 @@
// "Replace with addAll" "true"
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.ArrayList;
import java.util.List;
@@ -6,7 +6,7 @@ public class Main {
public void test(Integer[] arr) {
List<Integer> result = new ArrayList<>();
result.add(1);
for(Integer i : ar<caret>r)
result.add(i);
for(Integer i : arr)
result.<caret>add(i);
}
}
@@ -0,0 +1,13 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.ArrayList;
import java.util.List;
public class Main {
public void test(Integer[] arr) {
List<Integer> result = new ArrayList<>();
result.add(1);
for (int idx = 0; idx < arr.length; idx++) {
result.<caret>add(arr[idx]);
}
}
}
@@ -0,0 +1,12 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class Main {
public void test(Integer[] arr) {
List<Integer> result = new ArrayList<>();
result.add(1);
Stream.of(arr).forEachOrdered(<caret>result::add);
}
}
@@ -0,0 +1,13 @@
// "Replace iteration with bulk 'Collection.addAll' call" "true"
import java.util.*;
public class Collect {
class Person {}
void collectNames(List<Person> persons){
List<Person> names = new ArrayList<>();
for (Person person : persons) {
<caret>names.add(person);
}
}
}
@@ -0,0 +1,15 @@
// "Replace iteration with bulk 'CrudRepository.save' call" "true"
package org.springframework.data.repository;
import java.io.Serializable;
interface CrudRepository<T,ID extends Serializable> {
<S extends T> Iterable<S> save(Iterable<S> entities);
<S extends T> S save(S entity);
}
public class Main {
public void test(CrudRepository<CharSequence, Integer> repo, Iterable<String> stringsToSave) {
stringsToSave.forEach(<caret>repo::save);
}
}
@@ -0,0 +1,16 @@
// "Replace iteration with bulk 'CrudRepository.save' call" "false"
package org.springframework.data.repository;
import java.io.Serializable;
import java.util.List;
interface CrudRepository<T,ID extends Serializable> {
<S extends T> Iterable<S> save(Iterable<S> entities);
<S extends T> S save(S entity);
}
public class Main {
public void test(CrudRepository<CharSequence, Integer> repo, Iterable<List<String>> stringsToSave) {
stringsToSave.forEach(<caret>repo::save);
}
}
@@ -0,0 +1,16 @@
// "Replace iteration with bulk 'CrudRepository.save' call" "true"
package org.springframework.data.repository;
import java.io.Serializable;
import java.util.List;
interface CrudRepository<T,ID extends Serializable> {
<S extends T> Iterable<S> save(Iterable<S> entities);
<S extends T> S save(S entity);
}
public class Main {
public void test(CrudRepository<Iterable<String>, Integer> repo, Iterable<List<String>> stringsToSave) {
stringsToSave.forEach(<caret>repo::save);
}
}
@@ -0,0 +1,11 @@
// "Replace iteration with bulk 'List.removeAll' call" "true"
import java.util.*;
public class Collect {
static class Person {}
void collectNames(List<Person> persons, Collection<Person> toRemove){
for (Iterator<Person> it = toRemove.iterator(); it.hasNext(); )
persons<caret>.remove(it.next());
}
}
@@ -0,0 +1,14 @@
// "Replace iteration with bulk 'List.removeAll' call" "true"
import java.util.*;
public class Collect {
static class Person {}
void collectNames(List<Person> persons, Collection<Person> toRemove){
Iterator<Person> it = toRemove.iterator();
while (it.hasNext()) {
Person person = it.next();
persons<caret>.remove(person);
}
}
}