extract light method object: insert result as last statement

This commit is contained in:
Anna Kozlova
2014-09-11 13:13:50 +04:00
parent 9159b7810c
commit bad93dbdc9
4 changed files with 104 additions and 7 deletions
@@ -28,6 +28,7 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.controlFlow.*;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.psi.util.PsiTreeUtil;
@@ -80,9 +81,10 @@ public class ExtractLightMethodObjectHandler {
@NotNull final PsiCodeFragment fragment,
final String methodName) throws PrepareFailedException {
PsiExpression expression = CodeInsightUtil.findExpressionInRange(fragment, 0, fragment.getTextLength());
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
final PsiElement[] elements;
if (expression != null) {
elements = new PsiElement[] {JavaPsiFacade.getElementFactory(project).createStatementFromText(expression.getText() + ";", expression)};
elements = new PsiElement[] {elementFactory.createStatementFromText(expression.getText() + ";", expression)};
} else {
elements = CodeInsightUtil.findStatementsInRange(fragment, 0, fragment.getTextLength());
}
@@ -90,6 +92,18 @@ public class ExtractLightMethodObjectHandler {
return null;
}
if (elements[elements.length - 1] instanceof PsiExpressionStatement) {
final PsiExpression expr = ((PsiExpressionStatement)elements[elements.length - 1]).getExpression();
if (!(expr instanceof PsiAssignmentExpression)) {
final PsiType expressionType = expr.getType();
if (expressionType != null && expressionType != PsiType.VOID) {
final String uniqueResultName = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName("result", elements[0], true);
final String statementText = expressionType.getCanonicalText() + " " + uniqueResultName + " = " + expr.getText() + ";";
elements[elements.length - 1] = elements[elements.length - 1].replace(elementFactory.createStatementFromText(statementText, elements[elements.length -1]));
}
}
}
final PsiFile copy = PsiFileFactory.getInstance(project)
.createFileFromText(file.getName(), file.getFileType(), file.getText(), file.getModificationStamp(), false);
@@ -138,7 +152,7 @@ public class ExtractLightMethodObjectHandler {
return "\"variable: \" + " + variable.getName();
}
}, " +");
PsiStatement outStatement = JavaPsiFacade.getElementFactory(project).createStatementFromText("System.out.println(" + outputVariables + ");", anchor);
PsiStatement outStatement = elementFactory.createStatementFromText("System.out.println(" + outputVariables + ");", anchor);
outStatement = (PsiStatement)container.addAfter(outStatement, elementsCopy[elementsCopy.length - 1]);
final ExtractMethodObjectProcessor extractMethodObjectProcessor = new ExtractMethodObjectProcessor(project, null, elementsCopy, "") {
@@ -0,0 +1,25 @@
/*
* Copyright 2000-2014 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.
*/
class Sample {
void a() {
System.out.println("<caret>");
}
int foo() {
return 1;
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2000-2014 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.
*/
class Sample {
void a() {
System.out.println("<caret>");
}
int foo() {
return 1;
}
}
@@ -99,11 +99,11 @@ public class ExtractMethodObject4DebuggerTest extends LightRefactoringTestCase {
}
public void testAnonymousClassParams() throws Exception {
doTest("new I() {public void foo(int i) {i++;}};", "new Test().invoke();",
doTest("new I() {public void foo(int i) {i++;}};", "I result = new Test().invoke();",
"public class Test {\n" +
" public void invoke() {\n" +
" new I() {\n" +
" public I invoke() {\n" +
" return new I() {\n" +
" public void foo(int i) {\n" +
" i++;\n" +
" }\n" +
@@ -113,11 +113,44 @@ public class ExtractMethodObject4DebuggerTest extends LightRefactoringTestCase {
}
public void testInnerClass() throws Exception {
doTest(" new In(2).foo()", "new Test().invoke();",
doTest(" new I(2).foo()", "new Test().invoke();",
"public class Test {\n" +
" public void invoke() {\n" +
" new In(2).foo();\n" +
" new Sample.I(2).foo();\n" +
" }\n" +
" }");
}
public void testResultExpr() throws Exception {
doTest(" foo()", "int result = new Test().invoke();",
"public class Test {\n" +
" public int invoke() {\n" +
" return foo();\n" +
" }\n" +
" }");
}
public void testResultStatements() throws Exception {
doTest("int i = 0;\nfoo()", "Test test = new Test().invoke();int i = test.getI();int result = test.getResult();",
"public class Test {\n" +
" private int i;\n" +
" private int result;\n" +
"\n" +
" public int getI() {\n" +
" return i;\n" +
" }\n" +
"\n" +
" public int getResult() {\n" +
" return result;\n" +
" }\n" +
"\n" +
" public Test invoke() {\n" +
" i = 0;\n" +
" result = foo();\n" +
" return this;\n" +
" }\n" +
" }");
}