mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
InlineMethodProcessor: cover weird case with enum constant, lambda and explicit return with another call
This commit is contained in:
@@ -1446,17 +1446,14 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
PsiCall call = PsiTreeUtil.getParentOfType(ref, PsiCall.class);
|
||||
@NonNls String text = "new Object() { " + myMethod.getReturnTypeElement().getText() + " evaluate() { return " + call.getText() + ";}}.evaluate";
|
||||
PsiExpression callExpr = JavaPsiFacade.getInstance(myProject).getParserFacade().createExpressionFromText(text, call);
|
||||
PsiElement classExpr = ref.replace(callExpr);
|
||||
classExpr.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitReturnStatement(final PsiReturnStatement statement) {
|
||||
super.visitReturnStatement(statement);
|
||||
PsiExpression expr = statement.getReturnValue();
|
||||
if (expr instanceof PsiMethodCallExpression) {
|
||||
refsVector.add(((PsiMethodCallExpression) expr).getMethodExpression());
|
||||
}
|
||||
}
|
||||
});
|
||||
PsiReferenceExpression classExpr = (PsiReferenceExpression)ref.replace(callExpr);
|
||||
PsiNewExpression newObject = (PsiNewExpression)Objects.requireNonNull(classExpr.getQualifierExpression());
|
||||
PsiMethod evaluateMethod = Objects.requireNonNull(newObject.getAnonymousClass()).getMethods()[0];
|
||||
PsiExpression retVal = ((PsiReturnStatement)Objects.requireNonNull(evaluateMethod.getBody())
|
||||
.getStatements()[0]).getReturnValue();
|
||||
if (retVal instanceof PsiMethodCallExpression) {
|
||||
refsVector.add(((PsiMethodCallExpression) retVal).getMethodExpression());
|
||||
}
|
||||
if (classExpr.getParent() instanceof PsiMethodCallExpression) {
|
||||
PsiExpressionList args = ((PsiMethodCallExpression)classExpr.getParent()).getArgumentList();
|
||||
PsiExpression[] argExpressions = args.getExpressions();
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
class Test {
|
||||
enum Foo {
|
||||
BAR(<caret>getTwice(() -> {
|
||||
return getNum();
|
||||
}));
|
||||
|
||||
Foo(int val) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int getNum() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
static int getTwice(IntSupplier fn) {
|
||||
int x = fn.getAsInt();
|
||||
int y = fn.getAsInt();
|
||||
return x + y;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
class Test {
|
||||
enum Foo {
|
||||
BAR(new Object() {
|
||||
int evaluate() {
|
||||
IntSupplier fn = () -> {
|
||||
return getNum();
|
||||
};
|
||||
int x = fn.getAsInt();
|
||||
int y = fn.getAsInt();
|
||||
return x + y;
|
||||
}
|
||||
}.evaluate());
|
||||
|
||||
Foo(int val) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int getNum() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -144,6 +144,10 @@ public class InlineMethodTest extends LightRefactoringTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testEnumConstantConstructorParameterNestedLambda() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testEnumConstantConstructorWithArgs() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user