RefactoringUtil#ensureCodeBlock enhanced and used in Surround with try-catch

Fixes IDEA-178781 "Surround with try-catch" QuickFix for "Unhandled exception" in a field initializer
Enables stream-to-loop in field initializer
Fixes stream-to-loop in for initializer
Disables stream-to-loop in for update
This commit is contained in:
Tagir Valeev
2017-09-12 13:34:43 +07:00
parent 94a5fea51d
commit 6e87cb9bfd
10 changed files with 315 additions and 37 deletions
@@ -0,0 +1,62 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.*;
import java.util.stream.*;
public class Main {
static final List<String> STR;
static {
List<String> list = new ArrayList<>();
for (String s : Arrays.asList("foo", "bar", "baz")) {
String toUpperCase = s.toUpperCase();
list.add(toUpperCase);
}
STR = list;
}
final String field;
{
StringJoiner joiner = new StringJoiner(",");
for (String s : STR) {
if (!s.isEmpty()) {
joiner.add(s);
}
}
field = joiner.toString();
}
static {
System.out.println("static initializer already exists");
}
final long count;
{
long result = 0L;
for (Integer i : Arrays.asList(1, 2, 3, 4)) {
if (i % 2 == 0) {
result++;
}
}
count = result;
System.out.println("initializer already exists");
}
final long x = 0, count2 = Stream.of(1,2,3,4).filter(i -> i % 2 == 0).count();
final long count3 = Stream.of(1,2,3,4).filter(i -> i % 2 == 0).count(), y = 0;
final long[] countArray;
{
long result = 0L;
for (Integer i : Arrays.asList(1, 2, 3, 4)) {
if (i % 2 == 0) {
result++;
}
}
countArray = new long[]{result};
}
}
@@ -0,0 +1,50 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.*;
import java.util.stream.*;
public class Main {
String j = "foo";
public void test(List<String> list) {
long i = 0L;
for (String s : list) {
if (s.isEmpty()) {
i++;
}
}
for(;
i<10;
i+=list.stream().filter(String::isEmpty).count()) {
System.out.println(i);
}
{
long j = 0L;
for (String s : list) {
if (s.isEmpty()) {
j++;
}
}
for(;
j<10;
j+=list.stream().filter(String::isEmpty).count()) {
System.out.println(j);
}
}
System.out.println(j);
StringJoiner joiner = new StringJoiner(",");
for (String s1 : list) {
joiner.add(s1);
}
for(String s = joiner.toString(); !s.isEmpty(); s = s.substring(1)) {
System.out.println(s);
}
}
public static void main(String[] args) {
new Main().test(Arrays.asList("", "", "foo"));
}
}
@@ -0,0 +1,26 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.*;
import java.util.stream.*;
public class Main {
static final List<String> STR = Stream.of("foo","bar","baz").ma<caret>p(String::toUpperCase).collect(Collectors.toList());
final String field = STR.stream().filter(x -> !x.isEmpty()).collect(Collectors.joining(","));
static {
System.out.println("static initializer already exists");
}
final long count = Stream.of(1,2,3,4).filter(i -> i % 2 == 0).count();
{
System.out.println("initializer already exists");
}
final long x = 0, count2 = Stream.of(1,2,3,4).filter(i -> i % 2 == 0).count();
final long count3 = Stream.of(1,2,3,4).filter(i -> i % 2 == 0).count(), y = 0;
final long[] countArray = {Stream.of(1,2,3,4).filter(i -> i % 2 == 0).count()};
}
@@ -0,0 +1,32 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.*;
import java.util.stream.*;
public class Main {
String j = "foo";
public void test(List<String> list) {
for(long i = list.stream().filter(String::isEmpty).cou<caret>nt();
i<10;
i+=list.stream().filter(String::isEmpty).count()) {
System.out.println(i);
}
for(long j = list.stream().filter(String::isEmpty).count();
j<10;
j+=list.stream().filter(String::isEmpty).count()) {
System.out.println(j);
}
System.out.println(j);
for(String s = list.stream().collect(Collectors.joining(",")); !s.isEmpty(); s = s.substring(1)) {
System.out.println(s);
}
}
public static void main(String[] args) {
new Main().test(Arrays.asList("", "", "foo"));
}
}
@@ -0,0 +1,19 @@
// "Surround with try/catch" "true"
import java.io.IOException;
class C {
static final String S;
static {
try {
S = getString();
} catch (IOException e) {
e.printStackTrace();
}
}
static String getString() throws IOException {
if(Math.random() > 0.5) throw new IOException();
return "foo";
}
}
@@ -0,0 +1,11 @@
// "Surround with try/catch" "true"
import java.io.IOException;
class C {
static final String S = getSt<caret>ring();
static String getString() throws IOException {
if(Math.random() > 0.5) throw new IOException();
return "foo";
}
}