IDEA-163302 Suggest to use IntStream.boxed() if manually mapping to an Integer

This commit is contained in:
Tagir Valeev
2016-11-03 11:26:21 +07:00
parent 551d91c8f3
commit f04509a776
20 changed files with 311 additions and 25 deletions
@@ -21,6 +21,7 @@ import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.StreamApiUtil;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
@@ -71,7 +72,7 @@ abstract class SourceOperation extends Operation {
PsiType type = args[0].getType();
if(type instanceof PsiArrayType) {
PsiType componentType = ((PsiArrayType)type).getComponentType();
if(StreamToLoopInspection.getStreamElementType(callType).isAssignableFrom(componentType)) {
if(StreamApiUtil.getStreamElementType(callType).isAssignableFrom(componentType)) {
return new ForEachSource(args[0]);
}
}
@@ -32,6 +32,7 @@ import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.siyeh.ig.psiutils.StreamApiUtil;
import one.util.streamex.IntStreamEx;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Contract;
@@ -136,7 +137,7 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
!method.getModifierList().hasExplicitModifier(PsiModifier.STATIC)) {
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
if(qualifier != null) {
PsiType elementType = getStreamElementType(qualifier.getType());
PsiType elementType = StreamApiUtil.getStreamElementType(qualifier.getType());
if(elementType == null || ((elementType instanceof PsiClassType) && ((PsiClassType)elementType).isRaw())) {
// Raw type in any stream step is not supported
return null;
@@ -172,7 +173,7 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
if(!(qualifier instanceof PsiMethodCallExpression)) return null;
currentCall = (PsiMethodCallExpression)qualifier;
if(op.changesVariable()) {
PsiType type = getStreamElementType(currentCall.getType());
PsiType type = StreamApiUtil.getStreamElementType(currentCall.getType());
if(type == null) return null;
lastVar = new StreamVariable(type);
}
@@ -292,28 +293,6 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
}
}
@Contract("null -> null")
static PsiType getStreamElementType(PsiType type) {
if(!(type instanceof PsiClassType)) return null;
PsiClass aClass = ((PsiClassType)type).resolve();
if(InheritanceUtil.isInheritor(aClass, false, CommonClassNames.JAVA_UTIL_STREAM_INT_STREAM)) {
return PsiType.INT;
}
if(InheritanceUtil.isInheritor(aClass, false, CommonClassNames.JAVA_UTIL_STREAM_LONG_STREAM)) {
return PsiType.LONG;
}
if(InheritanceUtil.isInheritor(aClass, false, CommonClassNames.JAVA_UTIL_STREAM_DOUBLE_STREAM)) {
return PsiType.DOUBLE;
}
PsiType[] parameters = ((PsiClassType)type).getParameters();
if(parameters.length != 1) return null;
PsiType streamType = parameters[0];
if(streamType instanceof PsiCapturedWildcardType) {
streamType = ((PsiCapturedWildcardType)streamType).getUpperBound();
}
return streamType;
}
static class StreamToLoopReplacementContext {
private final boolean myHasNestedLoops;
private final boolean myInReturn;