From a2c90af6b349fef3520229c6d0e5fddadbcf1bdd Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Mon, 11 Jan 2016 20:14:09 +0100 Subject: [PATCH] trivial method reference inspection (IDEA-123298) --- .../TrivialMethodReferenceInspection.java | 74 +++++++++++++++++++ .../afterPredicate.java | 8 ++ .../afterPredicateInheritor.java | 12 +++ .../beforePredicate.java | 8 ++ .../beforePredicateInheritor.java | 12 +++ .../TrivialMethodReferenceInspectionTest.java | 45 +++++++++++ .../TrivialMethodReference.html | 5 ++ resources/src/META-INF/IdeaPlugin.xml | 3 + 8 files changed, 167 insertions(+) create mode 100644 java/java-analysis-impl/src/com/intellij/codeInspection/TrivialMethodReferenceInspection.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/afterPredicate.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/afterPredicateInheritor.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/beforePredicate.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/beforePredicateInheritor.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/TrivialMethodReferenceInspectionTest.java create mode 100644 resources-en/src/inspectionDescriptions/TrivialMethodReference.html diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/TrivialMethodReferenceInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/TrivialMethodReferenceInspection.java new file mode 100644 index 000000000000..c2678959d23c --- /dev/null +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/TrivialMethodReferenceInspection.java @@ -0,0 +1,74 @@ +/* + * 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.psi.*; +import com.intellij.psi.util.MethodSignatureUtil; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +public class TrivialMethodReferenceInspection extends BaseJavaBatchLocalInspectionTool { + @NotNull + @Override + public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { + return new JavaElementVisitor() { + @Override + public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) { + final PsiExpression qualifierExpression = expression.getQualifierExpression(); + final PsiElement referenceNameElement = expression.getReferenceNameElement(); + if (qualifierExpression != null && referenceNameElement != null) { + final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(expression); + if (interfaceMethod != null) { + final PsiElement resolve = expression.resolve(); + if (resolve instanceof PsiMethod && + (interfaceMethod == resolve || MethodSignatureUtil.isSuperMethod(interfaceMethod, (PsiMethod)resolve))) { + holder.registerProblem(referenceNameElement, "Method reference can be replaced with qualifier", new ReplaceMethodRefWithQualifierFix()); + } + } + } + } + }; + } + + private static class ReplaceMethodRefWithQualifierFix implements LocalQuickFix { + @Nls + @NotNull + @Override + public String getName() { + return getFamilyName(); + } + + @Nls + @NotNull + @Override + public String getFamilyName() { + return "Replace with qualifier"; + } + + @Override + public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { + PsiElement element = descriptor.getPsiElement(); + final PsiElement parent = element != null ? element.getParent() : null; + if (parent instanceof PsiMethodReferenceExpression) { + final PsiExpression qualifierExpression = ((PsiMethodReferenceExpression)parent).getQualifierExpression(); + if (qualifierExpression != null) { + parent.replace(qualifierExpression); + } + } + } + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/afterPredicate.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/afterPredicate.java new file mode 100644 index 000000000000..2e8598624932 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/afterPredicate.java @@ -0,0 +1,8 @@ +// "Replace with qualifier" "true" +import java.util.function.Predicate; + +class Test { + void foo(Predicate p){ + Predicate stringPredicate = p; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/afterPredicateInheritor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/afterPredicateInheritor.java new file mode 100644 index 000000000000..311417295d5d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/afterPredicateInheritor.java @@ -0,0 +1,12 @@ +// "Replace with qualifier" "true" +import java.util.function.Predicate; + +class Test implements Predicate { + void foo(){ + Predicate stringPredicate = this; + } + + public boolean test(String s) { + return true; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/beforePredicate.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/beforePredicate.java new file mode 100644 index 000000000000..67d9a0ac4203 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/beforePredicate.java @@ -0,0 +1,8 @@ +// "Replace with qualifier" "true" +import java.util.function.Predicate; + +class Test { + void foo(Predicate p){ + Predicate stringPredicate = p::test; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/beforePredicateInheritor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/beforePredicateInheritor.java new file mode 100644 index 000000000000..2270c5685ba9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference/beforePredicateInheritor.java @@ -0,0 +1,12 @@ +// "Replace with qualifier" "true" +import java.util.function.Predicate; + +class Test implements Predicate { + void foo(){ + Predicate stringPredicate = this::test; + } + + public boolean test(String s) { + return true; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/TrivialMethodReferenceInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/TrivialMethodReferenceInspectionTest.java new file mode 100644 index 000000000000..00263f77aab1 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/TrivialMethodReferenceInspectionTest.java @@ -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.TrivialMethodReferenceInspection; +import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.testFramework.IdeaTestUtil; +import org.jetbrains.annotations.NotNull; + + +public class TrivialMethodReferenceInspectionTest extends LightQuickFixParameterizedTestCase { + @NotNull + @Override + protected LocalInspectionTool[] configureLocalInspectionTools() { + return new LocalInspectionTool[]{ + new TrivialMethodReferenceInspection(), + }; + } + + public void test() throws Exception { doAllTests(); } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/trivialMethodReference"; + } + + @Override + protected Sdk getProjectJDK() { + return IdeaTestUtil.getMockJdk18(); + } +} \ No newline at end of file diff --git a/resources-en/src/inspectionDescriptions/TrivialMethodReference.html b/resources-en/src/inspectionDescriptions/TrivialMethodReference.html new file mode 100644 index 000000000000..b4674967606f --- /dev/null +++ b/resources-en/src/inspectionDescriptions/TrivialMethodReference.html @@ -0,0 +1,5 @@ + + +This inspection reports method references which point to the abstract methods of the same functional interface types and hence can be replaced with their qualifiers + + \ No newline at end of file diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index f82a80267692..b5cffccc5e54 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -726,6 +726,9 @@ +