Files
openide/java/java-tests/testData/inspection/java8MapForEach/afterForEach.java
Tagir Valeev da261a7705 [java-inspections] Java8MapForEachInspection: disable when entry getter is used in void context
Also avoid replacing expression with identifier, as this creates illegal PSI
Fixes EA-537547 - IOE: PsiExpressionStatementImpl.replaceChildInternal

GitOrigin-RevId: 7fd9c0f98165a2f5b6ad74154fa1b5b701610ee7
2022-06-27 13:22:11 +00:00

103 lines
3.1 KiB
Java

// "Fix all 'Map.forEach() can be used' problems in file" "true"
import java.util.Map;
import java.util.function.Supplier;
public class Test {
public static void testInline(Map<String, Integer> map) {
map.forEach((key, value) -> System.out.println(key + ":" + value));
}
public static void testKey(Map<String, Integer> map) {
map.forEach((str, value) -> System.out.println(str + ":" + value));
}
public static void testValue(Map<String, Integer> map) {
map.forEach((str, num) -> System.out.println(str + ":" + num));
}
public static void testTwoVarsWildcard(Map<? extends String, Integer> map) {
map.forEach((str, num) -> {
System.out.println(str + ":" + num);
String str2 = str;
System.out.println(str2);
});
}
public static <T extends Map<?, ?>> void testGeneric(Supplier<T> map) {
map.get().forEach((key, value) -> System.out.println(key + ":" + value));
}
public static <T extends Map<?, ?>> void testUsedHashCode(Supplier<T> map) {
map.get().entrySet().forEach(e -> System.out.println(e.getKey()+":"+e.getValue()+":"+e.hashCode()));
}
public static void testForLoop(Map<String, Integer> map) {
map.forEach((key, value) -> System.out.println(key + ":" + value));
}
public static void testForLoop2(Map<String, Integer> map) {
map.forEach((str, num) -> System.out.println(str + ":" + num));
}
public static void testForLoop3(Map<String, Integer> map) {
map.forEach((str, num) -> {
System.out.println(str + ":" + num);
System.out.println(num + ":" + str);
});
}
public static void testForLoopSet(Map<String, Integer> map) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String str = entry.getKey();
Integer num = entry.getValue();
System.out.println(str + ":" + entry.getValue());
entry.setValue(1);
}
}
public static void testForLoopPrimitive(Map<String, Integer> map) {
map.forEach((str, value) -> {
int num = value;
System.out.println(str + ":" + num);
});
}
public static void testForLoopSideEffect(Map<String, Integer> map) {
Integer num;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String str = entry.getKey();
num = entry.getValue();
System.out.println(str + ":" + num);
}
}
public static void testForLoopThrow(Map<String, Integer> map) throws Exception {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String str = entry.getKey();
Integer num = entry.getValue();
if(num > 0) throw new Exception();
System.out.println(str + ":" + num);
}
}
public static void testForLoopThrowRuntime(Map<String, Integer> map) throws Exception {
map.forEach((str, num) -> {
if (num > 0) throw new RuntimeException();
System.out.println(str + ":" + num);
});
}
void forEach(Map<String, String> map) {
map.forEach((key, value) -> {
//long comment
});
}
void expressionStatement(Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
String value = entry.getValue();
entry.getValue();
}
}
}