mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 05:21:29 +07:00
trivial functional expressions usage (IDEA-131090)
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
// "Replace method call on lambda with lambda body" "true"
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class Test {
|
||||
{
|
||||
String s = "";
|
||||
String str = s;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Replace method call on lambda with lambda body" "true"
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class Test {
|
||||
String s = "";
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Replace method call on method reference with corresponding method call" "true"
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class Test {
|
||||
String s = foo();
|
||||
|
||||
private String foo() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace method call on lambda with lambda body" "true"
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class Test {
|
||||
{
|
||||
String str = ((Supplier<String>)() -> {
|
||||
String s = "";
|
||||
return s;
|
||||
}).g<caret>et();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Replace method call on lambda with lambda body" "false"
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class Test {
|
||||
String str = ((Supplier<String>) () -> {
|
||||
String s = "";
|
||||
return s;
|
||||
}).g<caret>et();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace method call on lambda with lambda body" "true"
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class Test {
|
||||
String s = ((Supplier<String>) () -> {
|
||||
return "";
|
||||
}).g<caret>et();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Replace method call on method reference with corresponding method call" "true"
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class Test {
|
||||
String s = ((Supplier<String>) this::foo).ge<caret>t();
|
||||
|
||||
private String foo() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Replace method call on method reference with corresponding method call" "false"
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class Test {
|
||||
String s = ((Supplier<String>) this::foo).ge<caret>t();
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.TrivialFunctionalExpressionUsageInspection;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
public class TrivialFunctionalExpressionUsageInspectionTest extends LightQuickFixParameterizedTestCase {
|
||||
@NotNull
|
||||
@Override
|
||||
protected LocalInspectionTool[] configureLocalInspectionTools() {
|
||||
return new LocalInspectionTool[]{
|
||||
new TrivialFunctionalExpressionUsageInspection(),
|
||||
};
|
||||
}
|
||||
|
||||
public void test() throws Exception { doAllTests(); }
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/trivialFunctionalExpressionUsage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Sdk getProjectJDK() {
|
||||
return IdeaTestUtil.getMockJdk18();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
* 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.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.util.LambdaRefactoringUtil;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TrivialFunctionalExpressionUsageInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
|
||||
return new JavaElementVisitor() {
|
||||
|
||||
@Override
|
||||
public void visitMethodReferenceExpression(final PsiMethodReferenceExpression expression) {
|
||||
doCheckMethodCallOnFunctionalExpression(expression, new Condition<PsiElement>() {
|
||||
@Override
|
||||
public boolean value(PsiElement element) {
|
||||
return expression.resolve() != null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitLambdaExpression(final PsiLambdaExpression expression) {
|
||||
doCheckMethodCallOnFunctionalExpression(expression, new Condition<PsiElement>() {
|
||||
@Override
|
||||
public boolean value(PsiElement ggParent) {
|
||||
final PsiElement body = expression.getBody();
|
||||
if (!(body instanceof PsiCodeBlock) ||
|
||||
((PsiCodeBlock)body).getStatements().length == 1) {
|
||||
return true;
|
||||
}
|
||||
if (LambdaUtil.getReturnExpressions(expression).size() > 1) {
|
||||
return false;
|
||||
}
|
||||
final PsiElement callParent = ggParent.getParent();
|
||||
return callParent instanceof PsiStatement ||
|
||||
callParent instanceof PsiLocalVariable;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void doCheckMethodCallOnFunctionalExpression(PsiFunctionalExpression expression,
|
||||
Condition<PsiElement> elementContainerCondition) {
|
||||
final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
|
||||
if (parent instanceof PsiTypeCastExpression) {
|
||||
final PsiElement gParent = PsiUtil.skipParenthesizedExprUp(parent.getParent());
|
||||
if (gParent instanceof PsiReferenceExpression) {
|
||||
final PsiElement ggParent = gParent.getParent();
|
||||
if (ggParent instanceof PsiMethodCallExpression) {
|
||||
final PsiMethod resolveMethod = ((PsiMethodCallExpression)ggParent).resolveMethod();
|
||||
final PsiElement referenceNameElement = ((PsiMethodCallExpression)ggParent).getMethodExpression().getReferenceNameElement();
|
||||
if (resolveMethod != null &&
|
||||
resolveMethod.getParameterList().getParametersCount() == 0 && //todo pass args as parameters
|
||||
resolveMethod == LambdaUtil.getFunctionalInterfaceMethod(((PsiTypeCastExpression)parent).getType()) &&
|
||||
referenceNameElement != null &&
|
||||
elementContainerCondition.value(ggParent)) {
|
||||
final ReplaceWithLambdaBodyFix fix =
|
||||
new ReplaceWithLambdaBodyFix("Replace method call " +
|
||||
(expression instanceof PsiLambdaExpression ? "on lambda with lambda body"
|
||||
: "on method reference with corresponding method call"));
|
||||
holder.registerProblem(referenceNameElement, "Method call can be simplified", fix);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static class ReplaceWithLambdaBodyFix implements LocalQuickFix {
|
||||
private String myName;
|
||||
|
||||
public ReplaceWithLambdaBodyFix(String name) {
|
||||
myName = name;
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return getFamilyName();
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return myName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
final PsiMethodCallExpression callExpression = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiMethodCallExpression.class);
|
||||
if (callExpression != null) {
|
||||
final PsiExpression qualifierExpression = PsiUtil.skipParenthesizedExprDown(callExpression.getMethodExpression().getQualifierExpression());
|
||||
if (qualifierExpression instanceof PsiTypeCastExpression) {
|
||||
final PsiExpression element = ((PsiTypeCastExpression)qualifierExpression).getOperand();
|
||||
if (element instanceof PsiLambdaExpression) {
|
||||
replaceWithLambdaBody(callExpression, (PsiLambdaExpression)element);
|
||||
}
|
||||
else if (element instanceof PsiMethodReferenceExpression) {
|
||||
final PsiLambdaExpression lambdaExpression =
|
||||
LambdaRefactoringUtil.convertMethodReferenceToLambda((PsiMethodReferenceExpression)element, false, true);
|
||||
replaceWithLambdaBody(callExpression, lambdaExpression);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void replaceWithLambdaBody(PsiMethodCallExpression callExpression, PsiLambdaExpression element) {
|
||||
final PsiElement body = element.getBody();
|
||||
if (body instanceof PsiExpression) {
|
||||
callExpression.replace(body);
|
||||
}
|
||||
else if (body instanceof PsiCodeBlock) {
|
||||
final PsiElement parent = callExpression.getParent();
|
||||
if (parent instanceof PsiStatement) {
|
||||
final PsiElement gParent = parent.getParent();
|
||||
for (PsiStatement statement : ((PsiCodeBlock)body).getStatements()) {
|
||||
PsiElement toInsert;
|
||||
if (statement instanceof PsiReturnStatement) {
|
||||
toInsert = ((PsiReturnStatement)statement).getReturnValue();
|
||||
}
|
||||
else {
|
||||
toInsert = statement;
|
||||
}
|
||||
|
||||
if (toInsert != null) {
|
||||
gParent.addBefore(toInsert, parent);
|
||||
}
|
||||
}
|
||||
parent.delete();
|
||||
}
|
||||
else {
|
||||
final PsiStatement[] statements = ((PsiCodeBlock)body).getStatements();
|
||||
if (statements.length > 0) {
|
||||
final PsiStatement anchor = PsiTreeUtil.getParentOfType(parent, PsiStatement.class);
|
||||
if (anchor != null) {
|
||||
final PsiElement gParent = anchor.getParent();
|
||||
for (int i = 0; i < statements.length - 1; i++) {
|
||||
gParent.addBefore(statements[i], anchor);
|
||||
}
|
||||
}
|
||||
final PsiExpression returnValue = ((PsiReturnStatement)statements[statements.length - 1]).getReturnValue();
|
||||
if (returnValue != null) {
|
||||
callExpression.replace(returnValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports method calls to abstract methods of functional interfaces, when qualifiers for the calls are functional expressions: lambdas or method references.
|
||||
</body>
|
||||
</html>
|
||||
@@ -729,6 +729,9 @@
|
||||
<localInspection groupPath="Java" language="JAVA" shortName="TrivialMethodReference" displayName="Method reference can be replaced with its qualifier"
|
||||
groupKey="group.names.language.level.specific.issues.and.migration.aids" groupBundle="messages.InspectionsBundle" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="com.intellij.codeInspection.TrivialMethodReferenceInspection"/>
|
||||
<localInspection groupPath="Java" language="JAVA" shortName="TrivialFunctionalExpressionUsage" displayName="Trivial usage of functional expression"
|
||||
groupKey="group.names.language.level.specific.issues.and.migration.aids" groupBundle="messages.InspectionsBundle" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="com.intellij.codeInspection.TrivialFunctionalExpressionUsageInspection"/>
|
||||
<localInspection groupPath="Java" language="JAVA" shortName="CodeBlock2Expr" displayName="Statement lambda can be replaced with expression lambda"
|
||||
groupKey="group.names.language.level.specific.issues.and.migration.aids" groupBundle="messages.InspectionsBundle" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="com.intellij.codeInspection.RedundantLambdaCodeBlockInspection" />
|
||||
|
||||
Reference in New Issue
Block a user