private static boolean localVariableAreEquivalent(
     @NotNull PsiLocalVariable var1, @NotNull PsiLocalVariable var2) {
   final PsiType type1 = var1.getType();
   final PsiType type2 = var2.getType();
   if (!typesAreEquivalent(type1, type2)) {
     return false;
   }
   final String name1 = var1.getName();
   final String name2 = var2.getName();
   if (!name1.equals(name2)) {
     return false;
   }
   final PsiExpression initializer1 = var1.getInitializer();
   final PsiExpression initializer2 = var2.getInitializer();
   return expressionsAreEquivalent(initializer1, initializer2);
 }
 private static void initialize(final PsiLocalVariable variable)
     throws IncorrectOperationException {
   PsiType type = variable.getType();
   String init = PsiTypesUtil.getDefaultValueOfType(type);
   PsiElementFactory factory =
       JavaPsiFacade.getInstance(variable.getProject()).getElementFactory();
   PsiExpression initializer = factory.createExpressionFromText(init, variable);
   variable.setInitializer(initializer);
 }
 static PsiType getInitializerType(
     PsiType forcedType, PsiExpression parameterInitializer, PsiLocalVariable localVariable) {
   final PsiType initializerType;
   if (forcedType == null) {
     if (parameterInitializer == null) {
       if (localVariable != null) {
         initializerType = localVariable.getType();
       } else {
         LOG.assertTrue(false);
         initializerType = null;
       }
     } else {
       if (localVariable == null) {
         initializerType =
             RefactoringUtil.getTypeByExpressionWithExpectedType(parameterInitializer);
       } else {
         initializerType = localVariable.getType();
       }
     }
   } else {
     initializerType = forcedType;
   }
   return initializerType;
 }
 private static PsiDeclarationStatement createNewDeclaration(
     @NotNull PsiLocalVariable variable, PsiExpression initializer)
     throws IncorrectOperationException {
   final PsiManager manager = variable.getManager();
   final PsiElementFactory factory =
       JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
   final PsiDeclarationStatement newDeclaration =
       factory.createVariableDeclarationStatement(
           variable.getName(), variable.getType(), initializer);
   if (variable.hasModifierProperty(PsiModifier.FINAL)) {
     final PsiLocalVariable newVariable =
         (PsiLocalVariable) newDeclaration.getDeclaredElements()[0];
     final PsiModifierList modifierList = newVariable.getModifierList();
     modifierList.setModifierProperty(PsiModifier.FINAL, true);
   }
   return newDeclaration;
 }
  private static PsiStatement createAssignment(
      PsiLocalVariable local, String fieldname, PsiElementFactory factory) {
    try {
      String pattern = fieldname + "=0;";
      PsiExpressionStatement statement =
          (PsiExpressionStatement) factory.createStatementFromText(pattern, null);
      statement =
          (PsiExpressionStatement)
              CodeStyleManager.getInstance(local.getProject()).reformat(statement);

      PsiAssignmentExpression expr = (PsiAssignmentExpression) statement.getExpression();
      final PsiExpression initializer =
          RefactoringUtil.convertInitializerToNormalExpression(
              local.getInitializer(), local.getType());
      expr.getRExpression().replace(initializer);

      return statement;
    } catch (IncorrectOperationException e) {
      LOG.error(e);
      return null;
    }
  }
  private static PsiField createField(
      PsiLocalVariable local, PsiType forcedType, String fieldName, boolean includeInitializer) {
    @NonNls StringBuilder pattern = new StringBuilder();
    pattern.append("var ");
    pattern.append(fieldName);

    PsiType type = local.getType();
    if (local.getInitializer() == null) {
      includeInitializer = false;
    } else {
      type = local.getInitializer().getType();
    }

    pattern.append(": ").append(type.getPresentableText());
    if (includeInitializer) {
      pattern.append(" = ").append(local.getInitializer().getText());
    }

    final PsiElement psiElement =
        GosuPsiParseUtil.parseProgramm(
            pattern.toString(), PsiManager.getInstance(local.getProject()), null);

    final GosuFieldImpl field = PsiTreeUtil.findChildOfType(psiElement, GosuFieldImpl.class);

    GeneratedMarkerVisitor.markGenerated(field);

    try {
      final PsiModifierList modifierList = local.getModifierList();
      if (modifierList != null) {
        for (PsiAnnotation annotation : modifierList.getAnnotations()) {
          field.getModifierList().add(annotation.copy());
        }
      }
      return field;
    } catch (IncorrectOperationException e) {
      LOG.error(e);
      return null;
    }
  }