private void createConstantDeclaration() throws CoreException {
    Type type = getConstantType();

    IExpressionFragment fragment = getSelectedExpression();
    Expression initializer =
        getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);

    AST ast = fCuRewrite.getAST();
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
    variableDeclarationFragment.setInitializer(initializer);

    FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
    fieldDeclaration.setType(type);
    Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility);
    if (accessModifier != null) fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
    fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
    fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));

    boolean createComments =
        JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaProject()).createComments;
    if (createComments) {
      String comment =
          CodeGeneration.getFieldComment(
              fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
      if (comment != null && comment.length() > 0) {
        Javadoc doc =
            (Javadoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
        fieldDeclaration.setJavadoc(doc);
      }
    }

    AbstractTypeDeclaration parent = getContainingTypeDeclarationNode();
    ListRewrite listRewrite =
        fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
    TextEditGroup msg =
        fCuRewrite.createGroupDescription(
            RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
    if (insertFirst()) {
      listRewrite.insertFirst(fieldDeclaration, msg);
    } else {
      listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
    }

    if (fLinkedProposalModel != null) {
      ASTRewrite rewrite = fCuRewrite.getASTRewrite();
      LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
      nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);

      String[] nameSuggestions = guessConstantNames();
      if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
        nameGroup.addProposal(fConstantName, null, nameSuggestions.length + 1);
      }
      for (int i = 0; i < nameSuggestions.length; i++) {
        nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i);
      }

      LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
      typeGroup.addPosition(rewrite.track(type), true);

      ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
      if (typeBinding != null) {
        ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
        for (int i = 0; i < relaxingTypes.length; i++) {
          typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
        }
      }
      boolean isInterface =
          parent.resolveBinding() != null && parent.resolveBinding().isInterface();
      ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(
          fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), isInterface);
    }
  }
  private ASTRewrite doAddField(CompilationUnit astRoot) {
    SimpleName node = fOriginalNode;
    boolean isInDifferentCU = false;

    ASTNode newTypeDecl = astRoot.findDeclaringNode(fSenderBinding);
    if (newTypeDecl == null) {
      astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
      newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
      isInDifferentCU = true;
    }
    ImportRewrite imports = createImportRewrite(astRoot);
    ImportRewriteContext importRewriteContext =
        new ContextSensitiveImportRewriteContext(
            ASTResolving.findParentBodyDeclaration(node), imports);

    if (newTypeDecl != null) {
      AST ast = newTypeDecl.getAST();

      ASTRewrite rewrite = ASTRewrite.create(ast);

      VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
      fragment.setName(ast.newSimpleName(node.getIdentifier()));

      Type type = evaluateVariableType(ast, imports, importRewriteContext, fSenderBinding);

      FieldDeclaration newDecl = ast.newFieldDeclaration(fragment);
      newDecl.setType(type);
      newDecl
          .modifiers()
          .addAll(ASTNodeFactory.newModifiers(ast, evaluateFieldModifiers(newTypeDecl)));

      if (fSenderBinding.isInterface() || fVariableKind == CONST_FIELD) {
        fragment.setInitializer(ASTNodeFactory.newDefaultExpression(ast, type, 0));
      }

      ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
      List<BodyDeclaration> decls =
          ASTNodes.<BodyDeclaration>getChildListProperty(newTypeDecl, property);

      int maxOffset = isInDifferentCU ? -1 : node.getStartPosition();

      int insertIndex = findFieldInsertIndex(decls, newDecl, maxOffset);

      ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property);
      listRewriter.insertAt(newDecl, insertIndex, null);

      ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(
          getLinkedProposalModel(), rewrite, newDecl.modifiers(), fSenderBinding.isInterface());

      addLinkedPosition(rewrite.track(newDecl.getType()), false, KEY_TYPE);
      if (!isInDifferentCU) {
        addLinkedPosition(rewrite.track(node), true, KEY_NAME);
      }
      addLinkedPosition(rewrite.track(fragment.getName()), false, KEY_NAME);

      if (fragment.getInitializer() != null) {
        addLinkedPosition(rewrite.track(fragment.getInitializer()), false, KEY_INITIALIZER);
      }
      return rewrite;
    }
    return null;
  }
 private void process(
     IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals)
     throws CoreException {
   int id = problem.getProblemId();
   if (id == 0) { // no proposals for none-problem locations
     return;
   }
   switch (id) {
     case IProblem.UnterminatedString:
       String quoteLabel = CorrectionMessages.JavaCorrectionProcessor_addquote_description;
       int pos =
           moveBack(
               problem.getOffset() + problem.getLength(),
               problem.getOffset(),
               "\n\r",
               context.getCompilationUnit()); // $NON-NLS-1$
       proposals.add(
           new ReplaceCorrectionProposal(
               quoteLabel, context.getCompilationUnit(), pos, 0, "\"", 0)); // $NON-NLS-1$
       break;
     case IProblem.UnusedImport:
     case IProblem.DuplicateImport:
     case IProblem.CannotImportPackage:
     case IProblem.ConflictingImport:
       ReorgCorrectionsSubProcessor.removeImportStatementProposals(context, problem, proposals);
       break;
     case IProblem.ImportNotFound:
       ReorgCorrectionsSubProcessor.importNotFoundProposals(context, problem, proposals);
       ReorgCorrectionsSubProcessor.removeImportStatementProposals(context, problem, proposals);
       break;
     case IProblem.UndefinedMethod:
       UnresolvedElementsSubProcessor.getMethodProposals(context, problem, false, proposals);
       break;
     case IProblem.UndefinedConstructor:
       UnresolvedElementsSubProcessor.getConstructorProposals(context, problem, proposals);
       break;
     case IProblem.UndefinedAnnotationMember:
       UnresolvedElementsSubProcessor.getAnnotationMemberProposals(context, problem, proposals);
       break;
     case IProblem.ParameterMismatch:
       UnresolvedElementsSubProcessor.getMethodProposals(context, problem, true, proposals);
       break;
     case IProblem.MethodButWithConstructorName:
       ReturnTypeSubProcessor.addMethodWithConstrNameProposals(context, problem, proposals);
       break;
     case IProblem.UndefinedField:
     case IProblem.UndefinedName:
       UnresolvedElementsSubProcessor.getVariableProposals(context, problem, null, proposals);
       break;
     case IProblem.AmbiguousType:
     case IProblem.JavadocAmbiguousType:
       UnresolvedElementsSubProcessor.getAmbiguosTypeReferenceProposals(
           context, problem, proposals);
       break;
     case IProblem.PublicClassMustMatchFileName:
       ReorgCorrectionsSubProcessor.getWrongTypeNameProposals(context, problem, proposals);
       break;
     case IProblem.PackageIsNotExpectedPackage:
       ReorgCorrectionsSubProcessor.getWrongPackageDeclNameProposals(context, problem, proposals);
       break;
     case IProblem.UndefinedType:
     case IProblem.JavadocUndefinedType:
       UnresolvedElementsSubProcessor.getTypeProposals(context, problem, proposals);
       break;
     case IProblem.TypeMismatch:
       // AspectJ Change
       if (problem.getProblemArguments() != null) { // Bug 265052
         TypeMismatchSubProcessor.addTypeMismatchProposals(context, problem, proposals);
       }
       break;
     case IProblem.IncompatibleReturnType:
       TypeMismatchSubProcessor.addIncompatibleReturnTypeProposals(context, problem, proposals);
       break;
     case IProblem.IncompatibleExceptionInThrowsClause:
       TypeMismatchSubProcessor.addIncompatibleThrowsProposals(context, problem, proposals);
       break;
     case IProblem.UnhandledException:
       LocalCorrectionsSubProcessor.addUncaughtExceptionProposals(context, problem, proposals);
       break;
     case IProblem.UnreachableCatch:
     case IProblem.InvalidCatchBlockSequence:
       LocalCorrectionsSubProcessor.addUnreachableCatchProposals(context, problem, proposals);
       break;
     case IProblem.VoidMethodReturnsValue:
       ReturnTypeSubProcessor.addVoidMethodReturnsProposals(context, problem, proposals);
       break;
     case IProblem.MethodReturnsVoid:
       ReturnTypeSubProcessor.addMethodRetunsVoidProposals(context, problem, proposals);
       break;
     case IProblem.MissingReturnType:
       ReturnTypeSubProcessor.addMissingReturnTypeProposals(context, problem, proposals);
       break;
     case IProblem.ShouldReturnValue:
       ReturnTypeSubProcessor.addMissingReturnStatementProposals(context, problem, proposals);
       break;
     case IProblem.NonExternalizedStringLiteral:
       LocalCorrectionsSubProcessor.addNLSProposals(context, problem, proposals);
       break;
     case IProblem.UnnecessaryNLSTag:
       LocalCorrectionsSubProcessor.getUnnecessaryNLSTagProposals(context, problem, proposals);
       break;
     case IProblem.NonStaticAccessToStaticField:
     case IProblem.NonStaticAccessToStaticMethod:
     case IProblem.IndirectAccessToStaticField:
     case IProblem.IndirectAccessToStaticMethod:
       LocalCorrectionsSubProcessor.addCorrectAccessToStaticProposals(context, problem, proposals);
       break;
     case IProblem.StaticMethodRequested:
     case IProblem.NonStaticFieldFromStaticInvocation:
     case IProblem.InstanceMethodDuringConstructorInvocation:
     case IProblem.InstanceFieldDuringConstructorInvocation:
       ModifierCorrectionSubProcessor.addNonAccessibleReferenceProposal(
           context, problem, proposals, ModifierCorrectionSubProcessor.TO_STATIC, 5);
       break;
     case IProblem.NonBlankFinalLocalAssignment:
     case IProblem.DuplicateFinalLocalInitialization:
     case IProblem.FinalFieldAssignment:
     case IProblem.DuplicateBlankFinalFieldInitialization:
     case IProblem.AnonymousClassCannotExtendFinalClass:
     case IProblem.ClassExtendFinalClass:
       ModifierCorrectionSubProcessor.addNonAccessibleReferenceProposal(
           context, problem, proposals, ModifierCorrectionSubProcessor.TO_NON_FINAL, 9);
       break;
     case IProblem.InheritedMethodReducesVisibility:
     case IProblem.MethodReducesVisibility:
     case IProblem.OverridingNonVisibleMethod:
       ModifierCorrectionSubProcessor.addChangeOverriddenModifierProposal(
           context, problem, proposals, ModifierCorrectionSubProcessor.TO_VISIBLE);
       break;
     case IProblem.FinalMethodCannotBeOverridden:
       ModifierCorrectionSubProcessor.addChangeOverriddenModifierProposal(
           context, problem, proposals, ModifierCorrectionSubProcessor.TO_NON_FINAL);
       break;
     case IProblem.CannotOverrideAStaticMethodWithAnInstanceMethod:
       ModifierCorrectionSubProcessor.addChangeOverriddenModifierProposal(
           context, problem, proposals, ModifierCorrectionSubProcessor.TO_NON_STATIC);
       break;
     case IProblem.CannotHideAnInstanceMethodWithAStaticMethod:
     case IProblem.IllegalModifierForInterfaceMethod:
     case IProblem.IllegalModifierForInterface:
     case IProblem.IllegalModifierForClass:
     case IProblem.IllegalModifierForInterfaceField:
     case IProblem.IllegalModifierForMemberInterface:
     case IProblem.IllegalModifierForMemberClass:
     case IProblem.IllegalModifierForLocalClass:
     case IProblem.IllegalModifierForArgument:
     case IProblem.IllegalModifierForField:
     case IProblem.IllegalModifierForMethod:
     case IProblem.IllegalModifierForVariable:
     case IProblem.IllegalVisibilityModifierForInterfaceMemberType:
     case IProblem.UnexpectedStaticModifierForMethod:
       ModifierCorrectionSubProcessor.addRemoveInvalidModifiersProposal(
           context, problem, proposals, 5);
       break;
     case IProblem.NotVisibleMethod:
     case IProblem.NotVisibleConstructor:
     case IProblem.NotVisibleType:
     case IProblem.NotVisibleField:
     case IProblem.JavadocNotVisibleType:
       ModifierCorrectionSubProcessor.addNonAccessibleReferenceProposal(
           context, problem, proposals, ModifierCorrectionSubProcessor.TO_VISIBLE, 10);
       break;
     case IProblem.BodyForAbstractMethod:
     case IProblem.AbstractMethodInAbstractClass:
       ModifierCorrectionSubProcessor.addAbstractMethodProposals(context, problem, proposals);
       break;
     case IProblem.AbstractMethodMustBeImplemented:
       LocalCorrectionsSubProcessor.addUnimplementedMethodsProposals(context, problem, proposals);
       break;
     case IProblem.MissingValueForAnnotationMember:
       LocalCorrectionsSubProcessor.addValueForAnnotationProposals(context, problem, proposals);
       break;
     case IProblem.BodyForNativeMethod:
       ModifierCorrectionSubProcessor.addNativeMethodProposals(context, problem, proposals);
       break;
     case IProblem.MethodRequiresBody:
       ModifierCorrectionSubProcessor.addMethodRequiresBodyProposals(context, problem, proposals);
       break;
     case IProblem.OuterLocalMustBeFinal:
       ModifierCorrectionSubProcessor.addNonFinalLocalProposal(context, problem, proposals);
       break;
     case IProblem.UninitializedLocalVariable:
       LocalCorrectionsSubProcessor.addUninitializedLocalVariableProposal(
           context, problem, proposals);
       break;
     case IProblem.UnhandledExceptionInDefaultConstructor:
     case IProblem.UndefinedConstructorInDefaultConstructor:
     case IProblem.NotVisibleConstructorInDefaultConstructor:
       LocalCorrectionsSubProcessor.addConstructorFromSuperclassProposal(
           context, problem, proposals);
       break;
     case IProblem.UnusedPrivateMethod:
     case IProblem.UnusedPrivateConstructor:
     case IProblem.UnusedPrivateField:
     case IProblem.UnusedPrivateType:
     case IProblem.LocalVariableIsNeverUsed:
     case IProblem.ArgumentIsNeverUsed:
       LocalCorrectionsSubProcessor.addUnusedMemberProposal(context, problem, proposals);
       break;
     case IProblem.NeedToEmulateFieldReadAccess:
     case IProblem.NeedToEmulateFieldWriteAccess:
     case IProblem.NeedToEmulateMethodAccess:
     case IProblem.NeedToEmulateConstructorAccess:
       ModifierCorrectionSubProcessor.addNonAccessibleReferenceProposal(
           context, problem, proposals, ModifierCorrectionSubProcessor.TO_NON_PRIVATE, 5);
       break;
     case IProblem.SuperfluousSemicolon:
       LocalCorrectionsSubProcessor.addSuperfluousSemicolonProposal(context, problem, proposals);
       break;
     case IProblem.UnnecessaryCast:
       LocalCorrectionsSubProcessor.addUnnecessaryCastProposal(context, problem, proposals);
       break;
     case IProblem.UnnecessaryInstanceof:
       LocalCorrectionsSubProcessor.addUnnecessaryInstanceofProposal(context, problem, proposals);
       break;
     case IProblem.UnusedMethodDeclaredThrownException:
     case IProblem.UnusedConstructorDeclaredThrownException:
       LocalCorrectionsSubProcessor.addUnnecessaryThrownExceptionProposal(
           context, problem, proposals);
       break;
     case IProblem.UnqualifiedFieldAccess:
       LocalCorrectionsSubProcessor.addUnqualifiedFieldAccessProposal(context, problem, proposals);
       break;
     case IProblem.Task:
       proposals.add(new TaskMarkerProposal(context.getCompilationUnit(), problem, 10));
       break;
     case IProblem.JavadocMissing:
       JavadocTagsSubProcessor.getMissingJavadocCommentProposals(context, problem, proposals);
       break;
     case IProblem.JavadocMissingParamTag:
     case IProblem.JavadocMissingReturnTag:
     case IProblem.JavadocMissingThrowsTag:
       JavadocTagsSubProcessor.getMissingJavadocTagProposals(context, problem, proposals);
       break;
     case IProblem.JavadocInvalidThrowsClassName:
     case IProblem.JavadocDuplicateThrowsClassName:
     case IProblem.JavadocDuplicateReturnTag:
     case IProblem.JavadocDuplicateParamName:
     case IProblem.JavadocInvalidParamName:
     case IProblem.JavadocUnexpectedTag:
     case IProblem.JavadocInvalidTag:
       JavadocTagsSubProcessor.getRemoveJavadocTagProposals(context, problem, proposals);
       break;
     case IProblem.LocalVariableHidingLocalVariable:
     case IProblem.LocalVariableHidingField:
     case IProblem.FieldHidingLocalVariable:
     case IProblem.FieldHidingField:
     case IProblem.ArgumentHidingLocalVariable:
     case IProblem.ArgumentHidingField:
     case IProblem.UseAssertAsAnIdentifier:
     case IProblem.UseEnumAsAnIdentifier:
     case IProblem.RedefinedLocal:
     case IProblem.RedefinedArgument:
       LocalCorrectionsSubProcessor.addInvalidVariableNameProposals(context, problem, proposals);
       break;
     case IProblem.NoMessageSendOnArrayType:
       UnresolvedElementsSubProcessor.getArrayAccessProposals(context, problem, proposals);
       break;
     case IProblem.InvalidOperator:
       LocalCorrectionsSubProcessor.getInvalidOperatorProposals(context, problem, proposals);
       break;
     case IProblem.MissingSerialVersion:
       SerialVersionSubProcessor.getSerialVersionProposals(context, problem, proposals);
       break;
     case IProblem.UnnecessaryElse:
       LocalCorrectionsSubProcessor.getUnnecessaryElseProposals(context, problem, proposals);
       break;
     case IProblem.SuperclassMustBeAClass:
       LocalCorrectionsSubProcessor.getInterfaceExtendsClassProposals(context, problem, proposals);
       break;
     case IProblem.CodeCannotBeReached:
       LocalCorrectionsSubProcessor.getUnreachableCodeProposals(context, problem, proposals);
       break;
     case IProblem.InvalidUsageOfTypeParameters:
     case IProblem.InvalidUsageOfStaticImports:
     case IProblem.InvalidUsageOfForeachStatements:
     case IProblem.InvalidUsageOfTypeArguments:
     case IProblem.InvalidUsageOfEnumDeclarations:
     case IProblem.InvalidUsageOfVarargs:
     case IProblem.InvalidUsageOfAnnotations:
     case IProblem.InvalidUsageOfAnnotationDeclarations:
       // FICXADE  Need reflection here!
       // ECLIPSE 3.8
       //                ReorgCorrectionsSubProcessor.getNeedHigherComplianceProposals(context,
       // problem, proposals, JavaCore.VERSION_1_5);
       // ORIG
       //                ReorgCorrectionsSubProcessor.getNeed50ComplianceProposals(context,
       // problem, proposals);
       break;
     case IProblem.NonGenericType:
       TypeArgumentMismatchSubProcessor.removeMismatchedArguments(context, problem, proposals);
       break;
     case IProblem.MissingOverrideAnnotation:
       ModifierCorrectionSubProcessor.addOverrideAnnotationProposal(context, problem, proposals);
       break;
     case IProblem.FieldMissingDeprecatedAnnotation:
     case IProblem.MethodMissingDeprecatedAnnotation:
     case IProblem.TypeMissingDeprecatedAnnotation:
       ModifierCorrectionSubProcessor.addDeprecatedAnnotationProposal(context, problem, proposals);
       break;
     case IProblem.IsClassPathCorrect:
       ReorgCorrectionsSubProcessor.getIncorrectBuildPathProposals(context, problem, proposals);
       break;
     case IProblem.ForbiddenReference:
     case IProblem.DiscouragedReference:
       ReorgCorrectionsSubProcessor.getAccessRulesProposals(context, problem, proposals);
       break;
     case IProblem.AssignmentHasNoEffect:
       LocalCorrectionsSubProcessor.getAssignmentHasNoEffectProposals(context, problem, proposals);
       break;
     case IProblem.UnsafeTypeConversion:
     case IProblem.RawTypeReference:
     case IProblem.UnsafeRawMethodInvocation:
       LocalCorrectionsSubProcessor.addTypePrametersToRawTypeReference(
           context, problem, proposals);
       break;
     case IProblem.FallthroughCase:
       LocalCorrectionsSubProcessor.addFallThroughProposals(context, problem, proposals);
       break;
     case IProblem.UnhandledWarningToken:
       SuppressWarningsSubProcessor.addUnknownSuppressWarningProposals(
           context, problem, proposals);
       break;
     default:
   }
   if (JavaModelUtil.is50OrHigher(context.getCompilationUnit().getJavaProject())) {
     SuppressWarningsSubProcessor.addSuppressWarningsProposals(context, problem, proposals);
   }
 }