mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
introduce variable from lambda expression
This commit is contained in:
@@ -117,7 +117,8 @@ public class LambdaUtil {
|
||||
final int parameterIndex = ((PsiParameterList)paramParent).getParameterIndex(param);
|
||||
if (parameterIndex > -1) {
|
||||
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(param, PsiLambdaExpression.class);
|
||||
final PsiClassType.ClassResolveResult resolveResult = getFunctionInterfaceType(lambdaExpression);
|
||||
final PsiType type = getFunctionInterfaceType(lambdaExpression);
|
||||
final PsiClassType.ClassResolveResult resolveResult = type instanceof PsiClassType ? ((PsiClassType)type).resolveGenerics() : null;
|
||||
if (resolveResult != null) {
|
||||
final MethodSignature methodSignature = getFunction(resolveResult.getElement());
|
||||
if (methodSignature != null) {
|
||||
@@ -140,7 +141,7 @@ public class LambdaUtil {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiClassType.ClassResolveResult getFunctionInterfaceType(@Nullable PsiLambdaExpression lambdaExpression) {
|
||||
public static PsiType getFunctionInterfaceType(@Nullable PsiLambdaExpression lambdaExpression) {
|
||||
if (lambdaExpression != null) {
|
||||
final PsiElement parent = lambdaExpression.getParent();
|
||||
PsiType type = null;
|
||||
@@ -177,10 +178,7 @@ public class LambdaUtil {
|
||||
type = method.getReturnType();
|
||||
}
|
||||
}
|
||||
|
||||
if (type instanceof PsiClassType) {
|
||||
return ((PsiClassType)type).resolveGenerics();
|
||||
}
|
||||
return type;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class PsiLambdaExpressionType extends PsiType {
|
||||
|
||||
@Override
|
||||
public <A> A accept(@NotNull final PsiTypeVisitor<A> visitor) {
|
||||
return visitor.visitType(this);
|
||||
return visitor.visitLambdaExpressionType(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -67,4 +67,9 @@ public class PsiTypeVisitor<A> {
|
||||
public A visitDiamondType(PsiDiamondType diamondType) {
|
||||
return visitType(diamondType);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public A visitLambdaExpressionType(PsiLambdaExpressionType lambdaExpressionType) {
|
||||
return visitType(LambdaUtil.getFunctionInterfaceType(lambdaExpressionType.getExpression()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
interface SAM<X> {
|
||||
X m(int i, int j);
|
||||
}
|
||||
|
||||
class Foo {
|
||||
void test() {
|
||||
SAM<Integer> c = (i, j)->i + j;
|
||||
m(c);
|
||||
}
|
||||
void m(SAM<Integer> s) { }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
interface SAM<X> {
|
||||
X m(int i, int j);
|
||||
}
|
||||
|
||||
class Foo {
|
||||
void test() {
|
||||
m(<selection>(i, j) -> i + j</selection>);
|
||||
}
|
||||
void m(SAM<Integer> s) { }
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
interface SAM<X> {
|
||||
X m(int i, int j);
|
||||
}
|
||||
|
||||
class Foo {
|
||||
void test() {
|
||||
SAM<Integer> c = (i, j)->"" + i + j;
|
||||
SAM<Integer> s3 = m(c);
|
||||
}
|
||||
<X> SAM<X> m(SAM<X> s) { return null; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
interface SAM<X> {
|
||||
X m(int i, int j);
|
||||
}
|
||||
|
||||
class Foo {
|
||||
void test() {
|
||||
SAM<Integer> s3 = m(<selection>(i, j) -> "" + i + j</selection>);
|
||||
}
|
||||
<X> SAM<X> m(SAM<X> s) { return null; }
|
||||
}
|
||||
@@ -335,6 +335,14 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
|
||||
doTest(new MockIntroduceVariableHandler("c", false, false, false, CommonClassNames.JAVA_LANG_OBJECT));
|
||||
}
|
||||
|
||||
public void testLambdaExpr() throws Exception {
|
||||
doTest(new MockIntroduceVariableHandler("c", false, false, false, "SAM<java.lang.Integer>"));
|
||||
}
|
||||
|
||||
public void testLambdaExprNotAccepted() throws Exception {
|
||||
doTest(new MockIntroduceVariableHandler("c", false, false, false, "SAM<java.lang.Integer>"));
|
||||
}
|
||||
|
||||
private void doTest(IntroduceVariableBase testMe) throws Exception {
|
||||
@NonNls String baseName = "/refactoring/introduceVariable/" + getTestName(false);
|
||||
configureByFile(baseName + ".java");
|
||||
|
||||
Reference in New Issue
Block a user