Java inspection: Simplify stream API call chains (IDEA-140724, IDEA-156324)

This commit is contained in:
Pavel Dolgov
2016-05-25 14:34:32 +03:00
parent 0d167a1012
commit 0b542e981c
12 changed files with 341 additions and 0 deletions
@@ -0,0 +1,200 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Pavel.Dolgov
*/
public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalInspectionTool {
public static final Logger LOG = Logger.getInstance("#" + SimplifyStreamApiCallChainsInspection.class.getName());
public static final String FOR_EACH_METHOD = "forEach";
public static final String STREAM_METHOD = "stream";
public static final String AS_LIST_METHOD = "asList";
public static final String OF_METHOD = "of";
@Override
public boolean isEnabledByDefault() {
return true;
}
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!PsiUtil.isLanguageLevel8OrHigher(holder.getFile())) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@Override
public void visitMethodCallExpression(PsiMethodCallExpression methodCall) {
if (isCallOf(methodCall, CommonClassNames.JAVA_UTIL_COLLECTION, STREAM_METHOD, 0)) {
final PsiMethodCallExpression qualifierCall = getQualifierMethodCall(methodCall);
if (isCallOf(qualifierCall, CommonClassNames.JAVA_UTIL_ARRAYS, AS_LIST_METHOD, 1)) {
final PsiExpression[] argumentExpressions = qualifierCall.getArgumentList().getExpressions();
if (argumentExpressions.length == 1 && argumentExpressions[0].getType() instanceof PsiArrayType) {
holder.registerProblem(methodCall, null, "Arrays.asList().stream() can be replaced with Arrays.stream()",
new ArraysAsListSingleArrayFix());
}
else {
holder.registerProblem(methodCall, null, "Arrays.asList().stream() can be replaced with Stream.of()",
new ArraysAsListVarargFix());
}
}
}
else if (isCallOf(methodCall, CommonClassNames.JAVA_UTIL_STREAM_STREAM, FOR_EACH_METHOD, 1)) {
final PsiMethodCallExpression qualifierCall = getQualifierMethodCall(methodCall);
if (isCallOf(qualifierCall, CommonClassNames.JAVA_UTIL_COLLECTION, STREAM_METHOD, 0)) {
holder.registerProblem(methodCall, getCallChainRange(methodCall, qualifierCall),
"Collection.stream().forEach() can be replaced with Collection.forEach()",
new CollectionForEachFix());
}
}
}
};
}
private static PsiMethodCallExpression getQualifierMethodCall(PsiMethodCallExpression methodCall) {
final PsiExpression qualifierExpression = methodCall.getMethodExpression().getQualifierExpression();
if (qualifierExpression instanceof PsiMethodCallExpression) {
return (PsiMethodCallExpression)qualifierExpression;
}
return null;
}
@NotNull
protected TextRange getCallChainRange(@NotNull PsiMethodCallExpression expression,
@NotNull PsiMethodCallExpression qualifierExpression) {
final PsiReferenceExpression qualifierMethodExpression = qualifierExpression.getMethodExpression();
final PsiElement qualifierNameElement = qualifierMethodExpression.getReferenceNameElement();
final int startOffset = (qualifierNameElement != null ? qualifierNameElement : qualifierMethodExpression).getTextOffset();
final int endOffset = expression.getMethodExpression().getTextRange().getEndOffset();
return new TextRange(startOffset, endOffset).shiftRight(-expression.getTextOffset());
}
@Contract("null, _, _, _ -> false")
protected static boolean isCallOf(@Nullable PsiMethodCallExpression expression,
@NotNull String className,
@NotNull String methodName,
int parametersCount) {
if (expression == null) return false;
final PsiMethod method = expression.resolveMethod();
if (method != null && methodName.equals(method.getName()) && method.getParameterList().getParametersCount() == parametersCount) {
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null && className.equals(containingClass.getQualifiedName())) {
return true;
}
}
return false;
}
private static abstract class CallChainFixBase implements LocalQuickFix {
@Nls
@NotNull
@Override
public String getName() {
return getFamilyName();
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getStartElement();
if (element instanceof PsiMethodCallExpression) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(element.getContainingFile())) return;
final PsiMethodCallExpression expression = (PsiMethodCallExpression)element;
final PsiExpression forEachMethodQualifier = expression.getMethodExpression().getQualifierExpression();
if (forEachMethodQualifier instanceof PsiMethodCallExpression) {
final PsiMethodCallExpression previousExpression = (PsiMethodCallExpression)forEachMethodQualifier;
final PsiExpression qualifierExpression = previousExpression.getMethodExpression().getQualifierExpression();
if (qualifierExpression != null) {
final String text = createExpressionText(expression, previousExpression, qualifierExpression);
final PsiExpression newElement = JavaPsiFacade.getElementFactory(project).createExpressionFromText(text, null);
final PsiElement shortenedElement = JavaCodeStyleManager.getInstance(project).shortenClassReferences(newElement);
element.replace(shortenedElement);
}
}
}
}
@NotNull
protected abstract String createExpressionText(@NotNull PsiMethodCallExpression methodCall,
@NotNull PsiMethodCallExpression qualifierCall,
@NotNull PsiExpression qualifierExpression);
}
private static class ArraysAsListVarargFix extends CallChainFixBase {
@Nls
@NotNull
@Override
public String getFamilyName() {
return "Replace Arrays.asList().stream() with Stream.of()";
}
@NotNull
protected String createExpressionText(@NotNull PsiMethodCallExpression methodCall,
@NotNull PsiMethodCallExpression qualifierCall,
@NotNull PsiExpression qualifierExpression) {
return (CommonClassNames.JAVA_UTIL_STREAM_STREAM + "." + OF_METHOD) + qualifierCall.getArgumentList().getText();
}
}
private static class ArraysAsListSingleArrayFix extends CallChainFixBase {
@Nls
@NotNull
@Override
public String getFamilyName() {
return "Replace Arrays.asList().stream() with Arrays.stream()";
}
@NotNull
protected String createExpressionText(@NotNull PsiMethodCallExpression methodCall,
@NotNull PsiMethodCallExpression qualifierCall,
@NotNull PsiExpression qualifierExpression) {
return (CommonClassNames.JAVA_UTIL_ARRAYS + "." + STREAM_METHOD) + qualifierCall.getArgumentList().getText();
}
}
private static class CollectionForEachFix extends CallChainFixBase {
@Nls
@NotNull
@Override
public String getFamilyName() {
return "Replace Collection.stream().forEach() with Collection.forEach()";
}
@NotNull
@Override
protected String createExpressionText(@NotNull PsiMethodCallExpression methodCall,
@NotNull PsiMethodCallExpression qualifierCall,
@NotNull PsiExpression qualifierExpression) {
return qualifierExpression.getText() + "." + FOR_EACH_METHOD + methodCall.getArgumentList().getText();
}
}
}
@@ -0,0 +1,9 @@
// "Replace Arrays.asList().stream() with Arrays.stream()" "true"
import java.util.Arrays;
class AsListArrayStream {
String max(String[] args) {
return Arrays.stream(args).max(String::compareTo);
}
}
@@ -0,0 +1,10 @@
// "Replace Arrays.asList().stream() with Stream.of()" "true"
import java.util.Arrays;
import java.util.stream.Stream;
class AsListLiteralStream {
Stream<String> abc() {
return Stream.of("a", "b", "c");
}
}
@@ -0,0 +1,11 @@
// "Replace Collection.stream().forEach() with Collection.forEach()" "true"
import java.util.Arrays;
import java.util.Collection;
class Test {
void print() {
Collection<Character> def = Arrays.asList('d', 'e', 'f');
def.forEach(c -> System.out.print(" " + c));
}
}
@@ -0,0 +1,11 @@
// "Replace Collection.stream().forEach() with Collection.forEach()" "true"
import java.util.Arrays;
import java.util.Collection;
class Test {
void print() {
Collection<Character> def = Arrays.asList('d', 'e', 'f');
def.forEach(System.out::print);
}
}
@@ -0,0 +1,9 @@
// "Replace Arrays.asList().stream() with Arrays.stream()" "true"
import java.util.Arrays;
class AsListArrayStream {
String max(String[] args) {
return Arrays.asL<caret>ist(args).stream().max(String::compareTo);
}
}
@@ -0,0 +1,10 @@
// "Replace Arrays.asList().stream() with Stream.of()" "true"
import java.util.Arrays;
import java.util.stream.Stream;
class AsListLiteralStream {
Stream<String> abc() {
return Arrays.asList("a", "b", "c").stre<caret>am();
}
}
@@ -0,0 +1,11 @@
// "Replace Collection.stream().forEach() with Collection.forEach()" "true"
import java.util.Arrays;
import java.util.Collection;
class Test {
void print() {
Collection<Character> def = Arrays.asList('d', 'e', 'f');
def.st<caret>ream().forEach(c -> System.out.print(" " + c));
}
}
@@ -0,0 +1,11 @@
// "Replace Collection.stream().forEach() with Collection.forEach()" "true"
import java.util.Arrays;
import java.util.Collection;
class Test {
void print() {
Collection<Character> def = Arrays.asList('d', 'e', 'f');
def.stream().forE<caret>ach(System.out::print);
}
}
@@ -0,0 +1,47 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.testFramework.IdeaTestUtil;
import org.jetbrains.annotations.NotNull;
/**
* @author Pavel.Dolgov
*/
public class SimplifyStreamApiCallChainsInspectionTest extends LightQuickFixParameterizedTestCase {
@NotNull
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[]{new SimplifyStreamApiCallChainsInspection()};
}
public void test() throws Exception {
doAllTests();
}
@Override
protected Sdk getProjectJDK() {
return IdeaTestUtil.getMockJdk18();
}
@Override
protected String getBasePath() {
return "/inspection/streamApiCallChains";
}
}
@@ -0,0 +1,7 @@
<html>
<body>
This inspection reports redundant stream API call chains.
<br>
For example, Collection.stream().forEach() can be replaced with Collection.forEach()
</body>
</html>
+5
View File
@@ -786,6 +786,11 @@
groupKey="group.names.language.level.specific.issues.and.migration.aids" enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.codeInspection.java18api.Java8CollectionsApiInspection"
displayName="Collection usages can be replaced with single method"/>
<localInspection groupPath="Java" language="JAVA" shortName="SimplifyStreamApiCallChains"
groupBundle="messages.InspectionsBundle"
groupKey="group.names.declaration.redundancy" enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.codeInspection.SimplifyStreamApiCallChainsInspection"
displayName="Simplify stream API call chains"/>
<intentionAction>
<className>com.intellij.codeInsight.daemon.quickFix.RedundantLambdaParameterTypeIntention</className>