PsiStatement (PsiMethod newMethod, boolean doExtract) { PsiCodeBlock body = newMethod.getBody(); if (myExpression != null) { declareNecessaryVariablesInsideBody(body); if (myHasExpressionOutput) { PsiReturnStatement returnStatement = (PsiReturnStatement)myElementFactory.createStatementFromText("return x;", null); final PsiExpression returnValue = CommonJavaRefactoringUtil.convertInitializerToNormalExpression(myExpression, myForcedReturnType); returnStatement.getReturnValue().replace(returnValue); body.add(returnStatement); } else { PsiExpressionStatement statement = (PsiExpressionStatement)myElementFactory.createStatementFromText("x;", null); statement.getExpression().replace(myExpression); body.add(statement); } return null; } final boolean hasNormalExit = hasNormalExit(); String outVariableName = myOutputVariable != null ? getNewVariableName(myOutputVariable) : null; PsiReturnStatement returnStatement; if (myNullConditionalCheck) { returnStatement = (PsiReturnStatement)myElementFactory.createStatementFromText("return null;", null); } else if (myOutputVariable != null) { returnStatement = (PsiReturnStatement)myElementFactory.createStatementFromText("return " + outVariableName + ";", null); } else if (myGenerateConditionalExit) { returnStatement = (PsiReturnStatement)myElementFactory.createStatementFromText("return true;", null); } else { returnStatement = (PsiReturnStatement)myElementFactory.createStatementFromText("return;", null); } PsiStatement exitStatementCopy = !doExtract || myNotNullConditionalCheck ? null : myControlFlowWrapper.getExitStatementCopy(returnStatement, myElements); declareNecessaryVariablesInsideBody(body); body.addRange(myElements[0], myElements[myElements.length - 1]); if (myNullConditionalCheck) { body.add(myElementFactory.createStatementFromText("return " + myOutputVariable.getName() + ";", null)); } else if (myNotNullConditionalCheck) { body.add(myElementFactory.createStatementFromText("return null;", null)); } else if (myGenerateConditionalExit) { body.add(myElementFactory.createStatementFromText("return false;", null)); } else if (!myHasReturnStatement && hasNormalExit && myOutputVariable != null) { final PsiReturnStatement insertedReturnStatement = (PsiReturnStatement)body.add(returnStatement); if (myOutputVariables.length == 1) { final PsiExpression returnValue = insertedReturnStatement.getReturnValue(); if (returnValue instanceof PsiReferenceExpression) { final PsiVariable variable = ObjectUtils.tryCast(((PsiReferenceExpression)returnValue).resolve(), PsiVariable.class); if (variable != null && Comparing.strEqual(variable.getName(), outVariableName)) { final PsiStatement statement = PsiTreeUtil.getPrevSiblingOfType(insertedReturnStatement, PsiStatement.class); if (statement instanceof PsiDeclarationStatement) { final PsiElement[] declaredElements = ((PsiDeclarationStatement)statement).getDeclaredElements(); if (ArrayUtil.find(declaredElements, variable) != -1) { InlineUtil.inlineVariable(variable, PsiUtil.skipParenthesizedExprDown(variable.getInitializer()), (PsiReferenceExpression)returnValue); variable.delete(); } } } } } } else if (isArtificialOutputUsed()) { body.add(myElementFactory.createStatementFromText("return " + myArtificialOutputVariable.getName() + ";", null)); } return exitStatementCopy; }