lambda can be converted to method reference inspection

This commit is contained in:
anna
2012-10-09 15:51:41 +02:00
parent c4695e9e30
commit 0fbf5f2464
32 changed files with 774 additions and 1 deletions

View File

@@ -0,0 +1,209 @@
/*
* Copyright 2000-2012 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.GroupNames;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
/**
* User: anna
*/
public class LambdaCanBeMethReferenceInspection extends BaseJavaLocalInspectionTool {
public static final Logger LOG = Logger.getInstance("#" + LambdaCanBeMethReferenceInspection.class.getName());
@Nls
@NotNull
@Override
public String getGroupDisplayName() {
return GroupNames.LANGUAGE_LEVEL_SPECIFIC_GROUP_NAME;
}
@Nls
@NotNull
@Override
public String getDisplayName() {
return "Lambda can be replaced with method reference";
}
@Override
public boolean isEnabledByDefault() {
return true;
}
@NotNull
@Override
public String getShortName() {
return "Convert2MethodRef";
}
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override
public void visitLambdaExpression(PsiLambdaExpression expression) {
super.visitLambdaExpression(expression);
if (PsiUtil.getLanguageLevel(expression).isAtLeast(LanguageLevel.JDK_1_8)) {
final PsiElement body = expression.getBody();
PsiCallExpression methodCall = null;
if (body instanceof PsiCallExpression) {
methodCall = (PsiCallExpression)body;
} else if (body instanceof PsiCodeBlock) {
final PsiStatement[] statements = ((PsiCodeBlock)body).getStatements();
if (statements.length == 1) {
if (statements[0] instanceof PsiReturnStatement) {
final PsiExpression returnValue = ((PsiReturnStatement)statements[0]).getReturnValue();
if (returnValue instanceof PsiCallExpression) {
methodCall = (PsiCallExpression)returnValue;
}
} else if (statements[0] instanceof PsiExpressionStatement) {
final PsiExpression expr = ((PsiExpressionStatement)statements[0]).getExpression();
if (expr instanceof PsiCallExpression) {
methodCall = (PsiCallExpression)expr;
}
}
}
}
if (methodCall != null) {
final PsiExpressionList argumentList = methodCall.getArgumentList();
if (argumentList != null) {
final PsiParameter[] parameters = expression.getParameterList().getParameters();
final PsiExpression[] expressions = argumentList.getExpressions();
final PsiMethod psiMethod = methodCall.resolveMethod();
final PsiClass containingClass;
boolean isConstructor;
if (psiMethod == null) {
isConstructor = true;
if (!(methodCall instanceof PsiNewExpression)) return;
final PsiJavaCodeReferenceElement classReference = ((PsiNewExpression)methodCall).getClassOrAnonymousClassReference();
if (classReference == null) return;
containingClass = (PsiClass)classReference.resolve();
} else {
containingClass = psiMethod.getContainingClass();
isConstructor = psiMethod.isConstructor();
}
if (containingClass == null) return;
boolean isReceiverType = parameters.length > 0 && LambdaUtil.isReceiverType(parameters[0].getType(), containingClass, PsiUtil.resolveGenericsClassInType(parameters[0].getType()).getSubstitutor());
final boolean staticOrValidConstructorRef;
if (isConstructor) {
staticOrValidConstructorRef =
(containingClass.getContainingClass() == null || containingClass.hasModifierProperty(PsiModifier.STATIC));
} else {
staticOrValidConstructorRef = psiMethod.hasModifierProperty(PsiModifier.STATIC);
}
final int offset = isReceiverType && !staticOrValidConstructorRef ? 1 : 0;
if (parameters.length != expressions.length + offset) return;
for (int i = 0; i < expressions.length; i++) {
PsiExpression psiExpression = expressions[i];
if (!(psiExpression instanceof PsiReferenceExpression)) return;
final PsiElement resolve = ((PsiReferenceExpression)psiExpression).resolve();
if (resolve == null) return;
if (parameters[i + offset] != resolve) return;
}
if (offset > 0) {
final PsiExpression qualifierExpression;
if (methodCall instanceof PsiMethodCallExpression) {
qualifierExpression = ((PsiMethodCallExpression)methodCall).getMethodExpression().getQualifierExpression();
} else if (methodCall instanceof PsiNewExpression) {
qualifierExpression = ((PsiNewExpression)methodCall).getQualifier();
} else {
qualifierExpression = null;
}
if (!(qualifierExpression instanceof PsiReferenceExpression) || ((PsiReferenceExpression)qualifierExpression).resolve() != parameters[0]) return;
}
holder.registerProblem(methodCall,
"Can be replaced with method reference",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithMethodRefFix());
}
}
}
}
};
}
private static class ReplaceWithMethodRefFix implements LocalQuickFix {
@NotNull
@Override
public String getName() {
return "Replace lambda with method reference";
}
@NotNull
@Override
public String getFamilyName() {
return getName();
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
if (lambdaExpression == null) return;
String methodRefText = null;
if (element instanceof PsiMethodCallExpression) {
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)element;
final PsiMethod psiMethod = methodCall.resolveMethod();
LOG.assertTrue(psiMethod != null);
final PsiClass containingClass = psiMethod.getContainingClass();
LOG.assertTrue(containingClass != null);
final PsiReferenceExpression methodExpression = methodCall.getMethodExpression();
final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
final String methodReferenceName = methodExpression.getReferenceName();
if (qualifierExpression != null) {
final PsiParameter[] parameters = lambdaExpression.getParameterList().getParameters();
boolean isReceiverType = parameters.length > 0 && LambdaUtil.isReceiverType(parameters[0].getType(), containingClass, PsiUtil.resolveGenericsClassInType(parameters[0].getType()).getSubstitutor());
methodRefText = (isReceiverType ? containingClass.getQualifiedName() : qualifierExpression.getText()) + "::" + methodReferenceName;
} else {
methodRefText = (psiMethod.hasModifierProperty(PsiModifier.STATIC) ? containingClass.getQualifiedName() : "this") + "::" + methodReferenceName;
}
} else if (element instanceof PsiNewExpression) {
final PsiMethod constructor = ((PsiNewExpression)element).resolveConstructor();
if (constructor != null) {
final PsiClass containingClass = constructor.getContainingClass();
LOG.assertTrue(containingClass != null);
methodRefText = containingClass.getQualifiedName() + "::new";
} else {
final PsiJavaCodeReferenceElement classReference = ((PsiNewExpression)element).getClassOrAnonymousClassReference();
if (classReference != null) {
final JavaResolveResult resolve = classReference.advancedResolve(false);
final PsiElement containingClass = resolve.getElement();
if (containingClass instanceof PsiClass) {
methodRefText = ((PsiClass)containingClass).getQualifiedName() + "::new";
}
}
}
}
if (methodRefText != null) {
final PsiExpression psiExpression = JavaPsiFacade.getElementFactory(project).createExpressionFromText(methodRefText, lambdaExpression);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(lambdaExpression.replace(psiExpression));
}
}
}
}

View File

@@ -55,7 +55,7 @@ class MyTest2<X> {
public static void main(String[] args) {
I<String> s = MyTest2<String>::new;
s.m("");
//todo test(MyTest2<String>::new, "");
test(MyTest2<String>::new, "");
}
}

View File

@@ -0,0 +1,13 @@
// "Replace lambda with method reference" "true"
class NonStaticInner {
class Inner {
Inner() {}
}
interface I1 {
Inner m(NonStaticInner rec);
}
static {
I1 i1 = NonStaticInner.Inner::new;
}
}

View File

@@ -0,0 +1,17 @@
// "Replace lambda with method reference" "true"
class StaticInner {
static class Inner {
Inner(StaticInner outer) {}
}
interface I1 {
Inner m(StaticInner rec);
}
static {
I1 i1 = StaticInner.Inner::new;
}
}

View File

@@ -0,0 +1,13 @@
// "Replace lambda with method reference" "true"
class MyTest2<X> {
MyTest2(X x) {
}
interface I<Z> {
MyTest2<Z> m(Z z);
}
public static void main(String[] args) {
I<String> s = MyTest2::new;
}
}

View File

@@ -0,0 +1,33 @@
// "Replace lambda with method reference" "true"
class NonStaticInner3 {
class Foo {
Foo(Integer i) {}
Foo() {}
}
interface I1<X> {
X m(int i);
}
interface I2<X> {
X m();
}
interface I3<X> {
X m(NonStaticInner3 rec, int i);
}
interface I4<X> {
X m(NonStaticInner3 rec);
}
{
I1<Foo> b1 = NonStaticInner3.Foo::new;
I2<Foo> b2 = () -> new Foo();
}
{
I3<Foo> b1 = (rec, i) -> rec.new Foo(i);
I4<Foo> b2 = (rec) -> rec.new Foo();
}
}

View File

@@ -0,0 +1,33 @@
// "Replace lambda with method reference" "true"
class NonStaticInner3 {
class Foo {
Foo(Integer i) {}
Foo() {}
}
interface I1<X> {
X m(int i);
}
interface I2<X> {
X m();
}
interface I3<X> {
X m(NonStaticInner3 rec, int i);
}
interface I4<X> {
X m(NonStaticInner3 rec);
}
{
I1<Foo> b1 = (i) -> new Foo(i);
I2<Foo> b2 = NonStaticInner3.Foo::new;
}
{
I3<Foo> b1 = (rec, i) -> rec.new Foo(i);
I4<Foo> b2 = (rec) -> rec.new Foo();
}
}

View File

@@ -0,0 +1,33 @@
// "Replace lambda with method reference" "true"
class NonStaticInner3 {
class Foo {
Foo(Integer i) {}
Foo() {}
}
interface I1<X> {
X m(int i);
}
interface I2<X> {
X m();
}
interface I3<X> {
X m(NonStaticInner3 rec, int i);
}
interface I4<X> {
X m(NonStaticInner3 rec);
}
{
I1<Foo> b1 = (i) -> new Foo(i);
I2<Foo> b2 = () -> new Foo();
}
{
I3<Foo> b1 = NonStaticInner3.Foo::new;
I4<Foo> b2 = (rec) -> rec.new Foo();
}
}

View File

@@ -0,0 +1,33 @@
// "Replace lambda with method reference" "true"
class NonStaticInner3 {
class Foo {
Foo(Integer i) {}
Foo() {}
}
interface I1<X> {
X m(int i);
}
interface I2<X> {
X m();
}
interface I3<X> {
X m(NonStaticInner3 rec, int i);
}
interface I4<X> {
X m(NonStaticInner3 rec);
}
{
I1<Foo> b1 = (i) -> new Foo(i);
I2<Foo> b2 = () -> new Foo();
}
{
I3<Foo> b1 = (rec, i) -> rec.new Foo(i);
I4<Foo> b2 = NonStaticInner3.Foo::new;
}
}

View File

@@ -0,0 +1,13 @@
// "Replace lambda with method reference" "true"
class NonStaticInner3 {
class Foo {
}
interface I1<X> {
X m();
}
{
I1<Foo> b2 = NonStaticInner3.Foo::new;
}
}

View File

@@ -0,0 +1,9 @@
// "Replace lambda with method reference" "true"
class Example {
public void m() {
}
{
Runnable r = this::m;
}
}

View File

@@ -0,0 +1,9 @@
// "Replace lambda with method reference" "true"
class Example {
public static void m() {
}
{
Runnable r = Example::m;
}
}

View File

@@ -0,0 +1,12 @@
// "Replace lambda with method reference" "true"
class Example {
interface I {
void foo(Example e);
}
void m() {}
{
I i = Example::m;
}
}

View File

@@ -0,0 +1,12 @@
// "Replace lambda with method reference" "true"
class Example {
interface I {
void foo(int i);
}
void m(int i) {}
{
I i = this::m;
}
}

View File

@@ -0,0 +1,13 @@
// "Replace lambda with method reference" "true"
class NonStaticInner {
class Inner {
Inner() {}
}
interface I1 {
Inner m(NonStaticInner rec);
}
static {
I1 i1 = (rec) -> r<caret>ec.new Inner();
}
}

View File

@@ -0,0 +1,17 @@
// "Replace lambda with method reference" "true"
class StaticInner {
static class Inner {
Inner(StaticInner outer) {}
}
interface I1 {
Inner m(StaticInner rec);
}
static {
I1 i1 = (rec) -> <caret>new Inner(rec);
}
}

View File

@@ -0,0 +1,13 @@
// "Replace lambda with method reference" "true"
class MyTest2<X> {
MyTest2(X x) {
}
interface I<Z> {
MyTest2<Z> m(Z z);
}
public static void main(String[] args) {
I<String> s = (z) -> new MyTe<caret>st2<String>(z);
}
}

View File

@@ -0,0 +1,33 @@
// "Replace lambda with method reference" "true"
class NonStaticInner3 {
class Foo {
Foo(Integer i) {}
Foo() {}
}
interface I1<X> {
X m(int i);
}
interface I2<X> {
X m();
}
interface I3<X> {
X m(NonStaticInner3 rec, int i);
}
interface I4<X> {
X m(NonStaticInner3 rec);
}
{
I1<Foo> b1 = (i) -> <caret>new Foo(i);
I2<Foo> b2 = () -> new Foo();
}
{
I3<Foo> b1 = (rec, i) -> rec.new Foo(i);
I4<Foo> b2 = (rec) -> rec.new Foo();
}
}

View File

@@ -0,0 +1,33 @@
// "Replace lambda with method reference" "true"
class NonStaticInner3 {
class Foo {
Foo(Integer i) {}
Foo() {}
}
interface I1<X> {
X m(int i);
}
interface I2<X> {
X m();
}
interface I3<X> {
X m(NonStaticInner3 rec, int i);
}
interface I4<X> {
X m(NonStaticInner3 rec);
}
{
I1<Foo> b1 = (i) -> new Foo(i);
I2<Foo> b2 = () -> <caret>new Foo();
}
{
I3<Foo> b1 = (rec, i) -> rec.new Foo(i);
I4<Foo> b2 = (rec) -> rec.new Foo();
}
}

View File

@@ -0,0 +1,33 @@
// "Replace lambda with method reference" "true"
class NonStaticInner3 {
class Foo {
Foo(Integer i) {}
Foo() {}
}
interface I1<X> {
X m(int i);
}
interface I2<X> {
X m();
}
interface I3<X> {
X m(NonStaticInner3 rec, int i);
}
interface I4<X> {
X m(NonStaticInner3 rec);
}
{
I1<Foo> b1 = (i) -> new Foo(i);
I2<Foo> b2 = () -> new Foo();
}
{
I3<Foo> b1 = (rec, i) -> <caret>rec.new Foo(i);
I4<Foo> b2 = (rec) -> rec.new Foo();
}
}

View File

@@ -0,0 +1,33 @@
// "Replace lambda with method reference" "true"
class NonStaticInner3 {
class Foo {
Foo(Integer i) {}
Foo() {}
}
interface I1<X> {
X m(int i);
}
interface I2<X> {
X m();
}
interface I3<X> {
X m(NonStaticInner3 rec, int i);
}
interface I4<X> {
X m(NonStaticInner3 rec);
}
{
I1<Foo> b1 = (i) -> new Foo(i);
I2<Foo> b2 = () -> new Foo();
}
{
I3<Foo> b1 = (rec, i) -> rec.new Foo(i);
I4<Foo> b2 = (rec) -> <caret>rec.new Foo();
}
}

View File

@@ -0,0 +1,33 @@
// "Replace lambda with method reference" "false"
class NonStaticInner3 {
class Foo {
Foo(Integer i) {}
Foo() {}
}
interface I1<X> {
X m(int i);
}
interface I2<X> {
X m();
}
interface I3<X> {
X m(NonStaticInner3 rec, int i);
}
interface I4<X> {
X m(NonStaticInner3 rec);
}
{
I1<Foo> b1 = (i) -> new Foo(i);
I2<Foo> b2 = () -> new Foo();
}
{
I3<Foo> b1 = (rec, i) -> rec.new Foo(i);
I4<Foo> b2 = (rec) -> <caret>new Foo();
}
}

View File

@@ -0,0 +1,13 @@
// "Replace lambda with method reference" "true"
class NonStaticInner3 {
class Foo {
}
interface I1<X> {
X m();
}
{
I1<Foo> b2 = () -> <caret>new Foo();
}
}

View File

@@ -0,0 +1,9 @@
// "Replace lambda with method reference" "true"
class Example {
public void m() {
}
{
Runnable r = () -> <caret>m();
}
}

View File

@@ -0,0 +1,9 @@
// "Replace lambda with method reference" "true"
class Example {
public static void m() {
}
{
Runnable r = () -> <caret>m();
}
}

View File

@@ -0,0 +1,12 @@
// "Replace lambda with method reference" "true"
class Example {
interface I {
void foo(Example e);
}
void m() {}
{
I i = (e) -> e<caret>.m();
}
}

View File

@@ -0,0 +1,12 @@
// "Replace lambda with method reference" "false"
class Example {
interface I {
void foo(Example e);
}
void m() {}
{
I i = (e) -> <caret>.m();
}
}

View File

@@ -0,0 +1,12 @@
// "Replace lambda with method reference" "true"
class Example {
interface I {
void foo(int i);
}
void m(int i) {}
{
I i = (i1) -> <caret>m(i1);
}
}

View File

@@ -0,0 +1,12 @@
// "Replace lambda with method reference" "false"
class Example {
interface I {
void foo(int i);
}
void m(int i) {}
{
I i = (i1) -> <caret>m(i1 + 1);
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2000-2012 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.LambdaCanBeMethReferenceInspection;
import com.intellij.codeInspection.LocalInspectionTool;
public class Lambda2MethodReferenceInspectionTest extends LightQuickFixTestCase {
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[]{
new LambdaCanBeMethReferenceInspection(),
};
}
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/lambda2methodReference";
}
}

View File

@@ -0,0 +1,7 @@
<html>
<body>
This inspection reports lambdas which can be replaced with method references
<p>
Lambda/method references syntax is not supported under Java 1.7 or earlier JVMs.
</body>
</html>

View File

@@ -540,6 +540,9 @@
<localInspection language="JAVA" shortName="Convert2Lambda" displayName="Anonymous type can be replaced with lambda"
groupName="Java language level migration aids" enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.codeInspection.AnonymousCanBeLambdaInspection" />
<localInspection language="JAVA" shortName="Convert2MethodRef" displayName="Lambda can be replaced with method reference"
groupName="Java language level migration aids" enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.codeInspection.LambdaCanBeMethReferenceInspection" />
<localInspection language="JAVA" shortName="CodeBlock2Expr" displayName="Lambda code block can be replaced with expression"
groupName="Java language level migration aids" enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.codeInspection.RedundantLambdaCodeBlockInspection" />