Java Code Examples for org.eclipse.emf.ecore.resource.ResourceSet#getEObject()

The following examples show how to use org.eclipse.emf.ecore.resource.ResourceSet#getEObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: N4JSRenameStrategy.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Custom logics for creating text edits for definitions of composed members
 */
@Override
public void createDeclarationUpdates(String newName, ResourceSet resourceSet,
		IRefactoringUpdateAcceptor updateAcceptor) {

	EObject targetElement = resourceSet.getEObject(getTargetElementOriginalURI(), true);
	if (TypeModelUtils.isComposedTElement(targetElement)) {
		// If target element is a composed element, create updates for its constituent members
		List<TMember> constituentMembers = ((TMember) targetElement).getConstituentMembers();
		for (TMember constituentMember : constituentMembers) {
			String text = newName;
			EAttribute att = getNameAttribute(constituentMember);
			ITextRegion nameRegion = getOriginalNameRegion(constituentMember, att);
			ReplaceEdit replaceEdit = new ReplaceEdit(nameRegion.getOffset(), nameRegion.getLength(), text);
			updateAcceptor.accept(EcoreUtil.getURI(constituentMember).trimFragment(),
					replaceEdit);
		}
	} else {
		updateAcceptor.accept(getTargetElementOriginalURI().trimFragment(), getDeclarationTextEdit(newName));
	}
}
 
Example 2
Source File: DefaultReferenceUpdater.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void createReferenceUpdate(IReferenceDescription referenceDescription, URI referringResourceURI,
		ElementRenameArguments elementRenameArguments, ResourceSet resourceSet,
		IRefactoringUpdateAcceptor updateAcceptor) {
	URI referringElementNewURI = elementRenameArguments
			.getNewElementURI(referenceDescription.getSourceEObjectUri());
	EObject referringElement = resourceSet.getEObject(referringElementNewURI, false);
	if (referringElement == null) {
		updateAcceptor.getRefactoringStatus().add(RefactoringStatus.ERROR, "Cannot find new referring element.", referringElementNewURI);
		log.error("Cannot find new referring element. ReferringElementURI:" + referringElementNewURI);
		return;
	}
	URI targetElementNewURI = elementRenameArguments.getNewElementURI(referenceDescription.getTargetEObjectUri());
	EObject newTargetElement = resourceSet.getEObject(targetElementNewURI, false);
	if (newTargetElement == null) {
		updateAcceptor.getRefactoringStatus().add(RefactoringStatus.ERROR, "Cannot find new target element.", targetElementNewURI);
		log.error("Cannot find new target element. ReferringElement:" + referringElement + " targetElementNewURI:" + targetElementNewURI);
		return;
	}
	createReferenceUpdate(referringElement, referringResourceURI, referenceDescription.getEReference(),
			referenceDescription.getIndexInList(), newTargetElement, updateAcceptor);
}
 
Example 3
Source File: XFunctionTypeRefImplCustom.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected JvmTypeReference getJavaLangObjectTypeRef(JvmType rawType, TypesFactory typesFactory) {
	ResourceSet rs = EcoreUtil2.getResourceSet(rawType);
	JvmParameterizedTypeReference refToObject = typesFactory.createJvmParameterizedTypeReference();
	if (rs != null) {
		EObject javaLangObject = rs.getEObject(javaLangObjectURI, true);
		if (javaLangObject instanceof JvmType) {
			JvmType objectDeclaration = (JvmType) javaLangObject;
			refToObject.setType(objectDeclaration);
			return refToObject;
		}
	}
	JvmGenericType proxy = typesFactory.createJvmGenericType();
	((InternalEObject)proxy).eSetProxyURI(javaLangObjectURI);
	refToObject.setType(proxy);
	return refToObject;
}
 
Example 4
Source File: AbstractProcessorBasedRenameParticipant.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected List<? extends IRenameElementContext> createRenameElementContexts(Object element) {
	if (element instanceof IRenameElementContext) {
		IRenameElementContext triggeringContext = (IRenameElementContext) element;
		IProject project = projectUtil.getProject(triggeringContext.getTargetElementURI());
		if (project != null) {
			ResourceSet resourceSet = resourceSetProvider.get(project);
			EObject originalTarget = resourceSet.getEObject(triggeringContext.getTargetElementURI(), true);
			List<EObject> renamedElements = getRenamedElementsOrProxies(originalTarget);
			if (renamedElements == null || renamedElements.isEmpty())
				return null;
			List<IRenameElementContext> contexts = newArrayListWithCapacity(renamedElements.size());
			for (EObject renamedElement : renamedElements)
				contexts.add(new IRenameElementContext.Impl(EcoreUtil.getURI(renamedElement), renamedElement
						.eClass(), triggeringContext.getTriggeringEditor(), triggeringContext
						.getTriggeringEditorSelection(), triggeringContext.getContextResourceURI()));
			return contexts;
		}
	}
	return null;
}
 
Example 5
Source File: DeserializationConversionContext.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public void setGrammarIdToURIMap(String[] grammarIdToURIMap) {
	grammarIdToGrammarElementMap = new EObject[grammarIdToURIMap.length];

	ResourceSet grammarResourceSet = grammarAccess.getGrammar().eResource().getResourceSet();
	for (int grammarId = 0; grammarId < grammarIdToURIMap.length; ++grammarId) {
		URI uri = URI.createURI(grammarIdToURIMap[grammarId], true);
		EObject grammarElement = grammarResourceSet.getEObject(uri, true);

		if (grammarElement == null) {
			throw new IllegalStateException(
					"Apparently the grammar has changed so that it's no longer possible to identify the "
							+ "serialized grammar elements.  The following grammar element URI is no longer valid: "
							+ uri.toString());
		}

		grammarIdToGrammarElementMap[grammarId] = grammarElement;
	}
}
 
Example 6
Source File: EmfResourceReferenceUpdater.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void createReferenceUpdates(ElementRenameArguments elementRenameArguments,
		Multimap<URI, IReferenceDescription> resource2references, ResourceSet resourceSet,
		IRefactoringUpdateAcceptor updateAcceptor, IProgressMonitor monitor) {
	for (URI referringResourceURI : resource2references.keySet()) {
		try {
			if (monitor.isCanceled())
				throw new OperationCanceledException();
			Resource referringResource = resourceSet.getResource(referringResourceURI, false);
			EObject refactoredElement = resourceSet.getEObject(elementRenameArguments.getNewElementURI(elementRenameArguments.getTargetElementURI()), true);
			if (referringResource != refactoredElement.eResource()) {
				if (refactoredElement instanceof EClassifier) { 
					for (IReferenceDescription reference : resource2references.get(referringResourceURI)) {
						EObject source = referringResource.getEObject(reference.getSourceEObjectUri().fragment());
						if (source == null) {
							LOG.error("Couldn't find source element "+ reference.getSourceEObjectUri() + " in " + referringResource.getURI());
						} else {
							EObject referringEReference = source.eContainer();
							if (referringEReference != null && referringEReference instanceof EReference)
								((EReference) referringEReference).setEType((EClassifier) refactoredElement);
						}
					}
				}
				changeUtil.addSaveAsUpdate(referringResource, updateAcceptor);
			}
		} catch (OperationCanceledException e) {
			throw e;
		} catch (Exception exc) {
			throw new WrappedException(exc);
		}
	}
}
 
Example 7
Source File: AbstractLaunchConfigurationMainTab.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the name of the class and method if the USER_SELECTION URI refers to a method. Otherwise an empty string
 * is returned.
 */
public String getTestMethod(ILaunchConfiguration configuration) {
	try {
		String uriStr = configuration.getAttribute(RunConfiguration.USER_SELECTION, "");
		if (uriStr == null) {
			return "";
		}
		final URI uri = uriStr.trim().length() > 0 ? URI.createURI(uriStr) : null;
		if (uri == null) {
			return "";
		}
		final URI trimmedUri = uri.hasFragment() ? uri.trimFragment() : uri;
		final IN4JSProject project = in4jsCore.findProject(trimmedUri).orNull();
		if (project != null) {
			ResourceSet resSet = in4jsCore.createResourceSet(Optional.of(project));
			if (resSet != null) {
				EObject eobject = resSet.getEObject(uri, true);
				if (eobject instanceof N4MethodDeclaration) {
					N4MethodDeclaration method = (N4MethodDeclaration) eobject;
					String name = ((N4MethodDeclaration) eobject).getName();
					if (method.getOwner() instanceof N4ClassDeclaration) {
						return ((N4ClassDeclaration) method.getOwner()).getName() + "." + name;
					}
					return name;
				}
			}
		}
	} catch (Exception ex) {
		// be robust here
	}
	return "";

}
 
Example 8
Source File: ParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testUriFormat2() throws Exception {
	URI uri = EcoreUtil.getURI(AsubpackagePackage.Literals.ATYPE);
	assertTrue(AsubpackagePackage.eNS_URI.equals(uri.trimFragment().toString()));
	ResourceSet set = new ResourceSetImpl();
	EObject eObject = set.getEObject(uri, true);
	assertSame(AsubpackagePackage.Literals.ATYPE, eObject);
}
 
Example 9
Source File: N4JSRenameStrategy.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void revertDeclarationChange(ResourceSet resourceSet) {
	EObject targetElement = resourceSet.getEObject(getTargetElementOriginalURI(), true);
	if (TypeModelUtils.isComposedTElement(targetElement)) {
		for (URI targetElementNewURI : targetElementNewURIs) {
			setName(targetElementNewURI, getOriginalName(), resourceSet);
		}
	}
	super.revertDeclarationChange(resourceSet);
}
 
Example 10
Source File: StatusWrapper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected RefactoringStatusContext createContext(URI uri, ResourceSet resourceSet) {
	EObject eObject = null;
	if (resourceSet != null && uri.hasFragment())
		eObject = resourceSet.getEObject(uri, true);
	if (eObject != null)
		return createContext(eObject);
	else
		return createContext(projectUtil.findFileStorage(uri, false), null);
}
 
Example 11
Source File: DispatchMethodRenameStrategy.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean initialize(EObject xtendMethod, IRenameElementContext context) {
	Assert.isLegal(xtendMethod instanceof XtendFunction);
	Assert.isLegal(((XtendFunction) xtendMethod).isDispatch());
	Assert.isLegal(context instanceof DispatchMethodRenameContext);
	ResourceSet resourceSet = xtendMethod.eResource().getResourceSet();
	Map<URI, IJavaElement> jvm2JavaElements = ((DispatchMethodRenameContext)context).getJvm2JavaElements();
	for(URI dispatchOperationURI: jvm2JavaElements.keySet()) {
		JvmOperation dispatchOperation = (JvmOperation) resourceSet.getEObject(dispatchOperationURI, true);
		XtendFunction xtendDispatchMethod = associations.getXtendFunction(dispatchOperation);
		if(xtendDispatchMethod != null) {
			if(equal(xtendDispatchMethod.getName(),dispatchOperation.getSimpleName())) {
				// synthetic dispatcher
				dispatchers.add(dispatchOperation);
			} else {
				// xtend dispatch method
				XtendDispatchMethodChildStrategy xtendChildStrategy = childStrategyProvider.get();
				xtendChildStrategy.initialize(xtendDispatchMethod, context);
				children.add(xtendChildStrategy);
			}
		} else {
			// a dispatch method form a Java class
			JavaDispatchMethodChildStrategy jvmChildStrategy = javaStrategyProvider.get();
			jvmChildStrategy.initialize(dispatchOperation, context);
			children.add(jvmChildStrategy);
		}
	}
	return !children.isEmpty();
}
 
Example 12
Source File: EcorePackageRenameStrategy.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the text update for the rename.
 *
 * @param newName the new package name.
 * @param resourceSet the set of resources.
 * @return the text update.
 */
protected TextEdit getDeclarationTextEdit(String newName, ResourceSet resourceSet) {
	final EObject object = resourceSet.getEObject(this.uriProvider.apply(resourceSet), true);
	if (object instanceof SarlScript) {
		final ITextRegion region = getOriginalPackageRegion((SarlScript) object);
		if (region != null) {
			return new ReplaceEdit(region.getOffset(), region.getLength(), newName);
		}
	}
	throw new RefactoringException("SARL script not loaded."); //$NON-NLS-1$
}
 
Example 13
Source File: XtextElementLinksTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected EObject getEObject (String link) throws URISyntaxException {
	URI uri = elementLinks.parseURI(new java.net.URI(getLocation(link)));
	ResourceSet rs = document.readOnly(new IUnitOfWork<ResourceSet, XtextResource>() {
		@Override
		public ResourceSet exec(XtextResource state) throws Exception {
			return state.getResourceSet();
		}
	});
	return rs.getEObject(uri, true);	
}
 
Example 14
Source File: XtextRenameStrategyProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void renameReturnType(URI targetElementURI, String newName, ResourceSet resourceSet) {
	AbstractRule rule = (AbstractRule) resourceSet.getEObject(targetElementURI, false);
	TypeRef parserRuleType = rule.getType();
	if (rule.getName().equals(parserRuleType.getClassifier().getName())
			&& parserRuleType.getMetamodel() instanceof GeneratedMetamodel) {
		parserRuleType.getClassifier().setName(newName);
	}
}
 
Example 15
Source File: RenamedElementTracker.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected Map<EObject, URI> resolveRenamedElements(Iterable<URI> renamedElementURIs, ResourceSet resourceSet) {
	Map<EObject, URI> renamedElement2oldURI = newHashMap();
	for (URI renamedElementURI : renamedElementURIs) {
		EObject renamedElement = resourceSet.getEObject(renamedElementURI, true);
		if (renamedElement == null)
			throw new RefactoringException("Cannot resolve dependent element " + notNull(renamedElementURI));
		renamedElement2oldURI.put(renamedElement, renamedElementURI);
	}
	return renamedElement2oldURI;
}
 
Example 16
Source File: EcorePackageRenameStrategy.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Change the package name.
 *
 * @param newName the new name.
 * @param resourceSet the set of resource to use.
 */
protected void setPackageName(String newName, ResourceSet resourceSet) {
	final EObject object = resourceSet.getEObject(this.uriProvider.apply(resourceSet), true);
	if (object instanceof SarlScript) {
		((SarlScript) object).setPackage(newName);
	} else {
		throw new RefactoringException("SARL script not loaded."); //$NON-NLS-1$
	}
}
 
Example 17
Source File: DotRenameStrategy.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected EObject setName(URI targetElementURI, String newName,
		ResourceSet resourceSet) {
	EObject targetElement = resourceSet.getEObject(targetElementURI, false);
	if (targetElement == null) {
		throw new RefactoringException("Target element not loaded."); //$NON-NLS-1$
	}
	targetElement.eSet(getNameAttribute(), ID.fromString(newName));
	return targetElement;
}
 
Example 18
Source File: AbstractReferenceUpdater.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected List<IReferenceDescription> resolveReferenceProxies(ResourceSet resourceSet,
		Collection<IReferenceDescription> values, StatusWrapper status, IProgressMonitor monitor) {
	List<IReferenceDescription> unresolvedDescriptions = null;
	for (IReferenceDescription referenceDescription : values) {
		if (monitor.isCanceled()) {
			throw new OperationCanceledException();
		}
		EObject sourceEObject = resourceSet.getEObject(referenceDescription.getSourceEObjectUri(), true);
		if (sourceEObject == null) {
			handleCannotLoadReferringElement(referenceDescription, status);
		} else {
			// this should not be necessary. see https://bugs.eclipse.org/bugs/show_bug.cgi?id=385408 
			if(sourceEObject.eIsProxy()) {
				sourceEObject = EcoreUtil.resolve(sourceEObject, sourceEObject.eResource());
				if(sourceEObject.eIsProxy()) {
					handleCannotLoadReferringElement(referenceDescription, status);
				}
			}
			EObject resolvedReference = resolveReference(sourceEObject, referenceDescription);
			if (resolvedReference == null || resolvedReference.eIsProxy())
				handleCannotResolveExistingReference(sourceEObject, referenceDescription, status);
			else
				continue;
		}
		if (unresolvedDescriptions == null)
			unresolvedDescriptions = newArrayList();
		unresolvedDescriptions.add(referenceDescription);
	}
	return (unresolvedDescriptions == null) ? Collections.<IReferenceDescription> emptyList()
			: unresolvedDescriptions;
}
 
Example 19
Source File: AbstractReferenceUpdater.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean loadTargetResources(ResourceSet resourceSet, ElementRenameArguments elementRenameArguments,
		StatusWrapper status, IProgressMonitor monitor) {
	boolean isSuccess = true;
	for (URI renamedElementURI : elementRenameArguments.getRenamedElementURIs()) {
		if (monitor.isCanceled()) {
			throw new OperationCanceledException();
		}
		EObject renamedElement = resourceSet.getEObject(renamedElementURI, true);
		if (renamedElement == null || renamedElement.eIsProxy()) {
			status.add(ERROR, "Cannot load target element {0}.", renamedElementURI);
			isSuccess = false;
		}
	}
	return isSuccess;
}
 
Example 20
Source File: JvmElementFinder.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public EObject getCorrespondingJvmElement(IJavaElement javaElement, ResourceSet resourceSet) {
	typeProviderFactory.findOrCreateTypeProvider(resourceSet);
	URI jvmElementURI = typeURIHelper.getFullURI(javaElement);
	return (jvmElementURI != null) ? resourceSet.getEObject(jvmElementURI, true) : null;
}