diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiMethodReferenceExpressionImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiMethodReferenceExpressionImpl.java index 39152e9328b7..e649aba4a3f2 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiMethodReferenceExpressionImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiMethodReferenceExpressionImpl.java @@ -356,7 +356,7 @@ public class PsiMethodReferenceExpressionImpl extends PsiReferenceExpressionBase PsiSubstitutor substitutor, LanguageLevel languageLevel) { if (signature == null) return PsiSubstitutor.EMPTY; - final PsiType[] types = method.getSignature(substitutor).getParameterTypes(); + final PsiType[] types = method.getSignature(PsiUtil.isRawSubstitutor(method, substitutor) ? PsiSubstitutor.EMPTY : substitutor).getParameterTypes(); final PsiType[] rightTypes = signature.getParameterTypes(); if (types.length < rightTypes.length) { return PsiUtil.resolveGenericsClassInType(rightTypes[0]).getSubstitutor(); @@ -364,9 +364,12 @@ public class PsiMethodReferenceExpressionImpl extends PsiReferenceExpressionBase return PsiUtil.resolveGenericsClassInType(types[0]).getSubstitutor(); } - PsiSubstitutor psiSubstitutor = JavaPsiFacade.getInstance(getProject()).getResolveHelper().inferTypeArguments( - method.getTypeParameters(), types, rightTypes, languageLevel); + final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(getProject()).getResolveHelper(); + PsiSubstitutor psiSubstitutor = resolveHelper.inferTypeArguments(method.getTypeParameters(), types, rightTypes, languageLevel); psiSubstitutor = psiSubstitutor.putAll(substitutor); + if (method.isConstructor()) { + psiSubstitutor = psiSubstitutor.putAll(resolveHelper.inferTypeArguments(method.getContainingClass().getTypeParameters(), types, rightTypes, languageLevel)); + } return LambdaUtil.inferFromReturnType(method.getTypeParameters(), psiSubstitutor.substitute(method.getReturnType()), diff --git a/plugins/IntentionPowerPak/src/META-INF/plugin.xml b/plugins/IntentionPowerPak/src/META-INF/plugin.xml index 711c0e0bffe1..004627a131a7 100644 --- a/plugins/IntentionPowerPak/src/META-INF/plugin.xml +++ b/plugins/IntentionPowerPak/src/META-INF/plugin.xml @@ -214,6 +214,10 @@ com.siyeh.ipp.types.ReplaceLambdaWithAnonymousIntention intention.category.declaration + + com.siyeh.ipp.types.ReplaceMethodRefWithLambdaIntention + intention.category.declaration + com.siyeh.ipp.types.InferLambdaParameterTypeIntention intention.category.declaration diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index 5a1f7a2b66ab..d75127af8d3e 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -163,6 +163,8 @@ replace.diamond.with.explicit.type.arguments.intention.name=Replace '<>' with ex replace.diamond.with.explicit.type.arguments.intention.family.name=Replace Diamond with Explicit Type Arguments replace.lambda.with.anonymous.intention.name=Replace lambda with anonymous class replace.lambda.with.anonymous.intention.family.name=Replace lambda with anonymous class +replace.method.ref.with.lambda.intention.name=Replace method reference with lambda +replace.method.ref.with.lambda.intention.family.name=Replace method reference with lambda infer.lambda.parameter.type.intention.family.name=Infer lambda parameter type expand.one.line.lambda2.code.block.intention.family.name=Expand lambda expression body to code block make.public.intention.name=Make 'public' diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/types/ReplaceMethodRefWithLambdaIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/types/ReplaceMethodRefWithLambdaIntention.java new file mode 100644 index 000000000000..b07498f95882 --- /dev/null +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/types/ReplaceMethodRefWithLambdaIntention.java @@ -0,0 +1,178 @@ +/* + * Copyright 2011 Bas Leijdekkers + * + * 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.siyeh.ipp.types; + +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.*; +import com.intellij.psi.codeStyle.JavaCodeStyleManager; +import com.intellij.psi.codeStyle.VariableKind; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.psi.util.PsiUtil; +import com.intellij.psi.util.RedundantCastUtil; +import com.intellij.util.Function; +import com.intellij.util.IncorrectOperationException; +import com.siyeh.ipp.base.Intention; +import com.siyeh.ipp.base.PsiElementPredicate; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; + +public class ReplaceMethodRefWithLambdaIntention extends Intention { + private static final Logger LOG = Logger.getInstance("#" + ReplaceMethodRefWithLambdaIntention.class.getName()); + + @NotNull + @Override + protected PsiElementPredicate getElementPredicate() { + return new MethodRefPredicate(); + } + + @Override + protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { + final PsiMethodReferenceExpression referenceExpression = PsiTreeUtil.getParentOfType(element, PsiMethodReferenceExpression.class); + LOG.assertTrue(referenceExpression != null); + final PsiType functionalInterfaceType = referenceExpression.getFunctionalInterfaceType(); + final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType); + final StringBuilder buf = new StringBuilder("("); + LOG.assertTrue(functionalInterfaceType != null); + buf.append(functionalInterfaceType.getCanonicalText()).append(")("); + LOG.assertTrue(interfaceMethod != null); + final PsiParameter[] parameters = interfaceMethod.getParameterList().getParameters(); + + final Map map = new HashMap(); + final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(element.getProject()); + final String paramsString = StringUtil.join(parameters, new Function() { + @Override + public String fun(PsiParameter parameter) { + String parameterName = parameter.getName(); + if (parameterName != null) { + final String baseName = codeStyleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER); + parameterName = codeStyleManager.suggestUniqueVariableName(baseName, referenceExpression, true); + map.put(parameter, parameterName); + return parameterName; + } + return ""; + } + }, ", "); + buf.append(paramsString); + buf.append(") -> "); + + + final JavaResolveResult resolveResult = referenceExpression.advancedResolve(false); + final PsiElement resolveElement = resolveResult.getElement(); + if (resolveElement instanceof PsiMember) { + boolean needBraces = interfaceMethod.getReturnType() == PsiType.VOID && !(resolveElement instanceof PsiMethod && ((PsiMethod)resolveElement).getReturnType() == PsiType.VOID); + + if (needBraces) { + buf.append("{"); + } + final PsiElement qualifier = referenceExpression.getQualifier(); + boolean isReceiver = false; + if (resolveElement instanceof PsiMethod) { + final PsiClass containingClass = ((PsiMember)resolveElement).getContainingClass(); + LOG.assertTrue(containingClass != null); + isReceiver = parameters.length > 0 && + LambdaUtil.isReceiverType(parameters[0].getType(), containingClass, PsiUtil.resolveGenericsClassInType(parameters[0].getType()).getSubstitutor()); + if (isReceiver && ((PsiMethod)resolveElement).getParameterList().getParametersCount() != parameters.length - 1) { + isReceiver = false; + } + } + + final PsiElement referenceNameElement = referenceExpression.getReferenceNameElement(); + if (isReceiver){ + buf.append(parameters[0].getName()).append("."); + } else if (qualifier != null && + !(qualifier instanceof PsiThisExpression && ((PsiThisExpression)qualifier).getQualifier() == null) && + !(referenceNameElement instanceof PsiKeyword)){ + buf.append(qualifier.getText()).append("."); + } + + //new or method name + buf.append(referenceExpression.getReferenceName()); + + if (referenceNameElement instanceof PsiKeyword) { + //class name + buf.append(" "); + buf.append(((PsiMember)resolveElement).getName()); + + final PsiSubstitutor substitutor = resolveResult.getSubstitutor(); + + PsiClass containingClass; + if (resolveElement instanceof PsiClass) { + containingClass = (PsiClass)resolveElement; + } else { + containingClass = ((PsiMember)resolveElement).getContainingClass(); + } + + LOG.assertTrue(containingClass != null); + if (containingClass.hasTypeParameters() && !PsiUtil.isRawSubstitutor(containingClass, substitutor)) { + buf.append("<").append(StringUtil.join(containingClass.getTypeParameters(), new Function() { + @Override + public String fun(PsiTypeParameter parameter) { + final PsiType psiType = substitutor.substitute(parameter); + LOG.assertTrue(psiType != null); + return psiType.getCanonicalText(); + } + }, ", ")).append(">"); + } + } + + //param list + buf.append("("); + boolean first = true; + for (int i = isReceiver ? 1 : 0; i < parameters.length; i++) { + PsiParameter parameter = parameters[i]; + if (!first) { + buf.append(", "); + } else { + first = false; + } + buf.append(map.get(parameter)); + } + buf.append(")"); + + if (needBraces) { + buf.append(";}"); + } + } + + + final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)referenceExpression + .replace(JavaPsiFacade.getElementFactory(element.getProject()).createExpressionFromText(buf.toString(), referenceExpression)); + if (RedundantCastUtil.isCastRedundant(typeCastExpression)) { + final PsiExpression operand = typeCastExpression.getOperand(); + LOG.assertTrue(operand != null); + typeCastExpression.replace(operand); + } + } + + private static class MethodRefPredicate implements PsiElementPredicate { + @Override + public boolean satisfiedBy(PsiElement element) { + final PsiMethodReferenceExpression methodReferenceExpression = PsiTreeUtil.getParentOfType(element, PsiMethodReferenceExpression.class); + if (methodReferenceExpression != null) { + final PsiType interfaceType = methodReferenceExpression.getFunctionalInterfaceType(); + if (interfaceType != null && + LambdaUtil.getFunctionalInterfaceMethod(interfaceType) != null && + methodReferenceExpression.resolve() != null) { + return true; + } + } + return false; + } + } +} diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceMethodRefWithLambdaIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceMethodRefWithLambdaIntention/after.java.template new file mode 100644 index 000000000000..670ce55da81d --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceMethodRefWithLambdaIntention/after.java.template @@ -0,0 +1,10 @@ +class X { + interface I { + void _(); + } + + static void m() {} + { + I i = () -> m(); + } +} diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceMethodRefWithLambdaIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceMethodRefWithLambdaIntention/before.java.template new file mode 100644 index 000000000000..2633e25f6954 --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceMethodRefWithLambdaIntention/before.java.template @@ -0,0 +1,10 @@ +class X { + interface I { + void _(); + } + + static void m() {} + { + I i = X :: m; + } +} diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceMethodRefWithLambdaIntention/description.html b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceMethodRefWithLambdaIntention/description.html new file mode 100644 index 000000000000..977d2eb22cef --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceMethodRefWithLambdaIntention/description.html @@ -0,0 +1,5 @@ + + +This intention expands method reference to lambda expression + + diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Ambiguity.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Ambiguity.java new file mode 100644 index 000000000000..f4b0d1c1772c --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Ambiguity.java @@ -0,0 +1,19 @@ +public class MyTest { + + static void m(Integer i) { assertTrue(true); } + + interface I1 { + void m(int x); + } + + interface I2 { + void m(Integer x); + } + + static void call(int i, I1 s) {} + static void call(int i, I2 s) {} + + public static void main(String[] args) { + call(1, MyTest::m); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Ambiguity_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Ambiguity_after.java new file mode 100644 index 000000000000..cadb53a71c48 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Ambiguity_after.java @@ -0,0 +1,19 @@ +public class MyTest { + + static void m(Integer i) { assertTrue(true); } + + interface I1 { + void m(int x); + } + + interface I2 { + void m(Integer x); + } + + static void call(int i, I1 s) {} + static void call(int i, I2 s) {} + + public static void main(String[] args) { + call(1, (I2) (x) -> MyTest.m(x)); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ExprRefs.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ExprRefs.java new file mode 100644 index 000000000000..a87b35b99041 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ExprRefs.java @@ -0,0 +1,7 @@ +import java.util.*; + +public class MyTest { + static { + Arrays.sort( new String[0], String.CASE_INSENSITIVE_ORDER::compare ); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ExprRefs_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ExprRefs_after.java new file mode 100644 index 000000000000..05a84606b792 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ExprRefs_after.java @@ -0,0 +1,7 @@ +import java.util.*; + +public class MyTest { + static { + Arrays.sort( new String[0], (o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1, o2)); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefs.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefs.java new file mode 100644 index 000000000000..517df1e0e1bf --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefs.java @@ -0,0 +1,16 @@ +public class MyTest { + + MyTest() {} + + interface I { + MyTest m(); + } + + static void test(I i) { + i.m(); + } + + static { + I i = MyTest::new; + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsDefaultConstructor.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsDefaultConstructor.java new file mode 100644 index 000000000000..979212eb0a4a --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsDefaultConstructor.java @@ -0,0 +1,14 @@ +public class MyTest { + + interface I { + MyTest m(); + } + + static void test(I i) { + i.m(); + } + + static { + I i = MyTest::new; + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsDefaultConstructor_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsDefaultConstructor_after.java new file mode 100644 index 000000000000..f486638e6e9a --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsDefaultConstructor_after.java @@ -0,0 +1,14 @@ +public class MyTest { + + interface I { + MyTest m(); + } + + static void test(I i) { + i.m(); + } + + static { + I i = () -> new MyTest(); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference.java new file mode 100644 index 000000000000..79e049da255e --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference.java @@ -0,0 +1,16 @@ +public class MyTest { + + MyTest(X x) {} + + interface I { + MyTest m(Z z); + } + + static void test(I s, Y arg) { + s.m(arg); + } + + static { + I s = MyTest::new; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference1.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference1.java new file mode 100644 index 000000000000..8bef66441d0a --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference1.java @@ -0,0 +1,15 @@ +public class MyTest { + interface I { + void m(Integer s); + } + static class Foo { + Foo(X x) { } + } + + + static void m(I s) {} + + static { + m(Foo::new); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference1_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference1_after.java new file mode 100644 index 000000000000..b61947e47488 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference1_after.java @@ -0,0 +1,17 @@ +public class MyTest { + interface I { + void m(Integer s); + } + static class Foo { + Foo(X x) { } + } + + + static void m(I s) {} + + static { + m((s) -> { + new Foo(s); + }); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference_after.java new file mode 100644 index 000000000000..22e5bef35348 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInference_after.java @@ -0,0 +1,16 @@ +public class MyTest { + + MyTest(X x) {} + + interface I { + MyTest m(Z z); + } + + static void test(I s, Y arg) { + s.m(arg); + } + + static { + I s = (z) -> new MyTest(z); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInnerClass.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInnerClass.java new file mode 100644 index 000000000000..3a28070d1db5 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInnerClass.java @@ -0,0 +1,13 @@ +class MyTest { + class Inner { + Inner() {}; + } + + interface I { + Inner m(MyTest receiver); + } + + static { + I i1 = MyTest.Inner::new; + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInnerClass_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInnerClass_after.java new file mode 100644 index 000000000000..661cf589b482 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsInnerClass_after.java @@ -0,0 +1,13 @@ +class MyTest { + class Inner { + Inner() {}; + } + + interface I { + Inner m(MyTest receiver); + } + + static { + I i1 = (receiver) -> receiver.new Inner(); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsStaticInnerClass.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsStaticInnerClass.java new file mode 100644 index 000000000000..26685f1a6bcf --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsStaticInnerClass.java @@ -0,0 +1,15 @@ +class MyTest { + static class Inner { + Inner(MyTest outer) {}; + Inner() {}; + } + + interface I { + Inner m(MyTest receiver); + } + + + static { + I i1 = MyTest.Inner::new; + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsStaticInnerClass_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsStaticInnerClass_after.java new file mode 100644 index 000000000000..fdce48211ed4 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefsStaticInnerClass_after.java @@ -0,0 +1,15 @@ +class MyTest { + static class Inner { + Inner(MyTest outer) {}; + Inner() {}; + } + + interface I { + Inner m(MyTest receiver); + } + + + static { + I i1 = (receiver) -> new Inner(receiver); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefs_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefs_after.java new file mode 100644 index 000000000000..75ee4c010c89 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/NewRefs_after.java @@ -0,0 +1,16 @@ +public class MyTest { + + MyTest() {} + + interface I { + MyTest m(); + } + + static void test(I i) { + i.m(); + } + + static { + I i = () -> new MyTest(); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Receiver.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Receiver.java new file mode 100644 index 000000000000..2779edf75780 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Receiver.java @@ -0,0 +1,16 @@ +public class MyTest { + interface I { + void m(MyTest receiver); + } + + void m() { } + + { + I i = (I) MyTest::m; + s.m(this); + } + + static { + I i = (I)MyTest::m; + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Receiver_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Receiver_after.java new file mode 100644 index 000000000000..9f5bcafa929d --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/Receiver_after.java @@ -0,0 +1,16 @@ +public class MyTest { + interface I { + void m(MyTest receiver); + } + + void m() { } + + { + I i = (I) MyTest::m; + s.m(this); + } + + static { + I i = (I) (receiver) -> receiver.m(); + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/StaticMethodRef.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/StaticMethodRef.java new file mode 100644 index 000000000000..7c82ded8a473 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/StaticMethodRef.java @@ -0,0 +1,12 @@ +public class MyTest { + + interface I { void foo(int i); } + + static void print(int i) { + System.out.println(i); + } + + static { + I sam = MyTest::print; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/StaticMethodRef_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/StaticMethodRef_after.java new file mode 100644 index 000000000000..e88888254742 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/StaticMethodRef_after.java @@ -0,0 +1,12 @@ +public class MyTest { + + interface I { void foo(int i); } + + static void print(int i) { + System.out.println(i); + } + + static { + I sam = (i) -> MyTest.print(i); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/SuperRefs.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/SuperRefs.java new file mode 100644 index 000000000000..3a570463e260 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/SuperRefs.java @@ -0,0 +1,16 @@ +public class MyTest { + + interface I { + void meth(int i); + } + + static class A { + void m(int i) {} + } + + static class B extends A { + void m(int i1) { + I i = super::m; + } + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/SuperRefs_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/SuperRefs_after.java new file mode 100644 index 000000000000..f7c7004f070f --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/SuperRefs_after.java @@ -0,0 +1,16 @@ +public class MyTest { + + interface I { + void meth(int i); + } + + static class A { + void m(int i) {} + } + + static class B extends A { + void m(int i1) { + I i = (i2) -> super.m(i2); + } + } +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ThisRefs.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ThisRefs.java new file mode 100644 index 000000000000..f45e99a5b093 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ThisRefs.java @@ -0,0 +1,10 @@ +class MyTest { + static interface SAM { + void m(Integer i); + } + + void m(Integer i) {} + void m(Double d) {} + + SAM s = this::m; +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ThisRefs_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ThisRefs_after.java new file mode 100644 index 000000000000..9294bea19652 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/types/methodRefs2lambda/ThisRefs_after.java @@ -0,0 +1,10 @@ +class MyTest { + static interface SAM { + void m(Integer i); + } + + void m(Integer i) {} + void m(Double d) {} + + SAM s = (i) -> m(i); +} diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/types/ReplaceMethodReferenceWithLambdaIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/types/ReplaceMethodReferenceWithLambdaIntentionTest.java new file mode 100644 index 000000000000..47d40851ef0f --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/types/ReplaceMethodReferenceWithLambdaIntentionTest.java @@ -0,0 +1,80 @@ +/* + * 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.siyeh.ipp.types; + +import com.siyeh.IntentionPowerPackBundle; +import com.siyeh.ipp.IPPTestCase; + +public class ReplaceMethodReferenceWithLambdaIntentionTest extends IPPTestCase { + + @Override + protected String getIntentionName() { + return IntentionPowerPackBundle.message("replace.method.ref.with.lambda.intention.name"); + } + + @Override + protected String getRelativePath() { + return "types/methodRefs2lambda"; + } + + public void testStaticMethodRef() throws Exception { + doTest(); + } + + public void testThisRefs() throws Exception { + doTest(); + } + + public void testSuperRefs() throws Exception { + doTest(); + } + + public void testExprRefs() throws Exception { + doTest(); + } + + public void testReceiver() throws Exception { + doTest(); + } + + public void testNewRefs() throws Exception { + doTest(); + } + + public void testNewRefsDefaultConstructor() throws Exception { + doTest(); + } + + public void testNewRefsInnerClass() throws Exception { + doTest(); + } + + public void testNewRefsStaticInnerClass() throws Exception { + doTest(); + } + + public void testNewRefsInference() throws Exception { + doTest(); + } + + public void testNewRefsInference1() throws Exception { + doTest(); + } + + public void testAmbiguity() throws Exception { + doTest(); + } +}