Java Code Examples for org.eclipse.emf.ecore.util.EcoreUtil.resolve()

The following are Jave code examples for showing how to use resolve() of the org.eclipse.emf.ecore.util.EcoreUtil class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: n4js   File: FQNImporter.java   View Source Code Vote up 6 votes
/**
 * Return the to-be-inserted string if an existing import is present.
 */
@Override
public String getActualReplacementString(ConfigurableCompletionProposal proposal) {
	String syntacticReplacementString = proposal.getReplacementString();
	if (scope != null) {
		final QualifiedName qualifiedName = applyValueConverter(syntacticReplacementString);
		if (qualifiedName.getSegmentCount() == 1) {
			return syntacticReplacementString;
		}
		final IEObjectDescription element = scope.getSingleElement(qualifiedName);
		if (element != null) {
			EObject resolved = EcoreUtil.resolve(element.getEObjectOrProxy(), context);
			if (!resolved.eIsProxy()) {
				IEObjectDescription description = findApplicableDescription(resolved, qualifiedName, true);
				if (description != null) {
					String multisegmentProposal = applyValueConverter(description.getName());
					return multisegmentProposal;
				}
			}
		}
	}
	return syntacticReplacementString;

}
 
Example 2
Project: n4js   File: ContainerTypesHelper.java   View Source Code Vote up 6 votes
private List<Type> getPolyfillTypesFromScope(QualifiedName fqn) {

			IScope contextScope = polyfillScopeAccess.getRecordingPolyfillScope(contextResource);
			List<Type> types = new ArrayList<>();

			// contextScope.getElements(fqn) returns all polyfills, since shadowing is handled differently
			// for them!
			for (IEObjectDescription descr : contextScope.getElements(fqn)) {
				Type polyfillType = (Type) descr.getEObjectOrProxy();
				if (polyfillType.eIsProxy()) {
					// TODO review: this seems odd... is this a test setup problem (since we do not use the
					// index
					// there and load the resource separately)?
					polyfillType = (Type) EcoreUtil.resolve(polyfillType, contextResource);
					if (polyfillType.eIsProxy()) {
						throw new IllegalStateException("unexpected proxy");
					}
				}
				types.add(polyfillType);
			}
			// }

			return types;
		}
 
Example 3
Project: n4js   File: TestDiscoveryHelper.java   View Source Code Vote up 5 votes
private Optional<TClass> loadTClass(final ResourceSet resSet, final IEObjectDescription objDesc) {
	if (T_CLASS.isSuperTypeOf(objDesc.getEClass())) {
		final EObject objectOrProxy = objDesc.getEObjectOrProxy();
		final EObject object = objectOrProxy.eIsProxy() ? EcoreUtil.resolve(objectOrProxy, resSet) : objectOrProxy;
		if (!object.eIsProxy()) {
			return fromNullable((TClass) object);
		}
	}
	return absent();
}
 
Example 4
Project: n4js   File: AmbiguousImportDescription.java   View Source Code Vote up 5 votes
@Override
public String getMessage() {
	StringBuilder typeListStr = new StringBuilder();
	IdentifiableElement first = (IdentifiableElement) EcoreUtil.resolve(getEObjectOrProxy(), context);
	String typeIdent = first instanceof Type ? "type" : "variable";
	TModule module = (TModule) first.eContainer();
	typeListStr.append(module.getQualifiedName());
	Set<IdentifiableElement> uniqueTypes = Sets.newLinkedHashSet(elements);
	uniqueTypes.remove(first);
	Iterator<IdentifiableElement> iter = uniqueTypes.iterator();
	while (iter.hasNext()) {
		IdentifiableElement type = iter.next();
		if (iter.hasNext()) {
			typeListStr.append(", ");
		} else {
			typeListStr.append(" and ");
		}
		typeListStr.append(((TModule) type.eContainer()).getQualifiedName());
	}
	if (this.issueCode == IssueCodes.IMP_AMBIGUOUS_WILDCARD) {
		return IssueCodes.getMessageForIMP_AMBIGUOUS_WILDCARD(typeIdent, getName(), typeListStr.toString());
	} else if (this.issueCode == IssueCodes.IMP_AMBIGUOUS) {
		return IssueCodes.getMessageForIMP_AMBIGUOUS(typeIdent, getName(), typeListStr.toString());
	} else if (this.issueCode == IssueCodes.IMP_DUPLICATE_NAMESPACE) {
		return IssueCodes.getMessageForIMP_DUPLICATE_NAMESPACE(getName(), "stub");
	}
	return "Unknown ambiguous import issue: " + this.issueCode + " for " + context + ".";
}
 
Example 5
Project: neoscada   File: ScriptReferenceImpl.java   View Source Code Vote up 5 votes
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * 
 * @generated NOT
 */
@Override
public String getLanguage ()
{
    if ( this.reference != null )
    {
        final Script ref = (Script)EcoreUtil.resolve ( this.reference, this );
        return ref.getLanguage ();
    }
    else
    {
        return null;
    }
}
 
Example 6
Project: neoscada   File: ScriptReferenceImpl.java   View Source Code Vote up 5 votes
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * 
 * @generated NOT
 */
@Override
public String getSource ()
{
    if ( this.reference != null )
    {
        final Script ref = (Script)EcoreUtil.resolve ( this.reference, this );
        return ref.getSource ();
    }
    else
    {
        return null;
    }
}
 
Example 7
Project: lcdsl   File: LcDslHelper.java   View Source Code Vote up 5 votes
/**
 * Finds a launch configuration with the given name from the current EMF index. The index contains all
 * launch configurations from projects that have the Xtext nature. Launch configurations from projects
 * without the Xtext nature are not found. Use {@link #findLaunchConfig(String, IFile)} in this case.
 *
 * @param name the name of the configuration to find
 * @return the configuration or <code>null</code>
 */
public LaunchConfig findLaunchConfig(String name) {
    Iterable<IEObjectDescription> indexed = index.getExportedObjectsByType(LcDslPackage.eINSTANCE.getLaunchConfig());

    for (IEObjectDescription o : indexed) {
        if (o.getName().getLastSegment().equals(name)) {
            EObject obj = EcoreUtil.resolve(o.getEObjectOrProxy(), rsProvider.get());
            EcoreUtil.resolveAll(obj);
            return (LaunchConfig) obj;
        }
    }
    return null;
}
 
Example 8
Project: n4js   File: N4JSResource.java   View Source Code Vote up 4 votes
/**
 * Invoked from {@link ProxyResolvingEObjectImpl#eResolveProxy(InternalEObject)} whenever an EMF proxy inside an
 * N4JSResource is being resolved. The receiving resource is the resource containing the proxy, not necessarily the
 * resource the proxy points to.
 *
 * @param proxy
 *            the proxy to resolve.
 * @param objectContext
 *            the {@code EObject} contained in this resource that holds the given proxy.
 */
@Override
public EObject doResolveProxy(InternalEObject proxy, EObject objectContext) {
	// step 1: trigger post processing of the resource containing 'proxy' iff it is the first proxy being resolved
	// (if another proxy has been resolved before, post processing will already be running/completed, and in that
	// case the next line will simply do nothing, cf. #performPostProcessing())
	this.performPostProcessing();
	// step 2: now turn to resolving the proxy at hand
	final URI targetUri = proxy.eProxyURI();
	final boolean isLazyLinkingProxy = getEncoder().isCrossLinkFragment(this, targetUri.fragment());
	if (!isLazyLinkingProxy) {
		// we have an ordinary EMF proxy (not one of Xtext's lazy linking proxies) ...
		final ResourceSet resSet = getResourceSet();
		final URI targetResourceUri = targetUri.trimFragment();
		final String targetFileExt = targetResourceUri.fileExtension();
		if (N4JSGlobals.N4JS_FILE_EXTENSION.equals(targetFileExt)
				|| N4JSGlobals.N4JSD_FILE_EXTENSION.equals(targetFileExt)
				|| N4JSGlobals.N4JSX_FILE_EXTENSION.equals(targetFileExt)) {

			// proxy is pointing into an .n4js or .n4jsd file ...
			// check if we can work with the TModule from the index or if it is mandatory to load from source
			final boolean canLoadFromDescription = !targetUri.fragment().startsWith("/0")
					&& canLoadFromDescriptionHelper.canLoadFromDescription(targetResourceUri, getResourceSet());
			if (canLoadFromDescription) {

				final String targetFragment = targetUri.fragment();
				final Resource targetResource = resSet.getResource(targetResourceUri, false);

				// special handling #1:
				// if targetResource is not loaded yet, try to load it from index first
				if (targetResource == null) {
					if (targetFragment != null
							&& (targetFragment.equals("/1") || targetFragment.startsWith("/1/"))) {
						// uri points to a TModule element in a resource not yet contained in our resource set
						// --> try to load target resource from index
						final IResourceDescriptions index = n4jsCore.getXtextIndex(resSet);
						final IResourceDescription resDesc = index.getResourceDescription(targetResourceUri);
						if (resDesc != null) {
							// next line will add the new resource to resSet.resources
							n4jsCore.loadModuleFromIndex(resSet, resDesc, false);
						}
					}
				}
			}
			// standard behavior:
			// obtain target EObject from targetResource in the usual way
			// (might load targetResource from disk if it wasn't loaded from index above)
			final EObject targetObject = resSet.getEObject(targetUri, true);
			// special handling #2:
			// if targetResource exists, make sure it is post-processed *iff* this resource is post-processed
			// (only relevant in case targetResource wasn't loaded from index, because after loading from index it
			// is always marked as fullyPostProcessed==true)
			if (targetObject != null && (this.isPostProcessing() || this.isFullyProcessed())) {
				final Resource targetResource2 = targetObject.eResource();
				if (targetResource2 instanceof N4JSResource) {
					// no harm done, if already running/completed
					((N4JSResource) targetResource2).performPostProcessing();
				}
			}
			// return resolved target object
			return targetObject != null ? targetObject : proxy; // important: return proxy itself if unresolvable!
		}
	}
	// we will get here if
	// a) we have an Xtext lazy linking proxy or
	// b) targetUri points to an n4ts resource or some other, non-N4JS resource
	// --> above special handling not required, so just apply EMF's default resolution behavior
	return EcoreUtil.resolve(proxy, this);
}