StreamToLoopInspection: disable for raw types (IDEA-163493), disable for unresolved method references

This commit is contained in:
Tagir Valeev
2016-11-03 09:45:10 +07:00
parent f4f11e0c88
commit ef215da116
9 changed files with 86 additions and 5 deletions

View File

@@ -103,11 +103,13 @@ abstract class FunctionHelper {
return new LambdaFunctionHelper(body, parameters);
}
if (expression instanceof PsiMethodReferenceExpression) {
PsiType functionalInterfaceType = ((PsiMethodReferenceExpression)expression).getFunctionalInterfaceType();
PsiMethodReferenceExpression methodRef = (PsiMethodReferenceExpression)expression;
if (methodRef.resolve() == null) return null;
PsiType functionalInterfaceType = methodRef.getFunctionalInterfaceType();
PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
if (interfaceMethod == null) return null;
if (interfaceMethod.getParameterList().getParametersCount() != paramCount) return null;
return new MethodReferenceFunctionHelper(functionalInterfaceType, (PsiMethodReferenceExpression)expression);
return new MethodReferenceFunctionHelper(functionalInterfaceType, methodRef);
}
if (expression instanceof PsiReferenceExpression && ExpressionUtils.isSimpleExpression(expression)) {
PsiType functionalInterfaceType = expression.getType();

View File

@@ -21,6 +21,7 @@ import com.intellij.psi.PsiMethodCallExpression;
import com.intellij.psi.PsiType;
import com.intellij.psi.util.PsiTypesUtil;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@@ -52,7 +53,8 @@ abstract class Operation {
public void suggestNames(StreamVariable inVar, StreamVariable outVar) {}
@Nullable
static Operation createIntermediate(String name, PsiExpression[] args, StreamVariable outVar, PsiType inType) {
static Operation createIntermediate(@NotNull String name, @NotNull PsiExpression[] args,
@NotNull StreamVariable outVar, @NotNull PsiType inType) {
if(name.equals("distinct") && args.length == 0) {
return new DistinctOperation();
}

View File

@@ -132,10 +132,15 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
if(className == null) return null;
PsiType callType = call.getType();
if(callType == null) return null;
if(InheritanceUtil.isInheritor(aClass, CommonClassNames.JAVA_UTIL_STREAM_BASE_STREAM)) {
if(InheritanceUtil.isInheritor(aClass, CommonClassNames.JAVA_UTIL_STREAM_BASE_STREAM) &&
!method.getModifierList().hasExplicitModifier(PsiModifier.STATIC)) {
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
if(qualifier != null) {
PsiType elementType = getStreamElementType(qualifier.getType());
if(elementType == null || ((elementType instanceof PsiClassType) && ((PsiClassType)elementType).isRaw())) {
// Raw type in any stream step is not supported
return null;
}
Operation op = Operation.createIntermediate(name, args, outVar, elementType);
if (op != null) return op;
op = TerminalOperation.createTerminal(name, args, elementType, callType, call.getParent() instanceof PsiExpressionStatement);

View File

@@ -63,7 +63,8 @@ abstract class TerminalOperation extends Operation {
}
@Nullable
static TerminalOperation createTerminal(String name, PsiExpression[] args, PsiType elementType, PsiType resultType, boolean isVoid) {
static TerminalOperation createTerminal(@NotNull String name, @NotNull PsiExpression[] args,
@NotNull PsiType elementType, @NotNull PsiType resultType, boolean isVoid) {
if(isVoid) {
if ((name.equals("forEach") || name.equals("forEachOrdered")) && args.length == 1) {
FunctionHelper fn = FunctionHelper.create(args[0], 1, true);

View File

@@ -0,0 +1,18 @@
// "Replace Stream API chain with loop" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
interface Role {}
class FooRole {
public Role[] getRoles(Predicate<Object> p, List<Role> roles) {
List<Role> list = new ArrayList<>();
for (Role role : roles) {
if (p.test(role)) {
list.add(role);
}
}
return list.toArray(new Role[0]);
}
}

View File

@@ -0,0 +1,11 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
import java.util.function.Predicate;
interface Role {}
class FooRole {
public Role[] getRoles(Predicate<Object> p, List<Role> roles) {
return roles.stream().filter(p).toArr<caret>ay (Role[]::new);
}
}

View File

@@ -0,0 +1,11 @@
// "Replace Stream API chain with loop" "false"
import java.util.List;
import java.util.function.Predicate;
interface Role {}
class FooRole {
public Role[] getRoles(Predicate p, List<Role> roles) {
return roles.stream().filter(p).toArr<caret>ay (Role[]::new);
}
}

View File

@@ -0,0 +1,16 @@
// "Replace Stream API chain with loop" "false"
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Arrays.asList;
public class Main {
private static List<Object> test(List<List> list) {
return list.stream().<Object>flatMap(List::stream).coll<caret>ect(Collectors.toList());
}
public static void main(String[] args) {
System.out.println(test(asList(asList("a", "d"), asList("c", "b"))));
}
}

View File

@@ -0,0 +1,15 @@
// "Replace Stream API chain with loop" "false"
import java.util.List;
import static java.util.Arrays.asList;
public class Main {
private static int test(List<String> list) {
return list.stream().mapToInt(Blahblah::size).su<caret>m();
}
public static void main(String[] args) {
System.out.println(test(asList("a", "b", "c")));
}
}