IDEA-170033 Support raw-types when converting Iterable.forEach to loop

This commit is contained in:
Tagir Valeev
2017-03-22 16:51:46 +07:00
parent c4a2da99c5
commit b0c584a295
3 changed files with 14 additions and 4 deletions
@@ -168,7 +168,7 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
if(qualifier != null) {
PsiType elementType = StreamApiUtil.getStreamElementType(qualifier.getType());
if (!isValidElementType(elementType, call)) return null;
if (!isValidElementType(elementType, call, false)) return null;
Operation op = Operation.createIntermediate(name, args, outVar, elementType, supportUnknownSources);
if (op != null) return op;
op = TerminalOperation.createTerminal(name, args, elementType, callType, isVoidContext(call.getParent()));
@@ -179,8 +179,8 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
return SourceOperation.createSource(call, supportUnknownSources);
}
private static boolean isValidElementType(PsiType elementType, PsiElement context) {
if(elementType == null || ((elementType instanceof PsiClassType) && ((PsiClassType)elementType).isRaw())) {
private static boolean isValidElementType(PsiType elementType, PsiElement context, boolean allowRaw) {
if(elementType == null || (!allowRaw && (elementType instanceof PsiClassType) && ((PsiClassType)elementType).isRaw())) {
// Raw type in any stream step is not supported
return false;
}
@@ -213,7 +213,7 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
FunctionHelper fn = FunctionHelper.create(args[0], 1, true);
if (fn == null) return null;
PsiType elementType = PsiUtil.substituteTypeParameter(type, CommonClassNames.JAVA_LANG_ITERABLE, 0, false);
if(!isValidElementType(elementType, terminalCall)) return null;
if(!isValidElementType(elementType, terminalCall, true)) return null;
elementType = GenericsUtil.getVariableTypeByExpressionType(elementType);
TerminalOperation terminal = new TerminalOperation.ForEachTerminalOperation(fn);
SourceOperation source = new SourceOperation.ForEachSource(qualifier);
@@ -25,6 +25,12 @@ public class Main {
return collection;
}
void testRawTypeSupport(List<List> list) {
for (List l : list) {
System.out.println(l.size());
}
}
public interface SomeInterface {
Set<? extends SomeInterface> nodes();
@@ -19,6 +19,10 @@ public class Main {
return collection;
}
void testRawTypeSupport(List<List> list) {
list.forEach(l -> System.out.println(l.size()));
}
public interface SomeInterface {
Set<? extends SomeInterface> nodes();