IDEA-166424 Guava create collections , nonsupport replace with stream API

This commit is contained in:
Tagir Valeev
2017-01-12 11:55:44 +07:00
parent 347bd66e81
commit 5a599cdffd
8 changed files with 162 additions and 0 deletions

View File

@@ -161,6 +161,25 @@ class CollectMigration extends BaseStreamApiMigration {
PsiExpressionList argumentList = ((PsiNewExpression)expression).getArgumentList();
return argumentList != null && argumentList.getExpressions().length == 0;
}
if (expression instanceof PsiMethodCallExpression) {
PsiMethodCallExpression call = (PsiMethodCallExpression)expression;
String name = call.getMethodExpression().getReferenceName();
PsiExpressionList argumentList = call.getArgumentList();
if(name != null && name.startsWith("new") && argumentList.getExpressions().length == 0) {
PsiMethod method = call.resolveMethod();
if(method != null && method.getParameterList().getParametersCount() == 0) {
PsiClass aClass = method.getContainingClass();
if(aClass != null) {
String qualifiedName = aClass.getQualifiedName();
if("com.google.common.collect.Maps".equals(qualifiedName) ||
"com.google.common.collect.Lists".equals(qualifiedName) ||
"com.google.common.collect.Sets".equals(qualifiedName)) {
return true;
}
}
}
}
}
return false;
}

View File

@@ -0,0 +1,19 @@
// "Replace with collect" "true"
package com.google.common.collect;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class Lists {
public static <E> ArrayList<E> newArrayList() {
return new ArrayList<E>();
}
}
public class Test {
public void test() {
List<String> list = IntStream.range(0, 10).filter(i -> i % 2 == 0).mapToObj(String::valueOf).collect(Collectors.toList());
System.out.println(list);
}
}

View File

@@ -0,0 +1,19 @@
// "Replace with collect" "true"
package com.google.common.collect;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class Maps {
public static <K, V> HashMap<K, V> newHashMap() {
return new HashMap<K, V>();
}
}
public class Test {
public void test() {
Map<String, Integer> map = IntStream.range(0, 10).filter(i -> i % 2 == 0).collect(Collectors.toMap(String::valueOf, i -> i, (a, b) -> b));
System.out.println(map);
}
}

View File

@@ -0,0 +1,17 @@
// "Replace with toArray" "true"
package com.google.common.collect;
import java.util.*;
import java.util.stream.Stream;
class Sets {
public static <E extends Comparable> TreeSet<E> newTreeSet() {
return new TreeSet<E>();
}
}
public class Test {
public String[] test(List<String> input) {
return input.stream().filter(Objects::nonNull).flatMap(s -> Stream.of(s, s + s)).distinct().sorted().toArray(String[]::new);
}
}

View File

@@ -0,0 +1,22 @@
// "Replace with collect" "true"
package com.google.common.collect;
import java.util.*;
class Lists {
public static <E> ArrayList<E> newArrayList() {
return new ArrayList<E>();
}
}
public class Test {
public void test() {
List<String> list = Lists.newArrayList();
for(int <caret>i=0; i<10; i++) {
if(i%2 == 0) {
list.add(String.valueOf(i));
}
}
System.out.println(list);
}
}

View File

@@ -0,0 +1,22 @@
// "Replace with collect" "true"
package com.google.common.collect;
import java.util.*;
class Maps {
public static <K, V> HashMap<K, V> newHashMap() {
return new HashMap<K, V>();
}
}
public class Test {
public void test() {
Map<String, Integer> map = Maps.newHashMap();
for(int <caret>i=0; i<10; i++) {
if(i%2 == 0) {
map.put(String.valueOf(i), i);
}
}
System.out.println(map);
}
}

View File

@@ -0,0 +1,22 @@
// "Replace with collect" "false"
package com.google.common.collect;
import java.util.*;
class Lists {
public static <E> ArrayList<E> newArrayList() {
return new ArrayList<E>();
}
}
public class Test {
public void test(List<String> input) {
List<String> list = Lists.newArrayList(input);
for(int <caret>i=0; i<10; i++) {
if(i%2 == 0) {
list.add(String.valueOf(i));
}
}
System.out.println(list);
}
}

View File

@@ -0,0 +1,22 @@
// "Replace with toArray" "true"
package com.google.common.collect;
import java.util.*;
class Sets {
public static <E extends Comparable> TreeSet<E> newTreeSet() {
return new TreeSet<E>();
}
}
public class Test {
public String[] test(List<String> input) {
Set<String> set = Sets.newTreeSet();
for(String s : inp<caret>ut) {
if(s != null) {
Collections.addAll(set, s, s+s);
}
}
return set.toArray(new String[0]);
}
}