Search in sources :

Example 1 with EncapsulateFieldDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.EncapsulateFieldDescriptor in project che by eclipse.

the class SelfEncapsulateFieldRefactoring method createChange.

@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fField.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE;
    final IType declaring = fField.getDeclaringType();
    try {
        if (declaring.isAnonymous() || declaring.isLocal())
            flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    } catch (JavaModelException exception) {
        JavaPlugin.log(exception);
    }
    final String description = Messages.format(RefactoringCoreMessages.SelfEncapsulateField_descriptor_description_short, BasicElementLabels.getJavaElementName(fField.getElementName()));
    final String header = Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fField, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getElementLabel(declaring, JavaElementLabels.ALL_FULLY_QUALIFIED) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_original_pattern, JavaElementLabels.getElementLabel(fField, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_getter_pattern, BasicElementLabels.getJavaElementName(fGetterName)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_setter_pattern, BasicElementLabels.getJavaElementName(fSetterName)));
    String visibility = JdtFlags.getVisibilityString(fVisibility);
    if (//$NON-NLS-1$
    "".equals(visibility))
        visibility = RefactoringCoreMessages.SelfEncapsulateField_default_visibility;
    comment.addSetting(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_visibility_pattern, visibility));
    if (fEncapsulateDeclaringClass)
        comment.addSetting(RefactoringCoreMessages.SelfEncapsulateField_use_accessors);
    else
        comment.addSetting(RefactoringCoreMessages.SelfEncapsulateField_do_not_use_accessors);
    if (fGenerateJavadoc)
        comment.addSetting(RefactoringCoreMessages.SelfEncapsulateField_generate_comments);
    final EncapsulateFieldDescriptor descriptor = RefactoringSignatureDescriptorFactory.createEncapsulateFieldDescriptor(project, description, comment.asString(), arguments, flags);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fField));
    arguments.put(ATTRIBUTE_VISIBILITY, new Integer(fVisibility).toString());
    arguments.put(ATTRIBUTE_INSERTION, new Integer(fInsertionIndex).toString());
    arguments.put(ATTRIBUTE_SETTER, fSetterName);
    arguments.put(ATTRIBUTE_GETTER, fGetterName);
    arguments.put(ATTRIBUTE_COMMENTS, Boolean.valueOf(fGenerateJavadoc).toString());
    arguments.put(ATTRIBUTE_DECLARING, Boolean.valueOf(fEncapsulateDeclaringClass).toString());
    final DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, getName());
    TextChange[] changes = fChangeManager.getAllChanges();
    pm.beginTask(NO_NAME, changes.length);
    pm.setTaskName(RefactoringCoreMessages.SelfEncapsulateField_create_changes);
    for (int i = 0; i < changes.length; i++) {
        result.add(changes[i]);
        pm.worked(1);
    }
    pm.done();
    return result;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) EncapsulateFieldDescriptor(org.eclipse.jdt.core.refactoring.descriptors.EncapsulateFieldDescriptor) HashMap(java.util.HashMap) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) IType(org.eclipse.jdt.core.IType) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) IJavaProject(org.eclipse.jdt.core.IJavaProject)

Aggregations

HashMap (java.util.HashMap)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IType (org.eclipse.jdt.core.IType)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 EncapsulateFieldDescriptor (org.eclipse.jdt.core.refactoring.descriptors.EncapsulateFieldDescriptor)1 JDTRefactoringDescriptorComment (org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)1 DynamicValidationRefactoringChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange)1 TextChange (org.eclipse.ltk.core.refactoring.TextChange)1