Search in sources :

Example 1 with DynamicValidationStateChange

use of org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange in project che by eclipse.

the class IntroduceFactoryRefactoring method createChange.

/* (non-Javadoc)
	 * @see org.eclipse.ltk.core.refactoring.Refactoring#createChange(org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
    try {
        pm.beginTask(RefactoringCoreMessages.IntroduceFactory_createChanges, fAllCallsTo.length);
        final ITypeBinding binding = fFactoryOwningClass.resolveBinding();
        final Map<String, String> arguments = new HashMap<String, String>();
        String project = null;
        IJavaProject javaProject = fCUHandle.getJavaProject();
        if (javaProject != null)
            project = javaProject.getElementName();
        int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE;
        if (binding.isNested() && !binding.isMember())
            flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
        final String description = Messages.format(RefactoringCoreMessages.IntroduceFactoryRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fCtorOwningClass.getName().getIdentifier()));
        final String header = Messages.format(RefactoringCoreMessages.IntroduceFactory_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(fNewMethodName), BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), BindingLabelProvider.getBindingLabel(fCtorBinding, JavaElementLabels.ALL_FULLY_QUALIFIED) });
        final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
        comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceFactoryRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(fCtorBinding, JavaElementLabels.ALL_FULLY_QUALIFIED)));
        comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceFactoryRefactoring_factory_pattern, BasicElementLabels.getJavaElementName(fNewMethodName)));
        comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceFactoryRefactoring_owner_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED)));
        if (fProtectConstructor)
            comment.addSetting(RefactoringCoreMessages.IntroduceFactoryRefactoring_declare_private);
        final IntroduceFactoryDescriptor descriptor = RefactoringSignatureDescriptorFactory.createIntroduceFactoryDescriptor(project, description, comment.asString(), arguments, flags);
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCUHandle));
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fNewMethodName);
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + 1, JavaRefactoringDescriptorUtil.elementToHandle(project, binding.getJavaElement()));
        //$NON-NLS-1$
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString());
        arguments.put(ATTRIBUTE_PROTECT, Boolean.valueOf(fProtectConstructor).toString());
        final DynamicValidationStateChange result = new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.IntroduceFactory_name);
        boolean hitInFactoryClass = false;
        boolean hitInCtorClass = false;
        for (int i = 0; i < fAllCallsTo.length; i++) {
            SearchResultGroup rg = fAllCallsTo[i];
            ICompilationUnit unitHandle = rg.getCompilationUnit();
            CompilationUnitChange cuChange = new CompilationUnitChange(getName(), unitHandle);
            if (addAllChangesFor(rg, unitHandle, cuChange))
                result.add(cuChange);
            if (unitHandle.equals(fFactoryUnitHandle))
                hitInFactoryClass = true;
            if (unitHandle.equals(ASTCreator.getCu(fCtorOwningClass)))
                hitInCtorClass = true;
            pm.worked(1);
            if (pm.isCanceled())
                throw new OperationCanceledException();
        }
        if (!hitInFactoryClass) {
            // Handle factory class if no search hits there
            CompilationUnitChange cuChange = new CompilationUnitChange(getName(), fFactoryUnitHandle);
            addAllChangesFor(null, fFactoryUnitHandle, cuChange);
            result.add(cuChange);
        }
        if (!hitInCtorClass && !fFactoryUnitHandle.equals(ASTCreator.getCu(fCtorOwningClass))) {
            // Handle constructor-owning class if no search hits there
            CompilationUnitChange cuChange = new CompilationUnitChange(getName(), ASTCreator.getCu(fCtorOwningClass));
            addAllChangesFor(null, ASTCreator.getCu(fCtorOwningClass), cuChange);
            result.add(cuChange);
        }
        return result;
    } finally {
        pm.done();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) HashMap(java.util.HashMap) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) IntroduceFactoryDescriptor(org.eclipse.jdt.core.refactoring.descriptors.IntroduceFactoryDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 2 with DynamicValidationStateChange

use of org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange in project che by eclipse.

the class InferTypeArgumentsRefactoring method createChange.

/*
	 * @see org.eclipse.ltk.core.refactoring.Refactoring#createChange(org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    //$NON-NLS-1$
    pm.beginTask("", 1);
    try {
        DynamicValidationStateChange result = new DynamicValidationStateChange(RefactoringCoreMessages.InferTypeArgumentsRefactoring_name, fChangeManager.getAllChanges()) {

            @Override
            public final ChangeDescriptor getDescriptor() {
                final Map<String, String> arguments = new HashMap<String, String>();
                final IJavaProject project = getSingleProject();
                final String description = RefactoringCoreMessages.InferTypeArgumentsRefactoring_descriptor_description;
                final String header = project != null ? Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_descriptor_description_project, BasicElementLabels.getJavaElementName(project.getElementName())) : RefactoringCoreMessages.InferTypeArgumentsRefactoring_descriptor_description;
                final String name = project != null ? project.getElementName() : null;
                final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(name, this, header);
                final String[] settings = new String[fElements.length];
                for (int index = 0; index < settings.length; index++) settings[index] = JavaElementLabels.getTextLabel(fElements[index], JavaElementLabels.ALL_FULLY_QUALIFIED);
                comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.InferTypeArgumentsRefactoring_original_elements, settings));
                if (fAssumeCloneReturnsSameType)
                    comment.addSetting(RefactoringCoreMessages.InferTypeArgumentsRefactoring_assume_clone);
                if (fLeaveUnconstrainedRaw)
                    comment.addSetting(RefactoringCoreMessages.InferTypeArgumentsRefactoring_leave_unconstrained);
                final InferTypeArgumentsDescriptor descriptor = RefactoringSignatureDescriptorFactory.createInferTypeArgumentsDescriptor(name, description, comment.asString(), arguments, RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE);
                for (int index = 0; index < fElements.length; index++) arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (index + 1), JavaRefactoringDescriptorUtil.elementToHandle(name, fElements[index]));
                arguments.put(ATTRIBUTE_CLONE, Boolean.valueOf(fAssumeCloneReturnsSameType).toString());
                arguments.put(ATTRIBUTE_LEAVE, Boolean.valueOf(fLeaveUnconstrainedRaw).toString());
                return new RefactoringChangeDescriptor(descriptor);
            }
        };
        return result;
    } finally {
        pm.done();
    }
}
Also used : InferTypeArgumentsDescriptor(org.eclipse.jdt.core.refactoring.descriptors.InferTypeArgumentsDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) RefactoringChangeDescriptor(org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor)

Example 3 with DynamicValidationStateChange

use of org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange in project che by eclipse.

the class RefactoringSession method prepareChangesInfo.

/**
     * Prepare the information about changes which were applied.
     *
     * @param changes array of the applied changes
     * @param changesInfo prepared list of {@link ChangeInfo}
     */
public void prepareChangesInfo(Change[] changes, List<ChangeInfo> changesInfo) {
    for (Change ch : changes) {
        if (ch instanceof DynamicValidationStateChange) {
            prepareChangesInfo(((DynamicValidationStateChange) ch).getChildren(), changesInfo);
        } else {
            ChangeInfo changeInfo = DtoFactory.newDto(ChangeInfo.class);
            String refactoringName = ch.getName();
            if (ch instanceof UndoTextFileChange) {
                changeInfo.setName(ChangeInfo.ChangeName.UPDATE);
                changeInfo.setPath(((CompilationUnit) ch.getModifiedElement()).getPath().toString());
            }
            if (refactoringName.startsWith("Rename")) {
                if (ch instanceof RenameCompilationUnitChange) {
                    prepareRenameCompilationUnitChange(changeInfo, ch);
                } else if (ch instanceof RenamePackageChange) {
                    prepareRenamePackageChange(changesInfo, changeInfo, ch);
                }
            }
            if (refactoringName.startsWith("Move")) {
                prepareMoveChange(changeInfo, ch);
            }
            changesInfo.add(changeInfo);
        }
    }
}
Also used : CompilationUnit(org.eclipse.jdt.internal.core.CompilationUnit) ChangeInfo(org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo) RenamePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenamePackageChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) MoveCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange) UndoTextFileChange(org.eclipse.ltk.core.refactoring.UndoTextFileChange) Change(org.eclipse.ltk.core.refactoring.Change) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange) RenamePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenamePackageChange) UndoTextFileChange(org.eclipse.ltk.core.refactoring.UndoTextFileChange) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange)

Example 4 with DynamicValidationStateChange

use of org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange in project che by eclipse.

the class DeleteChangeCreator method createDeleteChange.

/**
	 * @param manager the text change manager
	 * @param resources the resources to delete
	 * @param javaElements the Java elements to delete
	 * @param changeName the name of the change
	 * @param packageDeletes a list of {@link IResource}s that will be deleted
	 *        by the delete operation of the {@link IPackageFragment}s in
	 *        <code>javaElements</code>, or <code>null</code> iff
	 *        <code>javaElements</code> does not contain package fragments
	 * @return the created change
	 * @throws CoreException
	 */
static Change createDeleteChange(TextChangeManager manager, IResource[] resources, IJavaElement[] javaElements, String changeName, List<IResource> packageDeletes) throws CoreException {
    /*
		 * Problem: deleting a package and subpackages can result in
		 * multiple package fragments in fJavaElements but only
		 * one folder in packageDeletes. The way to handle this is to make the undo
		 * change of individual package delete changes an empty change, and
		 * add take care of the undo in UndoablePackageDeleteChange.
		 */
    DynamicValidationStateChange result;
    if (packageDeletes.size() > 0) {
        result = new UndoablePackageDeleteChange(changeName, packageDeletes);
    } else {
        result = new DynamicValidationStateChange(changeName);
    }
    for (int i = 0; i < javaElements.length; i++) {
        IJavaElement element = javaElements[i];
        if (!ReorgUtils.isInsideCompilationUnit(element))
            result.add(createDeleteChange(element));
    }
    for (int i = 0; i < resources.length; i++) {
        result.add(createDeleteChange(resources[i]));
    }
    Map<ICompilationUnit, List<IJavaElement>> grouped = ReorgUtils.groupByCompilationUnit(getElementsSmallerThanCu(javaElements));
    if (grouped.size() != 0) {
        Assert.isNotNull(manager);
        for (Iterator<ICompilationUnit> iter = grouped.keySet().iterator(); iter.hasNext(); ) {
            ICompilationUnit cu = iter.next();
            result.add(createDeleteChange(cu, grouped.get(cu), manager));
        }
    }
    return result;
}
Also used : UndoablePackageDeleteChange(org.eclipse.jdt.internal.corext.refactoring.changes.UndoablePackageDeleteChange) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ArrayList(java.util.ArrayList) List(java.util.List) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange)

Example 5 with DynamicValidationStateChange

use of org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange in project che by eclipse.

the class DeleteChangeCreator method createPackageFragmentRootDeleteChange.

private static Change createPackageFragmentRootDeleteChange(IPackageFragmentRoot root) throws JavaModelException {
    IResource resource = root.getResource();
    if (resource != null && resource.isLinked()) {
        //XXX using this code is a workaround for jcore bug 31998
        //jcore cannot handle linked stuff
        //normally, we should always create DeletePackageFragmentRootChange
        CompositeChange composite = new DynamicValidationStateChange(RefactoringCoreMessages.DeleteRefactoring_delete_package_fragment_root);
        ClasspathChange change = ClasspathChange.removeEntryChange(root.getJavaProject(), root.getRawClasspathEntry());
        if (change != null) {
            composite.add(change);
        }
        //checked in preconditions
        Assert.isTrue(!Checks.isClasspathDelete(root));
        composite.add(createDeleteChange(resource));
        return composite;
    } else {
        Assert.isTrue(!root.isExternal());
        // TODO remove the query argument
        return new DeletePackageFragmentRootChange(root, true, null);
    }
}
Also used : ClasspathChange(org.eclipse.jdt.internal.corext.refactoring.changes.ClasspathChange) DeletePackageFragmentRootChange(org.eclipse.jdt.internal.corext.refactoring.changes.DeletePackageFragmentRootChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IResource(org.eclipse.core.resources.IResource) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange)

Aggregations

DynamicValidationStateChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange)7 JDTRefactoringDescriptorComment (org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)3 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)3 HashMap (java.util.HashMap)2 IResource (org.eclipse.core.resources.IResource)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 DynamicValidationRefactoringChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange)2 RenameCompilationUnitChange (org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange)2 Change (org.eclipse.ltk.core.refactoring.Change)2 RefactoringChangeDescriptor (org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ChangeInfo (org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo)1 IProject (org.eclipse.core.resources.IProject)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)1