[java-inspections] IDEA-284183 "Replace 'forEach' call with loop" generates red code in case of wildcards with Map

GitOrigin-RevId: 77f0fc7d530aed988653aa726be1244c9a3941f9
This commit is contained in:
Tagir Valeev
2021-12-20 12:11:41 +07:00
committed by intellij-monorepo-bot
parent 27184c1411
commit f2e0304c8d
3 changed files with 18 additions and 2 deletions

View File

@@ -173,13 +173,13 @@ public class StreamToLoopInspection extends AbstractBaseJavaLocalInspectionTool
PsiType keyType = PsiUtil.substituteTypeParameter(type, CommonClassNames.JAVA_UTIL_MAP, 0, false);
PsiType valueType = PsiUtil.substituteTypeParameter(type, CommonClassNames.JAVA_UTIL_MAP, 1, false);
if (!isValidElementType(keyType, terminalCall, true) || !isValidElementType(valueType, terminalCall, true)) return null;
keyType = GenericsUtil.getVariableTypeByExpressionType(keyType);
valueType = GenericsUtil.getVariableTypeByExpressionType(valueType);
Project project = terminalCall.getProject();
JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
PsiClass entryClass = facade.findClass(CommonClassNames.JAVA_UTIL_MAP_ENTRY, terminalCall.getResolveScope());
if (entryClass == null || entryClass.getTypeParameters().length != 2) return null;
PsiType entryType = JavaPsiFacade.getElementFactory(project).createType(entryClass, keyType, valueType);
keyType = GenericsUtil.getVariableTypeByExpressionType(keyType);
valueType = GenericsUtil.getVariableTypeByExpressionType(valueType);
TerminalOperation terminal = new TerminalOperation.MapForEachTerminalOperation(fn, keyType, valueType);
SourceOperation source = new SourceOperation.ForEachSource(qualifier, true);
OperationRecord terminalRecord = new OperationRecord();

View File

@@ -42,4 +42,13 @@ public class Main {
}
}
}
void convert(Map<Integer, ? extends List<? extends Appendable>> map) {
for (Map.Entry<Integer, ? extends List<? extends Appendable>> entry : map.entrySet()) {
Integer integer = entry.getKey();
List<? extends Appendable> appendables = entry.getValue();
System.out.println(integer);
System.out.println(appendables);
}
}
}

View File

@@ -28,4 +28,11 @@ public class Main {
}
}
}
void convert(Map<Integer, ? extends List<? extends Appendable>> map) {
map.forEach((integer, appendables) -> {
System.out.println(integer);
System.out.println(appendables);
});
}
}