InlineMethodProcessor: cover weird case with enum constant, lambda and explicit return with another call

This commit is contained in:
Tagir Valeev
2019-03-11 17:11:20 +07:00
parent c652bbf2ea
commit 3f3f575d07
4 changed files with 56 additions and 11 deletions
@@ -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();
@@ -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;
}
}
@@ -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();
}