Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.refactoring.impl;
  
 import static com.google.common.collect.Lists.*;
 import static com.google.common.collect.Sets.*;
 import static org.eclipse.ltk.core.refactoring.RefactoringStatus.*;
 
 import java.util.List;
 import java.util.Set;
 
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.OperationCanceledException;
 import  org.eclipse.core.runtime.SubMonitor;
 
Abstract base class to update the references to renamed elements. Sorts all references by project and uses a separate resource set for each project to assert proper initialization. Updates are performed in clusters of 20 (default) referring resources.

Author(s):
Jan Koehnlein - Initial contribution and API
Holger Schill
 
 public abstract class AbstractReferenceUpdater implements IReferenceUpdater {
 
 
 
 	public void createReferenceUpdates(ElementRenameArguments elementRenameArguments,
 			Iterable<IReferenceDescriptionreferenceDescriptionsIRefactoringUpdateAcceptor updateAcceptor,
 			IProgressMonitor monitor) {
 		RefactoringResourceSetProvider provider = elementRenameArguments.getResourceSetProvider();
 		createReferenceUpdates(elementRenameArgumentsreferenceDescriptionsupdateAcceptorprovider != null ? provider : monitor);
 	}
 	
 	protected void createReferenceUpdates(
 			ElementRenameArguments elementRenameArguments,
 			Iterable<IReferenceDescriptionreferenceDescriptions,
 			IRefactoringUpdateAcceptor updateAcceptor,
 			RefactoringResourceSetProvider resourceSetProvider,
 			IProgressMonitor monitor) {
 		SubMonitor progress = SubMonitor.convert(monitor, 100);
 		progress.beginTask("Sort references by project", 1);
 		Multimap<IProject, IReferenceDescriptionproject2references = .sortByProject(referenceDescriptions);
 		SubMonitor allProjectsProgress = progress.newChild(98).setWorkRemaining(project2references.keySet().size());
 		for (IProject project : project2references.keySet()) {
 			if (allProjectsProgress.isCanceled()) {
 				throw new OperationCanceledException();
 			}
 			Multimap<URIIReferenceDescriptionresource2references = .sortByResource(project2references
 					.get(project));
 			ResourceSet resourceSet = resourceSetProvider.get(project);
 			StatusWrapper status = updateAcceptor.getRefactoringStatus();
 			createClusteredReferenceUpdates(elementRenameArgumentsresource2referencesresourceSetupdateAcceptor,
 					statusallProjectsProgress.newChild(1));
 		}
 	}
 
 	protected void createClusteredReferenceUpdates(ElementRenameArguments elementRenameArguments,
 			Multimap<URIIReferenceDescriptionresource2referencesResourceSet resourceSet,
 			IRefactoringUpdateAcceptor updateAcceptorStatusWrapper status, IProgressMonitor monitor) {
 		SubMonitor progress = SubMonitor.convert(monitorresource2references.keySet().size() + 1);
 		if (loadTargetResources(resourceSetelementRenameArgumentsstatusprogress.newChild(1))) {
 			if (getClusterSize() > 0) {
 				Set<ResourcetargetResources = newHashSet(resourceSet.getResources());
 				Multimap<URIIReferenceDescriptioncluster = HashMultimap.create();
 				SubMonitor clusterMonitor = progress.newChild(1);
 				for (URI referringResourceURI : resource2references.keySet()) {
 					cluster.putAll(referringResourceURIresource2references.get(referringResourceURI));
 					if (cluster.keySet().size() == getClusterSize()) {
 						unloadNonTargetResources(resourceSettargetResources);
 						createReferenceUpdatesForCluster(elementRenameArgumentsclusterresourceSetupdateAcceptorstatusclusterMonitor);
 						cluster.clear();
					}
				}
				if (!cluster.isEmpty()) {
					unloadNonTargetResources(resourceSettargetResources);
					createReferenceUpdatesForCluster(elementRenameArgumentsclusterresourceSetupdateAcceptor,
							statusclusterMonitor);
				}
else {
				createReferenceUpdatesForCluster(elementRenameArgumentsresource2referencesresourceSet,
						updateAcceptorstatusprogress.newChild(resource2references.keySet().size()));
			}
		}
	}
	protected void unloadNonTargetResources(ResourceSet resourceSetSet<ResourcetargetResources) {
		for (Resource resource : newArrayList(resourceSet.getResources())) {
			if (!targetResources.contains(resource)) {
				resource.unload();
				resourceSet.getResources().remove(resource);
			}
		}
	}
	protected int getClusterSize() {
		return 20;
	}
	protected void createReferenceUpdatesForCluster(ElementRenameArguments elementRenameArguments,
			Multimap<URIIReferenceDescriptionresource2referencesResourceSet resourceSet,
			IRefactoringUpdateAcceptor updateAcceptorStatusWrapper status, IProgressMonitor monitor) {
		SubMonitor progress = SubMonitor.convert(monitor, 100);
		List<URIunloadableResources = loadReferringResources(resourceSetresource2references.keySet(), status,
				progress.newChild(10));
		if (progress.isCanceled()) {
			throw new OperationCanceledException();
		}
		for (URI unloadableResouce : unloadableResources)
			resource2references.removeAll(unloadableResouce);
		List<IReferenceDescriptionunresolvableReferences = resolveReferenceProxies(resourceSet,
				resource2references.values(), statusprogress.newChild(70));
		if (progress.isCanceled()) {
			throw new OperationCanceledException();
		}
		for (IReferenceDescription unresolvableReference : unresolvableReferences) {
			URI unresolvableReferringResource = unresolvableReference.getSourceEObjectUri().trimFragment();
			resource2references.remove(unresolvableReferringResourceunresolvableReference);
		}
		elementRenameArguments.getRenameStrategy().applyDeclarationChange(elementRenameArguments.getNewName(),
				resourceSet);
		if (progress.isCanceled()) {
			throw new OperationCanceledException();
		}
		createReferenceUpdates(elementRenameArgumentsresource2referencesresourceSetupdateAcceptor,
				progress.newChild(20));
		if (progress.isCanceled()) {
			throw new OperationCanceledException();
		}
		elementRenameArguments.getRenameStrategy().revertDeclarationChange(resourceSet);
	}
			Collection<IReferenceDescriptionvaluesStatusWrapper status, IProgressMonitor monitor) {
		List<IReferenceDescriptionunresolvedDescriptions = null;
		for (IReferenceDescription referenceDescription : values) {
			if (monitor.isCanceled()) {
				throw new OperationCanceledException();
			}
			EObject sourceEObject = resourceSet.getEObject(referenceDescription.getSourceEObjectUri(), true);
			if (sourceEObject == null) {
				handleCannotLoadReferringElement(referenceDescriptionstatus);
else {
				// this should not be necessary. see https://bugs.eclipse.org/bugs/show_bug.cgi?id=385408 
				if(sourceEObject.eIsProxy()) {
					sourceEObject = EcoreUtil.resolve(sourceEObjectsourceEObject.eResource());
					if(sourceEObject.eIsProxy()) {
						handleCannotLoadReferringElement(referenceDescriptionstatus);
					}
				}
				EObject resolvedReference = resolveReference(sourceEObjectreferenceDescription);
				if (resolvedReference == null || resolvedReference.eIsProxy())
					handleCannotResolveExistingReference(sourceEObjectreferenceDescriptionstatus);
				else
					continue;
			}
			if (unresolvedDescriptions == null)
				unresolvedDescriptions = newArrayList();
			unresolvedDescriptions.add(referenceDescription);
		}
		return (unresolvedDescriptions == null) ? Collections.<IReferenceDescriptionemptyList()
unresolvedDescriptions;
	}
	protected EObject resolveReference(EObject referringElementIReferenceDescription referenceDescription) {
		Object resolvedValue = referringElement.eGet(referenceDescription.getEReference());
		if (referenceDescription.getEReference().isMany()) {
			List<?> list = (List<?>) resolvedValue;
			resolvedValue = list.get(referenceDescription.getIndexInList());
		}
		return (EObjectresolvedValue;
	}
	protected void handleCannotLoadReferringElement(IReferenceDescription referenceDescriptionStatusWrapper status) {
		status.add(ERROR, "Cannot find referring element {0}.\nMaybe the index is be corrupt. Consider a rebuild.",
				referenceDescription.getSourceEObjectUri());
	}
	protected void handleCannotResolveExistingReference(EObject sourceEObject,
			IReferenceDescription referenceDescriptionStatusWrapper status) {
		status.add(ERROR, "Cannot resolve existing reference.\nMaybe the index is be corrupt. Consider a rebuild.",
				referenceDescription.getSourceEObjectUri());
	}
	protected abstract void createReferenceUpdates(ElementRenameArguments elementRenameArguments,
			Multimap<URIIReferenceDescriptionresource2referencesResourceSet resourceSet,
			IRefactoringUpdateAcceptor updateAcceptor, IProgressMonitor monitor);
	protected boolean loadTargetResources(ResourceSet resourceSetElementRenameArguments elementRenameArguments,
			StatusWrapper status, IProgressMonitor monitor) {
		boolean isSuccess = true;
		for (URI renamedElementURI : elementRenameArguments.getRenamedElementURIs()) {
			if (monitor.isCanceled()) {
				throw new OperationCanceledException();
			}
			EObject renamedElement = resourceSet.getEObject(renamedElementURItrue);
			if (renamedElement == null || renamedElement.eIsProxy()) {
				status.add(ERROR, "Cannot load target element {0}."renamedElementURI);
				isSuccess = false;
			}
		}
		return isSuccess;
	}
	protected List<URIloadReferringResources(ResourceSet resourceSetIterable<URIreferringResourceURIs,
			StatusWrapper status, IProgressMonitor monitor) {
		List<URIunloadableResources = null;
		for (URI referringResourceURI : referringResourceURIs) {
			if (monitor.isCanceled()) {
				throw new OperationCanceledException();
			}
			Resource referringResource = resourceSet.getResource(referringResourceURItrue);
			if (referringResource == null) {
				status.add(ERROR, "Could not load referring resource "referringResourceURI);
				if (unloadableResources == null)
					unloadableResources = newArrayList();
				unloadableResources.add(referringResourceURI);
			}
		}
		return unloadableResources == null ? Collections.<URIemptyList() : unloadableResources;
	}
New to GrepCode? Check out our FAQ X