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

The following examples show how to use org.eclipse.emf.ecore.util.EcoreUtil#resolve() . 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: N4JSReplacementTextApplier.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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
Source File: AnonymousClassUtil.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public JvmDeclaredType getSuperTypeNonResolving(AnonymousClass anonymousClass, IScope typeScope) {
	XConstructorCall constructorCall = anonymousClass.getConstructorCall();
	EObject constructorProxy = (EObject) constructorCall.eGet(XbasePackage.Literals.XCONSTRUCTOR_CALL__CONSTRUCTOR, false);
	IEObjectDescription description = null;
	if (constructorProxy != null) {
		if (!constructorProxy.eIsProxy()) {
			return getSuperType(anonymousClass);
		}
		String fragment = EcoreUtil.getURI(constructorProxy).fragment();
		INode node = uriEncoder.getNode(constructorCall, fragment);
		String name = linkingHelper.getCrossRefNodeAsString(node, true);
		QualifiedName superTypeName = qualifiedNameConverter.toQualifiedName(name);
		description = typeScope.getSingleElement(superTypeName);
	}
	if (description == null || !EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_DECLARED_TYPE, description.getEClass())) {
		description = typeScope.getSingleElement(QualifiedName.create("java", "lang", "Object"));
	}
	if (description != null && EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_DECLARED_TYPE, description.getEClass())) {
		JvmDeclaredType type = (JvmDeclaredType) description.getEObjectOrProxy();
		if (!type.eIsProxy())
			return type;
		return (JvmDeclaredType) EcoreUtil.resolve(type, anonymousClass);
	}
	return null;
}
 
Example 3
Source File: CheckResourceUtil.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets all available grammars.
 * <p>
 * The result contains no null entries.
 * </p>
 *
 * @return an iterator over all grammars in the workspace followed by all those in the registry.
 */
private Iterable<Grammar> allGrammars() {
  final ResourceSet resourceSetForResolve = new ResourceSetImpl();
  final Function<IEObjectDescription, Grammar> description2GrammarTransform = new Function<IEObjectDescription, Grammar>() {
    @Override
    public Grammar apply(final IEObjectDescription desc) {
      EObject obj = desc.getEObjectOrProxy();
      if (obj != null && obj.eIsProxy()) {
        obj = EcoreUtil.resolve(obj, resourceSetForResolve);
      }
      if (obj instanceof Grammar && !obj.eIsProxy()) {
        return (Grammar) obj;
      } else {
        return null;
      }

    }
  };

  final Iterable<IEObjectDescription> grammarDescriptorsFromIndex = Access.getIResourceDescriptions().get().getExportedObjectsByType(XtextPackage.Literals.GRAMMAR);
  return Iterables.concat(Iterables.filter(Iterables.transform(grammarDescriptorsFromIndex, description2GrammarTransform), Predicates.notNull()), allGrammarsFromRegistry());
}
 
Example 4
Source File: XtextRenameContextFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public IRenameElementContext createRenameElementContext(EObject targetElement, XtextEditor editor,
		ITextSelection selection, XtextResource resource) {
	if(targetElement instanceof AbstractRule) {
		AbstractRule targetRule = (AbstractRule) targetElement;
		List<IEObjectDescription> overriddenRules = ruleOverrideUtil.getOverriddenRules(targetRule);
		if(!overriddenRules.isEmpty()) {
			IEObjectDescription topMostSuperRule = overriddenRules.get(overriddenRules.size()-1);
			StringBuilder builder = new StringBuilder();
			builder
				.append("Rule '")
				.append(targetRule.getName())
				.append("' overrides a rule from a super grammar.\n")
				.append("Rename super rule instead?");
			boolean isRenameSuperRule = MessageDialog.openQuestion(Display.getCurrent().getActiveShell(), "Overriding Rule", builder.toString());
			if(isRenameSuperRule) {
				EObject newTarget = EcoreUtil.resolve(topMostSuperRule.getEObjectOrProxy(), targetElement.eResource().getResourceSet());
				return super.createRenameElementContext(newTarget, editor, selection, resource);
			}
		} 
	}
	return super.createRenameElementContext(targetElement, editor, selection, resource);
}
 
Example 5
Source File: TypeResource.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public EObject resolveJavaObjectURIProxy(InternalEObject proxy, EObject sender) {
	final URI proxyURI = proxy.eProxyURI();
       if (proxyURI != null && URIHelperConstants.PROTOCOL.equals(proxyURI.scheme())) {
           if ("Objects".equals(proxyURI.segment(0))) {
			if (indexedJvmTypeAccess != null) {
				try {
					EObject result = indexedJvmTypeAccess.getIndexedJvmType(proxy.eProxyURI(), getResourceSet());
					if (result != null) {
						return result;
					}
				} catch(UnknownNestedTypeException e) {
					return proxy;
				}
			}
			return EcoreUtil.resolve(proxy, sender);
           }
       }
       return null;
}
 
Example 6
Source File: IndexedJvmTypeAccess.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Read and resolve the EObject from the given description and navigate to its children according
 * to the given fragment.
 * @since 2.8
 */
protected EObject getAccessibleType(IEObjectDescription description, String fragment, ResourceSet resourceSet) throws UnknownNestedTypeException {
	EObject typeProxy = description.getEObjectOrProxy();
	if (typeProxy.eIsProxy()) {
		typeProxy = EcoreUtil.resolve(typeProxy, resourceSet);
	}
	if (!typeProxy.eIsProxy() && typeProxy instanceof JvmType) {
		if (fragment != null) {
			EObject result = resolveJavaObject((JvmType) typeProxy, fragment);
			if (result != null)
				return result;
		} else
			return typeProxy;
	}
	return null;
}
 
Example 7
Source File: ContainerTypesHelper.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
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 8
Source File: ConstructorTypeScopeWrapper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected EObject getResolvedProxy(IEObjectDescription description) {
	EObject proxy = description.getEObjectOrProxy();
	if (proxy.eIsProxy()) {
		proxy = EcoreUtil.resolve(proxy, context);
	}
	return proxy;
}
 
Example 9
Source File: InferredJvmModelUtil.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns an inferred element of specified name and type for a given source element.
 * 
 * @param sourceElement
 *          a source {@link EObject}, must not be {@code null}
 * @param name
 *          the name of the element, must not be {@code null}
 * @param clazz
 *          the type of the element, must not be {@code null}
 * @return an inferred element, or {@code null} if nothing has been found
 */
public static EObject getInferredElement(final EObject sourceElement, final String name, final Class<? extends EObject> clazz) {
  final Collection<? extends EObject> inferredElements = getInferredElements(sourceElement, clazz);
  EObject target = null;
  for (final EObject object : inferredElements) {
    if (clazz.isAssignableFrom(object.getClass()) && object instanceof JvmIdentifiableElement
        && ((JvmIdentifiableElement) object).getQualifiedName().equals(name)) {
      target = EcoreUtil.resolve(object, sourceElement);
      break;
    }
  }
  return target;
}
 
Example 10
Source File: StatechartEqualityHelper.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean equals(EObject eObject1, EObject eObject2) {
	
	if (eObject1!=null && eObject1.eIsProxy()) {
		EcoreUtil.resolve(eObject1, eObject2.eResource());
	}
	if (eObject2!=null && eObject2.eIsProxy()) {
		EcoreUtil.resolve(eObject2, eObject1.eResource());
	}
	
	return super.equals(eObject1, eObject2);
}
 
Example 11
Source File: Index.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public EObject getModelObject() {
  EObject result = delegate.getEObjectOrProxy();
  if (result != null && result.eIsProxy()) {
    result = EcoreUtil.resolve(result, context);
  }
  return result;
}
 
Example 12
Source File: ScriptReferenceImpl.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * <!-- 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 13
Source File: CrossReferenceSerializer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.11
 */
protected EObject handleProxy(EObject proxy, EObject semanticObject, EReference reference) {
	if (reference != null && reference.isResolveProxies()) {
		if (reference.isMany()) {
			@SuppressWarnings("unchecked")
			EList<? extends EObject> list = (EList<? extends EObject>) semanticObject.eGet(reference);
			int index = list.indexOf(proxy);
			if (index >= 0)
				return list.get(index);
		} else {
			return (EObject) semanticObject.eGet(reference, true);
		}
	}
	return EcoreUtil.resolve(proxy, semanticObject);
}
 
Example 14
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 15
Source File: CheckCfgTemplateProposalProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adds the populated check configuration.
 *
 * @param templateContext
 *          the template context
 * @param context
 *          the context
 * @param acceptor
 *          the acceptor
 */
@SuppressWarnings("all")
private void addCatalogConfigurations(final TemplateContext templateContext, final ContentAssistContext context, final ITemplateAcceptor acceptor) {
  final String templateName = "Add all registered catalogs"; //$NON-NLS-1$
  final String templateDescription = "configures all missing catalogs"; //$NON-NLS-1$

  final String contextTypeId = templateContext.getContextType().getId();
  if (context.getRootModel() instanceof CheckConfiguration) {
    final CheckConfiguration conf = (CheckConfiguration) context.getRootModel();
    List<IEObjectDescription> allElements = Lists.newArrayList(scopeProvider.getScope(conf, CheckcfgPackage.Literals.CONFIGURED_CATALOG__CATALOG).getAllElements());

    StringBuilder builder = new StringBuilder();
    for (IEObjectDescription description : allElements) {
      if (description.getEObjectOrProxy() instanceof CheckCatalog) {
        CheckCatalog catalog = (CheckCatalog) description.getEObjectOrProxy();
        if (catalog.eIsProxy()) {
          catalog = (CheckCatalog) EcoreUtil.resolve(catalog, conf);
        }
        if (isCatalogConfigured(conf, catalog)) {
          continue;
        } else if (allElements.indexOf(description) > 0) {
          builder.append(Strings.newLine());
        }
        final String catalogName = qualifiedNameValueConverter.toString(description.getQualifiedName().toString());
        builder.append("catalog ").append(catalogName).append(" {}").append(Strings.newLine()); //$NON-NLS-1$ //$NON-NLS-2$
      }

    }

    if (builder.length() > 0) {
      builder.append("${cursor}"); //$NON-NLS-1$
      Template t = new Template(templateName, templateDescription, contextTypeId, builder.toString(), true);
      TemplateProposal tp = createProposal(t, templateContext, context, images.forConfiguredCatalog(), getRelevance(t));
      acceptor.accept(tp);
    }
  }
}
 
Example 16
Source File: TestDiscoveryHelper.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private 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 (TClass) object;
		}
	}
	return null;
}
 
Example 17
Source File: PortableURIs.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected PortableURIs.PortableFragmentDescription createPortableFragmentDescription(IEObjectDescription desc,
		EObject target) {
	EObject possibleContainer = EcoreUtil.resolve(desc.getEObjectOrProxy(), target);
	String fragmentToTarget = getFragment(target, possibleContainer);
	return new PortableURIs.PortableFragmentDescription(desc.getEClass(), desc.getQualifiedName(),
			fragmentToTarget);
}
 
Example 18
Source File: XFunctionTypeRefs.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public static JvmType getType(Class<?> clazz, EObject context) {
	InternalEObject proxy = (InternalEObject) TypesFactory.eINSTANCE.createJvmVoid();
	proxy.eSetProxyURI(computeTypeUri(clazz));
	return (JvmType) EcoreUtil.resolve(proxy, context);
}
 
Example 19
Source File: JdtTypesProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Compute the JVM modifiers that corresponds to the given description.
 *
 * <p>This function fixes the issue related to the missed modifiers given to the content assist.
 *
 * @param context the current content assist context.
 * @param description the description.
 * @return the JVM modifiers.
 * @since 2.11
 */
protected int getDirtyStateModifiers(ContentAssistContext context, IEObjectDescription description) {
	EObject eobject = description.getEObjectOrProxy();
	if (eobject.eIsProxy()) {
		eobject = EcoreUtil.resolve(eobject, context.getResource().getResourceSet());
	}
	int accessModifiers = Flags.AccPublic;
	int otherModifiers = 0;
	if (eobject instanceof JvmMember) {
		final JvmMember member = (JvmMember) eobject;
		switch (member.getVisibility()) {
		case PUBLIC:
			accessModifiers = Flags.AccPublic;
			break;
		case PRIVATE:
			accessModifiers = Flags.AccPrivate;
			break;
		case PROTECTED:
			accessModifiers = Flags.AccProtected;
			break;
		case DEFAULT:
		default:
			accessModifiers = Flags.AccDefault;
			break;
		}
		if (DeprecationUtil.isDeprecated(member)) {
			otherModifiers |= Flags.AccDeprecated;
		}
		if (eobject instanceof JvmDeclaredType) {
			final JvmDeclaredType type = (JvmDeclaredType) eobject;
			if (type.isFinal()) {
				otherModifiers |= Flags.AccFinal;
			}
			if (type.isAbstract()) {
				otherModifiers |= Flags.AccAbstract;
			}
			if (type.isStatic()) {
				otherModifiers |= Flags.AccStatic;
			}
			if (type instanceof JvmEnumerationType) {
                otherModifiers |= Flags.AccEnum;
            } else  if (type instanceof JvmAnnotationType) {
                otherModifiers |= Flags.AccAnnotation;
            } else if (type instanceof JvmGenericType) {
                if (((JvmGenericType) type).isInterface()) {
                    otherModifiers |= Flags.AccInterface;
                }
            }
		}
	}
	return accessModifiers | otherModifiers;
}
 
Example 20
Source File: NotationClipboardOperationHelper.java    From statecharts with Eclipse Public License 1.0 4 votes vote down vote up
protected boolean shouldAllowPaste(
		PasteChildOperation overriddenChildPasteOperation) {
	EObject eObject = overriddenChildPasteOperation.getEObject();
	EObject parentEObject = overriddenChildPasteOperation
			.getParentEObject();
	// RATLC01137919 removed the condition that parentEObject is a diagram
	// to allow paste into diagram elements
	if ((parentEObject instanceof View) && (eObject instanceof View)) {
		EObject semanticChildElement = ((View) eObject).getElement();
		if (semanticChildElement == null || isSubdiagram(eObject, semanticChildElement)) {
			return true;
		}

		// PATCH START
		EObject target = getSemanticPasteTarget((View) eObject,
				(View) overriddenChildPasteOperation.getParentEObject());
		if (target == null) {
			return false;
		}
		// PATCH END

		if (semanticChildElement.eIsProxy()) {
			semanticChildElement = ClipboardSupportUtil.resolve(
					semanticChildElement, overriddenChildPasteOperation
							.getParentPasteProcess()
							.getLoadedIDToEObjectMapCopy());
			if (semanticChildElement.eIsProxy()) {
				semanticChildElement = EcoreUtil.resolve(
						semanticChildElement, getResource(parentEObject));
			}
		}

		EPackage semanticChildEpackage = semanticChildElement.eClass()
				.getEPackage();
		EPackage parentRootContainerEpackage = EcoreUtil
				.getRootContainer(parentEObject).eClass().getEPackage();
		EPackage sematicParentRootContainerEpackage = null;
		EObject sematicParentElement = ((View) parentEObject).getElement();
		if (sematicParentElement != null) {
			sematicParentRootContainerEpackage = EcoreUtil
					.getRootContainer(sematicParentElement).eClass()
					.getEPackage();
		}

		if (parentRootContainerEpackage != NotationPackage.eINSTANCE) {
			if (semanticChildEpackage != parentRootContainerEpackage) {
				return false;
			}
		}

		if ((sematicParentRootContainerEpackage != null)
				&& (sematicParentRootContainerEpackage != NotationPackage.eINSTANCE)) {
			if (semanticChildEpackage != sematicParentRootContainerEpackage) {
				return false;
			}
		}
		return true;
	}
	return false;
}