use of com.intellij.refactoring.inline.InlineMethodProcessor in project intellij-community by JetBrains.
the class InlineCallFix method inline.
protected void inline(Project project, PsiReferenceExpression methodExpression, PsiMethod method) {
final JavaRefactoringSettings settings = JavaRefactoringSettings.getInstance();
final InlineMethodProcessor processor = new InlineMethodProcessor(project, method, methodExpression, null, true, settings.RENAME_SEARCH_IN_COMMENTS_FOR_METHOD, settings.RENAME_SEARCH_FOR_TEXT_FOR_METHOD);
processor.inlineMethodCall(processor.addBracesWhenNeeded(new PsiReferenceExpression[] { methodExpression })[0]);
}
use of com.intellij.refactoring.inline.InlineMethodProcessor in project intellij-community by JetBrains.
the class JavaCodeInsightTestUtil method doInlineMethodTest.
public static void doInlineMethodTest(@NotNull final CodeInsightTestFixture fixture, @NotNull final String before, @NotNull final String after) {
fixture.configureByFile(before);
final Editor editor = fixture.getEditor();
final PsiElement element = TargetElementUtil.findTargetElement(editor, TARGET_FOR_INLINE_FLAGS);
assert element instanceof PsiMethod : element;
final PsiReference ref = fixture.getFile().findReferenceAt(editor.getCaretModel().getOffset());
final PsiReferenceExpression refExpr = ref instanceof PsiReferenceExpression ? (PsiReferenceExpression) ref : null;
final PsiMethod method = (PsiMethod) element;
assert !(InlineMethodProcessor.checkBadReturns(method) && !InlineUtil.allUsagesAreTailCalls(method)) : "Bad returns found";
new InlineMethodProcessor(getProject(), method, refExpr, editor, false).run();
fixture.checkResultByFile(after, false);
}
use of com.intellij.refactoring.inline.InlineMethodProcessor in project intellij-community by JetBrains.
the class InlineSuperCallUsageInfo method fixUsage.
@Override
public void fixUsage() throws IncorrectOperationException {
PsiElement element = getElement();
if (element != null && myConstrBody != null) {
assert !element.isPhysical();
final PsiStatement statement = JavaPsiFacade.getElementFactory(getProject()).createStatementFromText("super();", myConstrBody);
element = ((PsiExpressionStatement) myConstrBody.addBefore(statement, myConstrBody.getFirstBodyElement())).getExpression();
}
if (element instanceof PsiMethodCallExpression) {
PsiReferenceExpression methodExpression = ((PsiMethodCallExpression) element).getMethodExpression();
final PsiMethod superConstructor = (PsiMethod) methodExpression.resolve();
if (superConstructor != null) {
PsiMethod methodCopy = JavaPsiFacade.getElementFactory(getProject()).createMethod("toInline", PsiType.VOID);
final PsiCodeBlock constructorBody = superConstructor.getBody();
if (constructorBody != null) {
final PsiCodeBlock methodBody = methodCopy.getBody();
assert methodBody != null;
methodBody.replace(constructorBody);
methodCopy.getParameterList().replace(superConstructor.getParameterList());
methodCopy.getThrowsList().replace(superConstructor.getThrowsList());
methodExpression = (PsiReferenceExpression) methodExpression.replace(JavaPsiFacade.getElementFactory(getProject()).createExpressionFromText(methodCopy.getName(), methodExpression));
final PsiClass inliningClass = superConstructor.getContainingClass();
assert inliningClass != null;
methodCopy = (PsiMethod) inliningClass.add(methodCopy);
final InlineMethodProcessor inlineMethodProcessor = new InlineMethodProcessor(getProject(), methodCopy, methodExpression, null, true);
inlineMethodProcessor.inlineMethodCall(methodExpression);
methodCopy.delete();
}
}
}
}
use of com.intellij.refactoring.inline.InlineMethodProcessor in project intellij-community by JetBrains.
the class SequentialRefactoringTest method testFormattingAfterInlineExtractMethod.
public void testFormattingAfterInlineExtractMethod() throws IOException, PrepareFailedException {
String text = "public class BrokenAlignment {\n" + "\n" + " public Object test() {\n" + " if (System.currentTimeMillis() > 1) {\n" + " if (System.currentTimeMillis() > 2) {\n" + " getData();\n" + " }\n" + " }\n" + " return \"hey\";\n" + " }\n" + "\n" + " private void getData() {\n" + " String[] args = new String[]{};\n" + " String result = \"data: \";\n" + " int i = 0;\n" + " while (i < args.length) {\n" + " result += args[i];\n" + " if (i % 2 == 0) {\n" + " result += \", it's even!\";\n" + " } else {\n" + " System.out.println(\"It's odd :(\");\n" + " break;\n" + " }\n" + " }\n" + " int k = 1;\n" + " }\n" + "\n" + "}";
configureFromFileText("test.java", text);
// Perform inline.
final PsiClass clazz = ((PsiClassOwner) myFile).getClasses()[0];
final PsiMethod[] methods = clazz.findMethodsByName("getData", false);
final PsiReferenceExpression ref = (PsiReferenceExpression) myFile.findReferenceAt(text.indexOf("getData") + 1);
final InlineMethodProcessor processor = new InlineMethodProcessor(getProject(), methods[0], ref, myEditor, false);
processor.run();
// Perform extract.
final String currentText = myEditor.getDocument().getText();
int start = currentText.indexOf("String[] args");
int end = currentText.indexOf("\n", currentText.indexOf("int k"));
myEditor.getSelectionModel().setSelection(start, end);
ExtractMethodTest.performExtractMethod(true, true, myEditor, myFile, getProject());
checkResultByText(text.replace("getData", "newMethod"));
}
Aggregations