IDEA-161861 Stream API migration should handle cases where result variable is not declared just before the for loop

This commit is contained in:
Tagir Valeev
2016-09-29 15:54:08 +07:00
parent b962a9f417
commit b9a06d65b9
57 changed files with 681 additions and 68 deletions
@@ -4,6 +4,6 @@ import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
String found = data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty()) ? "yes" : "no";
String found = data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty()) ? "yes" : "no";
}
}
@@ -4,6 +4,6 @@ import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found = data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty());
boolean found = data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty());
}
}
@@ -4,6 +4,6 @@ import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found = !data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty());
boolean found = !data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty());
}
}
@@ -0,0 +1,14 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found;
if (data.size() > 10) {
System.out.println("Big data");
}
found = data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty());
System.out.println(found);
}
}
@@ -4,13 +4,12 @@ import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found = false;
boolean found;
if(Math.random() > 0.5) {
found = true;
} else {
if (data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty())) {
found = true;
}
found = data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty());
}
System.out.println(found);
}
}
@@ -0,0 +1,15 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found = false;
if(Math.random() > 0.5) {
System.out.println("oops");
} else {
found = data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty());
}
System.out.println(found);
}
}
@@ -0,0 +1,16 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found;
try {
found = data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty());
System.out.println(found);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
@@ -0,0 +1,16 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found = false;
try {
found = data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty());
}
catch(Exception ex) {
ex.printStackTrace();
}
System.out.println(found);
}
}
@@ -6,6 +6,6 @@ import java.util.stream.Collectors;
public class Main {
public void test(Integer[] arr) {
List<Integer> result = Arrays.stream(arr).filter(x -> x > 5).collect(Collectors.toList());
List<Integer> result = Arrays.stream(arr).filter(x -> x > 5).collect(Collectors.toList());
}
}
@@ -4,7 +4,7 @@ import java.util.stream.Collectors;
class A {
public static void main(List<String> args) {
ArrayList<String> uniqNames = args.stream().map(name -> name.substring(1)).collect(Collectors.toCollection(ArrayList::new));
ArrayList<String> uniqNames = args.stream().map(name -> name.substring(1)).collect(Collectors.toCollection(ArrayList::new));
uniqNames.forEach(System.out::println);
}
@@ -5,7 +5,7 @@ import java.util.stream.Collectors;
class Test {
public static <T> List<TokenFilter<T>> fromString(final T src, Function<T, List<String>> extractor) {
final List<TokenFilter<T>> result = extractor.apply(src).stream().map((Function<String, TokenFilter<T>>) TokenFilter::new).collect(Collectors.toList());
final List<TokenFilter<T>> result = extractor.apply(src).stream().map((Function<String, TokenFilter<T>>) TokenFilter::new).collect(Collectors.toList());
return result;
}
@@ -10,6 +10,6 @@ public class Collect {
}
void collectNames(List<Person> persons){
List<String> names = persons.stream().map(Person::getName).collect(Collectors.toList());
List<String> names = persons.stream().map(Person::getName).collect(Collectors.toList());
}
}
@@ -10,6 +10,6 @@ public class Collect {
}
void collectNames(List<Person> persons){
List<String> names = persons.stream().filter(Objects::nonNull).map(Person::getName).collect(Collectors.toList());
List<String> names = persons.stream().filter(Objects::nonNull).map(Person::getName).collect(Collectors.toList());
}
}
@@ -10,6 +10,6 @@ public class Collect {
}
void collectNames(List<Person> persons){
Collection<String> names = persons.stream().map(Person::getName).collect(Collectors.toList());
Collection<String> names = persons.stream().map(Person::getName).collect(Collectors.toList());
}
}
@@ -10,7 +10,7 @@ public class Collect {
}
void collectNames(List<Person> persons){
List<String> names = persons.stream().map(Person::getName).collect(Collectors.toList());
List<String> names = persons.stream().map(Person::getName).collect(Collectors.toList());
//some comment
}
}
@@ -10,6 +10,6 @@ public class Collect {
}
void collectNames(List<Person> persons){
List<String> names = persons.stream().map(person -> "name: " + person.getName()).collect(Collectors.toList());
List<String> names = persons.stream().map(person -> "name: " + person.getName()).collect(Collectors.toList());
}
}
@@ -6,6 +6,6 @@ import java.util.stream.Collectors;
public class Main {
public void testPrimitiveMap(List<String> data) {
List<Integer> list = data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).boxed().collect(Collectors.toList());
List<Integer> list = data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).boxed().collect(Collectors.toList());
}
}
@@ -10,6 +10,6 @@ public class Collect {
}
void collectNames(List<Person> persons){
Set<String> names = persons.stream().map(Person::getName).collect(Collectors.toSet());
Set<String> names = persons.stream().map(Person::getName).collect(Collectors.toSet());
}
}
@@ -0,0 +1,17 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names, other = new HashSet<>();
names = persons.stream().map(Person::getName).collect(Collectors.toSet());
System.out.println(names);
}
}
@@ -0,0 +1,18 @@
// "Replace with forEach" "true"
import java.util.*;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = new HashSet<>();
for(int i=0; i<10; i++) {
persons.stream().map(Person::getName).forEach(names::add);
}
System.out.println(names);
}
}
@@ -0,0 +1,17 @@
// "Replace with forEach" "true"
import java.util.*;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = new HashSet<>();
names.add("Test");
persons.stream().map(Person::getName).forEach(names::add);
System.out.println(names);
}
}
@@ -0,0 +1,19 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = new HashSet<>(), other = new HashSet<>();
if(persons != null) {
names = persons.stream().map(Person::getName).collect(Collectors.toSet());
}
System.out.println(names);
}
}
@@ -10,6 +10,6 @@ public class Collect {
}
void collectNames(List<Person> persons){
Set<String> names = persons.stream().map(Person::getName).collect(Collectors.toCollection(LinkedHashSet::new));
Set<String> names = persons.stream().map(Person::getName).collect(Collectors.toCollection(LinkedHashSet::new));
}
}
@@ -6,6 +6,6 @@ import java.util.stream.Collectors;
public class Main {
public void testPrimitiveMap(List<String> data) {
List<String> list = data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).mapToObj(String::valueOf).collect(Collectors.toList());
List<String> list = data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).mapToObj(String::valueOf).collect(Collectors.toList());
}
}
@@ -6,7 +6,7 @@ import java.util.stream.Collectors;
public class Main {
public List<String> test(Map<String, List<String>> map) {
List<String> result = map.entrySet().stream().filter(entry -> !entry.getKey().isEmpty()).map(Map.Entry::getValue).filter(Objects::nonNull).flatMap(Collection::stream).map(String::trim).filter(trimmed -> !trimmed.isEmpty()).collect(Collectors.toList());
List<String> result = map.entrySet().stream().filter(entry -> !entry.getKey().isEmpty()).map(Map.Entry::getValue).filter(Objects::nonNull).flatMap(Collection::stream).map(String::trim).filter(trimmed -> !trimmed.isEmpty()).collect(Collectors.toList());
return result;
}
}
@@ -6,7 +6,7 @@ import java.util.stream.Collectors;
public class Main {
public List<String> test(Map<String, List<String>> map) {
List<String> result = map.entrySet().stream().filter(entry -> !entry.getKey().isEmpty()).map(Map.Entry::getValue).filter(Objects::nonNull).flatMap(Collection::stream).map(String::trim).filter(trimmed -> !trimmed.isEmpty()).collect(Collectors.toList());
List<String> result = map.entrySet().stream().filter(entry -> !entry.getKey().isEmpty()).map(Map.Entry::getValue).filter(Objects::nonNull).flatMap(Collection::stream).map(String::trim).filter(trimmed -> !trimmed.isEmpty()).collect(Collectors.toList());
return result;
}
}
@@ -4,6 +4,6 @@ import java.util.stream.Collectors;
public class Main {
public void test(List<Set<String>> nested) {
List<String> result = nested.stream().filter(Objects::nonNull).flatMap(Collection::stream).filter(str -> str.startsWith("xyz")).map(String::trim).collect(Collectors.toList());
List<String> result = nested.stream().filter(Objects::nonNull).flatMap(Collection::stream).filter(str -> str.startsWith("xyz")).map(String::trim).collect(Collectors.toList());
}
}
@@ -6,6 +6,6 @@ import java.util.stream.Collectors;
public class Main {
public void test(List<String[]> list) {
List<String> result = list.stream().filter(arr -> arr.length > 2).flatMap(Arrays::stream).map(String::trim).filter(trimmed -> !trimmed.isEmpty()).collect(Collectors.toList());
List<String> result = list.stream().filter(arr -> arr.length > 2).flatMap(Arrays::stream).map(String::trim).filter(trimmed -> !trimmed.isEmpty()).collect(Collectors.toList());
}
}
@@ -7,6 +7,6 @@ import java.util.stream.Collectors;
public class Main {
public void test(Map<String, String[]> map) {
List<String> result = map.entrySet().stream().filter(entry -> entry.getKey().startsWith("x")).map(Map.Entry::getValue).flatMap(Arrays::stream).map(String::trim).collect(Collectors.toList());
List<String> result = map.entrySet().stream().filter(entry -> entry.getKey().startsWith("x")).map(Map.Entry::getValue).flatMap(Arrays::stream).map(String::trim).collect(Collectors.toList());
}
}
@@ -6,7 +6,7 @@ import java.util.Objects;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = map.values().stream().filter(Objects::nonNull).findFirst().map(List::size).orElse(0);
int firstSize = map.values().stream().filter(Objects::nonNull).findFirst().map(List::size).orElse(0);
// comment
System.out.println(firstSize);
}
@@ -4,7 +4,7 @@ import java.util.List;
public class Main {
public void testCast(List<Object> data) {
String found = (String) data.stream().filter(obj -> obj instanceof String).findFirst().orElse(null);
String found = (String) data.stream().filter(obj -> obj instanceof String).findFirst().orElse(null);
System.out.println(found);
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize;
int other = map.size();
if(other > 10) {
System.out.println("Big");
}
// comment
firstSize = map.values().stream().filter(Objects::nonNull).findFirst().map(List::size).orElse(0);
System.out.println(firstSize);
}
}
@@ -0,0 +1,14 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = 0, other = firstSize;
// comment
firstSize = map.values().stream().filter(Objects::nonNull).findFirst().map(List::size).orElse(firstSize);
System.out.println(firstSize);
}
}
@@ -7,7 +7,7 @@ import java.util.Objects;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
String firstStr = map.values().stream().filter(Objects::nonNull).flatMap(Collection::stream).filter(str -> !str.isEmpty()).findFirst().orElse("");
String firstStr = map.values().stream().filter(Objects::nonNull).flatMap(Collection::stream).filter(str -> !str.isEmpty()).findFirst().orElse("");
System.out.println(firstStr);
}
}
@@ -7,7 +7,7 @@ import java.util.stream.Collectors;
public class Main {
public List<String> test(String[][] arr) {
List<String> result = Arrays.stream(arr).filter(Objects::nonNull).flatMap(Arrays::stream).collect(Collectors.toList());
List<String> result = Arrays.stream(arr).filter(Objects::nonNull).flatMap(Arrays::stream).collect(Collectors.toList());
return result;
}
}
@@ -7,7 +7,7 @@ import java.util.stream.Collectors;
public class Main {
public List<Integer> test(int[][] arr) {
List<Integer> result = Arrays.stream(arr).filter(Objects::nonNull).flatMapToInt(Arrays::stream).boxed().collect(Collectors.toList());
List<Integer> result = Arrays.stream(arr).filter(Objects::nonNull).flatMapToInt(Arrays::stream).boxed().collect(Collectors.toList());
return result;
}
}
@@ -0,0 +1,15 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public void testPrimitiveMap(List<String> data) {
int sum;
if(Math.random() > 0.5) {
sum = 10;
} else {
sum = data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).map(len -> len * 2).sum();
}
System.out.println(sum);
}
}
@@ -0,0 +1,15 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public void testPrimitiveMap(List<String> data) {
int sum = 0;
if(Math.random() > 0.5) {
System.out.println("oops");
} else {
sum = data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).map(len -> len * 2).sum();
}
System.out.println(sum);
}
}
@@ -0,0 +1,20 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found = false;
if (data.size() > 10) {
System.out.println("Big data");
}
for(String str : da<caret>ta) {
String trimmed = str.trim();
if(!trimmed.isEmpty()) {
found = true;
break;
}
}
System.out.println(found);
}
}
@@ -16,5 +16,6 @@ public class Main {
}
}
}
System.out.println(found);
}
}
@@ -0,0 +1,21 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found = false;
if(Math.random() > 0.5) {
System.out.println("oops");
} else {
for (String str : da<caret>ta) {
String trimmed = str.trim();
if (!trimmed.isEmpty()) {
found = true;
break;
}
}
}
System.out.println(found);
}
}
@@ -0,0 +1,22 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found = false;
try {
for (String str : da<caret>ta) {
String trimmed = str.trim();
if (!trimmed.isEmpty()) {
found = true;
break;
}
}
System.out.println(found);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
@@ -0,0 +1,22 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
boolean found = false;
try {
for (String str : da<caret>ta) {
String trimmed = str.trim();
if (!trimmed.isEmpty()) {
found = true;
break;
}
}
}
catch(Exception ex) {
ex.printStackTrace();
}
System.out.println(found);
}
}
@@ -0,0 +1,18 @@
// "Replace with collect" "true"
import java.util.*;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = new HashSet<>(), other = new HashSet<>();
for (Person person : pers<caret>ons) {
names.add(person.getName());
}
System.out.println(names);
}
}
@@ -0,0 +1,20 @@
// "Replace with forEach" "true"
import java.util.*;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = new HashSet<>();
for(int i=0; i<10; i++) {
for (Person person : pers<caret>ons) {
names.add(person.getName());
}
}
System.out.println(names);
}
}
@@ -0,0 +1,19 @@
// "Replace with forEach" "true"
import java.util.*;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = new HashSet<>();
names.add("Test");
for (Person person : pers<caret>ons) {
names.add(person.getName());
}
System.out.println(names);
}
}
@@ -0,0 +1,20 @@
// "Replace with collect" "true"
import java.util.*;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = new HashSet<>(), other = new HashSet<>();
if(persons != null) {
for (Person person : pers<caret>ons) {
names.add(person.getName());
}
}
System.out.println(names);
}
}
@@ -0,0 +1,22 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = 0;
int other = map.size();
if(other > 10) {
System.out.println("Big");
}
for(List<String> list : map.valu<caret>es()) {
if(list != null) {
firstSize = list.size();
// comment
break;
}
}
System.out.println(firstSize);
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = 0, other = firstSize;
for(List<String> list : map.valu<caret>es()) {
if(list != null) {
firstSize = list.size();
// comment
break;
}
}
System.out.println(firstSize);
}
}
@@ -0,0 +1,22 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public void testPrimitiveMap(List<String> data) {
int sum = 0;
if(Math.random() > 0.5) {
sum = 10;
} else {
for (String str : dat<caret>a) {
if (str.startsWith("xyz")) {
int len = str.length();
if (len > 10) {
sum += len * 2;
}
}
}
}
System.out.println(sum);
}
}
@@ -0,0 +1,22 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public void testPrimitiveMap(List<String> data) {
int sum = 0;
if(Math.random() > 0.5) {
System.out.println("oops");
} else {
for (String str : dat<caret>a) {
if (str.startsWith("xyz")) {
int len = str.length();
if (len > 10) {
sum += len * 2;
}
}
}
}
System.out.println(sum);
}
}