Example #1
0
 private RefactoringStatus initialize(JavaRefactoringArguments extended) {
   final String handle = extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
   if (handle != null) {
     final IJavaElement element =
         JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(), handle, false);
     if (element == null || !element.exists() || element.getElementType() != IJavaElement.FIELD)
       return JavaRefactoringDescriptorUtil.createInputFatalStatus(
           element, getProcessorName(), IJavaRefactorings.RENAME_FIELD);
     else fField = (IField) element;
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
   final String name = extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
   if (name != null && !"".equals(name)) // $NON-NLS-1$
   setNewElementName(name);
   else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
   final String references =
       extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES);
   if (references != null) {
     fUpdateReferences = Boolean.valueOf(references).booleanValue();
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES));
   final String matches = extended.getAttribute(ATTRIBUTE_TEXTUAL_MATCHES);
   if (matches != null) {
     fUpdateTextualMatches = Boolean.valueOf(matches).booleanValue();
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             ATTRIBUTE_TEXTUAL_MATCHES));
   final String getters = extended.getAttribute(ATTRIBUTE_RENAME_GETTER);
   if (getters != null) fRenameGetter = Boolean.valueOf(getters).booleanValue();
   else fRenameGetter = false;
   final String setters = extended.getAttribute(ATTRIBUTE_RENAME_SETTER);
   if (setters != null) fRenameSetter = Boolean.valueOf(setters).booleanValue();
   else fRenameSetter = false;
   final String delegate = extended.getAttribute(ATTRIBUTE_DELEGATE);
   if (delegate != null) {
     fDelegateUpdating = Boolean.valueOf(delegate).booleanValue();
   } else fDelegateUpdating = false;
   final String deprecate = extended.getAttribute(ATTRIBUTE_DEPRECATE);
   if (deprecate != null) {
     fDelegateDeprecation = Boolean.valueOf(deprecate).booleanValue();
   } else fDelegateDeprecation = false;
   return new RefactoringStatus();
 }
 private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
   final String selection =
       arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
   if (selection != null) {
     int offset = -1;
     int length = -1;
     final StringTokenizer tokenizer = new StringTokenizer(selection);
     if (tokenizer.hasMoreTokens()) offset = Integer.valueOf(tokenizer.nextToken()).intValue();
     if (tokenizer.hasMoreTokens()) length = Integer.valueOf(tokenizer.nextToken()).intValue();
     if (offset >= 0 && length >= 0) {
       fSelectionStart = offset;
       fSelectionLength = length;
     } else
       return RefactoringStatus.createFatalErrorStatus(
           Messages.format(
               RefactoringCoreMessages.InitializableRefactoring_illegal_argument,
               new Object[] {selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION}));
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION));
   final String handle = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
   if (handle != null) {
     final IJavaElement element =
         JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
     if (element == null
         || !element.exists()
         || element.getElementType() != IJavaElement.COMPILATION_UNIT)
       return JavaRefactoringDescriptorUtil.createInputFatalStatus(
           element, getName(), IJavaRefactorings.EXTRACT_CONSTANT);
     else fCu = (ICompilationUnit) element;
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
   final String visibility = arguments.getAttribute(ATTRIBUTE_VISIBILITY);
   if (visibility != null && !"".equals(visibility)) { // $NON-NLS-1$
     int flag = 0;
     try {
       flag = Integer.parseInt(visibility);
     } catch (NumberFormatException exception) {
       return RefactoringStatus.createFatalErrorStatus(
           Messages.format(
               RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
               ATTRIBUTE_VISIBILITY));
     }
     fVisibility = JdtFlags.getVisibilityString(flag);
   }
   final String name = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
   if (name != null && !"".equals(name)) // $NON-NLS-1$
   fConstantName = name;
   else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
   final String replace = arguments.getAttribute(ATTRIBUTE_REPLACE);
   if (replace != null) {
     fReplaceAllOccurrences = Boolean.valueOf(replace).booleanValue();
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             ATTRIBUTE_REPLACE));
   final String declareFinal = arguments.getAttribute(ATTRIBUTE_QUALIFY);
   if (declareFinal != null) {
     fQualifyReferencesWithDeclaringClassName = Boolean.valueOf(declareFinal).booleanValue();
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             ATTRIBUTE_QUALIFY));
   return new RefactoringStatus();
 }
  private ExtractConstantDescriptor createRefactoringDescriptor() {
    final Map<String, String> arguments = new HashMap<>();
    String project = null;
    IJavaProject javaProject = fCu.getJavaProject();
    if (javaProject != null) project = javaProject.getElementName();
    int flags =
        JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    if (JdtFlags.getVisibilityCode(fVisibility) != Modifier.PRIVATE)
      flags |= RefactoringDescriptor.STRUCTURAL_CHANGE;

    final String expression = ASTNodes.asString(fSelectedExpression.getAssociatedExpression());
    final String description =
        Messages.format(
            RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description_short,
            BasicElementLabels.getJavaElementName(fConstantName));
    final String header =
        Messages.format(
            RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description,
            new String[] {
              BasicElementLabels.getJavaElementName(fConstantName),
              BasicElementLabels.getJavaCodeString(expression)
            });
    final JDTRefactoringDescriptorComment comment =
        new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(
        Messages.format(
            RefactoringCoreMessages.ExtractConstantRefactoring_constant_name_pattern,
            BasicElementLabels.getJavaElementName(fConstantName)));
    comment.addSetting(
        Messages.format(
            RefactoringCoreMessages.ExtractConstantRefactoring_constant_expression_pattern,
            BasicElementLabels.getJavaCodeString(expression)));
    String visibility = fVisibility;
    if ("".equals(visibility)) // $NON-NLS-1$
    visibility = RefactoringCoreMessages.ExtractConstantRefactoring_default_visibility;
    comment.addSetting(
        Messages.format(
            RefactoringCoreMessages.ExtractConstantRefactoring_visibility_pattern, visibility));
    if (fReplaceAllOccurrences)
      comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_replace_occurrences);
    if (fQualifyReferencesWithDeclaringClassName)
      comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_qualify_references);
    arguments.put(
        JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT,
        JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fConstantName);
    arguments.put(
        JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION,
        new Integer(fSelectionStart).toString()
            + " "
            + new Integer(fSelectionLength).toString()); // $NON-NLS-1$
    arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceAllOccurrences).toString());
    arguments.put(
        ATTRIBUTE_QUALIFY, Boolean.valueOf(fQualifyReferencesWithDeclaringClassName).toString());
    arguments.put(
        ATTRIBUTE_VISIBILITY, new Integer(JdtFlags.getVisibilityCode(fVisibility)).toString());

    ExtractConstantDescriptor descriptor =
        RefactoringSignatureDescriptorFactory.createExtractConstantDescriptor(
            project, description, comment.asString(), arguments, flags);
    return descriptor;
  }
 /**
  * Initializes the refactoring from scripting arguments. Used by {@link
  * RenameVirtualMethodProcessor} and {@link RenameNonVirtualMethodProcessor}
  *
  * @param extended the arguments
  * @return the resulting status
  */
 protected final RefactoringStatus initialize(JavaRefactoringArguments extended) {
   fInitialized = true;
   final String handle = extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
   if (handle != null) {
     final IJavaElement element =
         JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(), handle, false);
     final String refactoring = getProcessorName();
     if (element instanceof IMethod) {
       final IMethod method = (IMethod) element;
       final IType declaring = method.getDeclaringType();
       if (declaring != null && declaring.exists()) {
         final IMethod[] methods = declaring.findMethods(method);
         if (methods != null && methods.length == 1 && methods[0] != null) {
           if (!methods[0].exists())
             return JavaRefactoringDescriptorUtil.createInputFatalStatus(
                 methods[0], refactoring, IJavaRefactorings.RENAME_METHOD);
           fMethod = methods[0];
           initializeWorkingCopyOwner();
         } else
           return JavaRefactoringDescriptorUtil.createInputFatalStatus(
               null, refactoring, IJavaRefactorings.RENAME_METHOD);
       } else
         return JavaRefactoringDescriptorUtil.createInputFatalStatus(
             element, refactoring, IJavaRefactorings.RENAME_METHOD);
     } else
       return JavaRefactoringDescriptorUtil.createInputFatalStatus(
           element, refactoring, IJavaRefactorings.RENAME_METHOD);
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
   final String name = extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
   if (name != null && !"".equals(name)) // $NON-NLS-1$
   setNewElementName(name);
   else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
   final String references =
       extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES);
   if (references != null) {
     fUpdateReferences = Boolean.valueOf(references).booleanValue();
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES));
   final String delegate = extended.getAttribute(ATTRIBUTE_DELEGATE);
   if (delegate != null) {
     fDelegateUpdating = Boolean.valueOf(delegate).booleanValue();
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             ATTRIBUTE_DELEGATE));
   final String deprecate = extended.getAttribute(ATTRIBUTE_DEPRECATE);
   if (deprecate != null) {
     fDelegateDeprecation = Boolean.valueOf(deprecate).booleanValue();
   } else
     return RefactoringStatus.createFatalErrorStatus(
         Messages.format(
             RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
             ATTRIBUTE_DEPRECATE));
   return new RefactoringStatus();
 }