PsiLocalVariable ( Project project, PsiElement anchorStatement, PsiType type, String initialVariableName ) { PsiElement context = anchorStatement.getParent(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); String value = PsiTypesUtil.getDefaultValueOfType(type); PsiExpression initializer = factory.createExpressionFromText(value, anchorStatement); PsiDeclarationStatement declaration = factory.createVariableDeclarationStatement(initialVariableName, type, initializer, context); if (CommonJavaRefactoringUtil.isLoopOrIf(context)) { declaration = (PsiDeclarationStatement)CommonJavaRefactoringUtil.putStatementInLoopBody(declaration, context, anchorStatement); } else { declaration = (PsiDeclarationStatement)context.addBefore(declaration, anchorStatement); } return (PsiLocalVariable)declaration.getDeclaredElements()[0]; }