Files
openide/java/java-tests/testData/inspection/java8MapForEach/beforeForEach.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

125 lines
3.9 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.entrySet().for<caret>Each(entry ->
System.out.println(entry.getKey() +":"+entry.getValue())
);
}
public static void testKey(Map<String, Integer> map) {
map.entrySet().forEach(entry -> {
String str = entry.getKey();
System.out.println(str +":"+entry.getValue());
});
}
public static void testValue(Map<String, Integer> map) {
map.entrySet().forEach(entry -> {
String str = entry.getKey();
Integer num = entry.getValue();
System.out.println(str +":"+ num);
});
}
public static void testTwoVarsWildcard(Map<? extends String, Integer> map) {
map.entrySet().forEach(entry -> {
String str = entry.getKey();
Integer num = entry.getValue();
System.out.println(str +":"+ num);
String str2 = entry.getKey();
System.out.println(str2);
});
}
public static <T extends Map<?, ?>> void testGeneric(Supplier<T> map) {
map.get().entrySet().forEach(e -> System.out.println(e.getKey()+":"+e.getValue()));
}
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) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
public static void testForLoop2(Map<String, Integer> map) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String str = entry.getKey();
Integer num = entry.getValue();
System.out.println(str + ":" + num);
}
}
public static void testForLoop3(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());
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) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String str = entry.getKey();
int num = entry.getValue();
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 {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String str = entry.getKey();
Integer num = entry.getValue();
if(num > 0) throw new RuntimeException();
System.out.println(str + ":" + num);
}
}
void forEach(Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
//long comment
}
}
void expressionStatement(Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
String value = entry.getValue();
entry.getValue();
}
}
}