From 5a599cdffdde22733383f76699c7ede67579e7f7 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Thu, 12 Jan 2017 11:55:44 +0700 Subject: [PATCH] IDEA-166424 Guava create collections , nonsupport replace with stream API --- .../streamMigration/CollectMigration.java | 19 ++++++++++++++++ .../streamApiMigration/afterCollectGuava.java | 19 ++++++++++++++++ .../afterCollectGuavaMap.java | 19 ++++++++++++++++ .../afterCollectGuavaSet.java | 17 ++++++++++++++ .../beforeCollectGuava.java | 22 +++++++++++++++++++ .../beforeCollectGuavaMap.java | 22 +++++++++++++++++++ .../beforeCollectGuavaNonEmpty.java | 22 +++++++++++++++++++ .../beforeCollectGuavaSet.java | 22 +++++++++++++++++++ 8 files changed, 162 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuava.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuavaMap.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuavaSet.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuava.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaMap.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaNonEmpty.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaSet.java diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java index b0caeb0ba084..8b205d686dad 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java @@ -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; } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuava.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuava.java new file mode 100644 index 000000000000..ff8ebcf017b1 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuava.java @@ -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 ArrayList newArrayList() { + return new ArrayList(); + } +} + +public class Test { + public void test() { + List list = IntStream.range(0, 10).filter(i -> i % 2 == 0).mapToObj(String::valueOf).collect(Collectors.toList()); + System.out.println(list); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuavaMap.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuavaMap.java new file mode 100644 index 000000000000..17fdbf12ccc2 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuavaMap.java @@ -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 HashMap newHashMap() { + return new HashMap(); + } +} + +public class Test { + public void test() { + Map map = IntStream.range(0, 10).filter(i -> i % 2 == 0).collect(Collectors.toMap(String::valueOf, i -> i, (a, b) -> b)); + System.out.println(map); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuavaSet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuavaSet.java new file mode 100644 index 000000000000..8a31e06c82d0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectGuavaSet.java @@ -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 TreeSet newTreeSet() { + return new TreeSet(); + } +} + +public class Test { + public String[] test(List input) { + return input.stream().filter(Objects::nonNull).flatMap(s -> Stream.of(s, s + s)).distinct().sorted().toArray(String[]::new); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuava.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuava.java new file mode 100644 index 000000000000..48e0dc4ccc16 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuava.java @@ -0,0 +1,22 @@ +// "Replace with collect" "true" +package com.google.common.collect; + +import java.util.*; + +class Lists { + public static ArrayList newArrayList() { + return new ArrayList(); + } +} + +public class Test { + public void test() { + List list = Lists.newArrayList(); + for(int i=0; i<10; i++) { + if(i%2 == 0) { + list.add(String.valueOf(i)); + } + } + System.out.println(list); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaMap.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaMap.java new file mode 100644 index 000000000000..ef3b13c4e025 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaMap.java @@ -0,0 +1,22 @@ +// "Replace with collect" "true" +package com.google.common.collect; + +import java.util.*; + +class Maps { + public static HashMap newHashMap() { + return new HashMap(); + } +} + +public class Test { + public void test() { + Map map = Maps.newHashMap(); + for(int i=0; i<10; i++) { + if(i%2 == 0) { + map.put(String.valueOf(i), i); + } + } + System.out.println(map); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaNonEmpty.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaNonEmpty.java new file mode 100644 index 000000000000..02c91d5560bf --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaNonEmpty.java @@ -0,0 +1,22 @@ +// "Replace with collect" "false" +package com.google.common.collect; + +import java.util.*; + +class Lists { + public static ArrayList newArrayList() { + return new ArrayList(); + } +} + +public class Test { + public void test(List input) { + List list = Lists.newArrayList(input); + for(int i=0; i<10; i++) { + if(i%2 == 0) { + list.add(String.valueOf(i)); + } + } + System.out.println(list); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaSet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaSet.java new file mode 100644 index 000000000000..2fe5d6892b48 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectGuavaSet.java @@ -0,0 +1,22 @@ +// "Replace with toArray" "true" +package com.google.common.collect; + +import java.util.*; + +class Sets { + public static TreeSet newTreeSet() { + return new TreeSet(); + } +} + +public class Test { + public String[] test(List input) { + Set set = Sets.newTreeSet(); + for(String s : input) { + if(s != null) { + Collections.addAll(set, s, s+s); + } + } + return set.toArray(new String[0]); + } +}