Files
openide/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamToLoop/afterFieldInitializer.java
Tagir Valeev 3fb0b781f6 IDEA-382020 [java-inspections]: 'Split declaration and initializer' produces exception when injection is used
Analysis: `CodeStyleManager::reformat` inside ModCommand triggers reformatting of the injected fragment as well (IJPL-35687), but due to implementation deficiency, the injected fragment always has the event system enabled, even for a non-physical host file. Fixing this in the platform looks hard, but we can avoid reformatting the injection inside the intention.
Implementation-wise, I used the CodeBlockSurrounder which does the similar thing but does not have the reformatting problem. I also improved CodeBlockSurrounder to support corner cases and added more tests.

GitOrigin-RevId: c7467aa5771489c95698dc01b487059d87d655c3
2025-11-14 14:41:00 +00:00

72 lines
1.7 KiB
Java

// "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;
final String field;
static {
List<String> list = new ArrayList<>();
for (String s : Arrays.asList("foo", "bar", "baz")) {
String upperCase = s.toUpperCase();
list.add(upperCase);
}
STR = list;
System.out.println("static initializer already exists");
}
final long count;
{
StringJoiner joiner = new StringJoiner(",");
for (String s : STR) {
if (!s.isEmpty()) {
joiner.add(s);
}
}
field = joiner.toString();
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;
final long count2;
final long count3;
final long y = 0;
final long[] countArray;
{
long result1 = 0L;
for (Integer i1 : Arrays.asList(1, 2, 3, 4)) {
if (i1 % 2 == 0) {
result1++;
}
}
count2 = result1;
long count1 = 0L;
for (Integer integer : Arrays.asList(1, 2, 3, 4)) {
if (integer % 2 == 0) {
count1++;
}
}
count3 = count1;
long result = 0L;
for (Integer i : Arrays.asList(1, 2, 3, 4)) {
if (i % 2 == 0) {
result++;
}
}
countArray = new long[]{result};
}
}