Java Code Examples for org.eclipse.emf.ecore.EReference#getEReferenceType()

The following examples show how to use org.eclipse.emf.ecore.EReference#getEReferenceType() . 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: XbaseHighlightingCalculator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void computeReferencedJvmTypeHighlighting(IHighlightedPositionAcceptor acceptor, EObject referencer,
		CancelIndicator cancelIndicator) {
	for (EReference reference : referencer.eClass().getEAllReferences()) {
		EClass referencedType = reference.getEReferenceType();
		if (EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_TYPE, referencedType)) {
			List<EObject> referencedObjects = EcoreUtil2.getAllReferencedObjects(referencer, reference);
			if (referencedObjects.size() > 0)
				operationCanceledManager.checkCanceled(cancelIndicator);
			for (EObject referencedObject : referencedObjects) {
				EObject resolvedReferencedObject = EcoreUtil.resolve(referencedObject, referencer);
				if (resolvedReferencedObject != null && !resolvedReferencedObject.eIsProxy()) {
					highlightReferenceJvmType(acceptor, referencer, reference, resolvedReferencedObject);
				}
			}
		}
	}
}
 
Example 2
Source File: NotationClipboardOperationHelper.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Customized Method to find the semantic target which should contain the
 * copied elements.
 * 
 * @param view
 * @param container
 * @return the semantic target.
 */
public static EObject getSemanticPasteTarget(View view, View container) {
	EObject copiedSemanticObject = view.getElement();
	EObject semanticTarget = container.getElement();
	if (copiedSemanticObject instanceof Transition) {
		semanticTarget = copiedSemanticObject.eContainer();
	}
	EList<EReference> eAllReferences = semanticTarget.eClass()
			.getEAllReferences();
	for (EReference eReference : eAllReferences) {
		EClass eReferenceType = eReference.getEReferenceType();
		if (eReference.isContainment()
				&& eReferenceType.isSuperTypeOf(copiedSemanticObject
						.eClass())) {
			return semanticTarget;
		}
	}
	return null;
}
 
Example 3
Source File: ErrorAwareLinkingService.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException {
	final EClass requiredType = ref.getEReferenceType();
	if (requiredType == null)
		return Collections.<EObject> emptyList();

	final String crossRefString = getCrossRefNodeAsString(context, ref, node);
	if (crossRefString != null && !crossRefString.equals("")) {
		final IScope scope = getScope(context, ref);
		QualifiedName qualifiedLinkName = qualifiedNameConverter.toQualifiedName(crossRefString);
		IEObjectDescription eObjectDescription = scope.getSingleElement(qualifiedLinkName);
		IEObjectDescriptionWithError errorDescr;
		Resource resource = context.eResource();
		if (resource != null
				&& (errorDescr = IEObjectDescriptionWithError
						.getDescriptionWithError(eObjectDescription)) != null
				// isNoValidate traverses the file system so it should be the last part of the check
				&& !n4jsCore.isNoValidate(resource.getURI())) {
			addError(context, node, errorDescr);
		} else if (eObjectDescription instanceof UnresolvableObjectDescription) {
			return Collections.<EObject> singletonList((EObject) context.eGet(ref, false));
		}

		if (eObjectDescription != null) {
			EObject candidate = eObjectDescription.getEObjectOrProxy();
			if (!candidate.eIsProxy() && candidate.eResource() == null) {
				// Error is necessary since EMF catches all exceptions in EcoreUtil#resolve
				throw new AssertionError("Found an instance without resource and without URI");
			}

			// if supported, mark object description as used
			if (eObjectDescription instanceof IUsageAwareEObjectDescription) {
				((IUsageAwareEObjectDescription) eObjectDescription).markAsUsed();
			}

			return Collections.singletonList(candidate);
		}
	}
	return Collections.emptyList();
}
 
Example 4
Source File: ScopeProviderAccess.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns a bunch of descriptions most of which are actually {@link IIdentifiableElementDescription describing identifiables}. 
 * The provided iterable is never empty but it may contain a single {@link ErrorDescription error description}.
 * 
 * @return the available descriptions.
 */
public Iterable<IEObjectDescription> getCandidateDescriptions(XExpression expression, EReference reference, /* @Nullable */ EObject toBeLinked,
		IFeatureScopeSession session, IResolvedTypes types) throws IllegalNodeException {
	if (toBeLinked == null) {
		return Collections.emptyList();
	}
	if (!toBeLinked.eIsProxy()) {
		throw new IllegalStateException(expression + " was already linked to " + toBeLinked);
	}
	URI uri = EcoreUtil.getURI(toBeLinked);
	String fragment = uri.fragment();
	if (encoder.isCrossLinkFragment(expression.eResource(), fragment)) {
		INode node = encoder.getNode(expression, fragment);
		final EClass requiredType = reference.getEReferenceType();
		if (requiredType == null)
			return Collections.emptyList();

		final String crossRefString = linkingHelper.getCrossRefNodeAsString(node, true);
		if (crossRefString != null && !crossRefString.equals("")) {
			QualifiedName qualifiedLinkName = qualifiedNameConverter.toQualifiedName(crossRefString);
			if (!qualifiedLinkName.isEmpty()) {
				final IScope scope = session.getScope(expression, reference, types);
				Iterable<IEObjectDescription> descriptions = scope.getElements(qualifiedLinkName);
				if (Iterables.isEmpty(descriptions)) {
					INode errorNode = getErrorNode(expression, node);
					if (errorNode != node) {
						qualifiedLinkName = getErrorName(errorNode);
					}
					return Collections.<IEObjectDescription>singletonList(new ErrorDescription(getErrorNode(expression, node), qualifiedLinkName));
				}
				return descriptions;
			} else {
				return Collections.<IEObjectDescription>singletonList(new ErrorDescription(null /* followUp problem */));
			}
		}
		return Collections.emptyList();
	} else {
		throw new IllegalStateException(expression + " uses unsupported uri fragment " + uri);
	}
}
 
Example 5
Source File: Linker.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected EClass getProxyType(EObject obj, EReference eRef) {
	EClass referenceType = eRef.getEReferenceType();
	if (referenceType == TypesPackage.Literals.JVM_TYPE 
			|| referenceType == TypesPackage.Literals.JVM_IDENTIFIABLE_ELEMENT)
		return TypesPackage.Literals.JVM_VOID;
	if (referenceType == TypesPackage.Literals.JVM_DECLARED_TYPE)
		return TypesPackage.Literals.JVM_GENERIC_TYPE;
	return referenceType;
}
 
Example 6
Source File: DefaultLinkingService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return the first element returned from the injected {@link IScopeProvider} which matches the text of the passed
 *         {@link INode node}
 */
@Override
public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException {
	final EClass requiredType = ref.getEReferenceType();
	if (requiredType == null) {
		return Collections.<EObject>emptyList();
	}
	final String crossRefString = getCrossRefNodeAsString(node);
	if (crossRefString == null || crossRefString.equals("")) {
		return Collections.<EObject>emptyList();
	}
	if (logger.isDebugEnabled()) {
		logger.debug("before getLinkedObjects: node: '" + crossRefString + "'");
	}
	final IScope scope = getScope(context, ref);
	if (scope == null) {
		throw new AssertionError(
				"Scope provider " + scopeProvider.getClass().getName() + " must not return null for context "
						+ context + ", reference " + ref + "! Consider to return IScope.NULLSCOPE instead.");
	}
	final QualifiedName qualifiedLinkName = qualifiedNameConverter.toQualifiedName(crossRefString);
	final IEObjectDescription eObjectDescription = scope.getSingleElement(qualifiedLinkName);
	if (logger.isDebugEnabled()) {
		logger.debug("after getLinkedObjects: node: '" + crossRefString + "' result: " + eObjectDescription);
	}
	if (eObjectDescription == null) {
		return Collections.emptyList();
	}
	final EObject result = eObjectDescription.getEObjectOrProxy();
	return Collections.singletonList(result);
}
 
Example 7
Source File: EcoreGenericsUtil.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public EClass getReferenceType(EReference reference, EClass context) {
	EGenericType genericType = reference.getEGenericType();
	if (genericType == null) {
		return reference.getEReferenceType();
	}
	EGenericType boundGenericType = getBoundGenericType(genericType, context);
	if (boundGenericType.getEClassifier() == null) {
		throw new IllegalStateException("Either typeParameter or eRawType must be set in EGenericType "
				+ genericType);
	}
	return (EClass) boundGenericType.getEClassifier();
}
 
Example 8
Source File: GeneratorUtil.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Internal helper method for {@link #canContain(EClass, Set)}.
 *
 * @param eClass
 *          EClass to check if it could be the container
 * @param candidates
 *          other potential contained EClasses
 * @param instantiatedTypes
 *          all types instantiated by the Xtext grammar
 * @param visited
 *          set of visited EClasses (to avoid endless recursion)
 * @return true if <code>eClass</code> could be the direct or indirect container of any of the given other EClasses
 */
private static boolean internalCanContain(final EClass eClass, final Set<EClass> candidates, final Set<EClass> instantiatedTypes, final Set<EClass> visited) {
  if (!visited.add(eClass)) {
    return false;
  } else if (EcorePackage.Literals.EOBJECT == eClass) {
    return true;
  }

  // check containment references
  for (EReference containment : eClass.getEAllContainments()) {
    if (!containment.isContainment() || containment.isTransient()) {
      // TODO check if it is always correct to skip transient containments
      continue;
    }

    EClass containmentType = containment.getEReferenceType();
    if (candidates.contains(containmentType)) {
      return true;
    } else {
      for (EClass candidate : candidates) {
        if (containmentType.isSuperTypeOf(candidate)) {
          return true;
        }
      }
      if (internalCanContain(containmentType, candidates, instantiatedTypes, visited)) {
        return true;
      }
    }
  }

  // check subtypes
  for (EClass subtype : EClasses.findInstantiableCompatibleTypes(eClass, instantiatedTypes)) {
    if (internalCanContain(subtype, candidates, instantiatedTypes, visited)) {
      return true;
    }
  }

  return false;
}
 
Example 9
Source File: LinkingService.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public List<EObject> getLinkedObjects(final EObject context, final EReference ref, final INode node) {
  final EClass requiredType = ref.getEReferenceType();
  if (requiredType == null) {
    return Collections.emptyList();
  }

  final String linkName = getCrossRefNodeAsString(node);
  if (linkName != null && !linkName.isEmpty()) {
    final QualifiedName qualifiedLinkName = qualifiedNameConverter.toQualifiedName(linkName);
    final IEObjectDescription desc = getSingleElement(context, ref, qualifiedLinkName);
    final EObject target = desc == null ? null : getEObjectOrProxy(context, desc, ref);

    if (target != null) {
      if (isImportRequired(context, ref, target)) {
        if (target.eIsProxy()) {
          importObject(context, desc, ref.getEReferenceType());
        } else {
          importObject(context, target, ref.getEReferenceType());
        }
      }
      return Collections.singletonList(target);
    }

    if (doRegisterUnresolvedReference(context, ref, node)) {
      registerUnresolvedReference(context, linkName, requiredType);
    }
  }
  return Collections.emptyList();

}
 
Example 10
Source File: OperationOverloadingLinkingService.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
public List<EObject> getLinkedOperation(ArgumentExpression context, EReference ref, INode node) {
	final EClass requiredType = ref.getEReferenceType();
	if (requiredType == null) {
		return Collections.<EObject>emptyList();
	}
	final String crossRefString = getCrossRefNodeAsString(node);
	if (crossRefString == null || crossRefString.equals("")) {
		return Collections.<EObject>emptyList();
	}
	final IScope scope = getScope(context, ref);
	final QualifiedName qualifiedLinkName = qualifiedNameConverter.toQualifiedName(crossRefString);
	// Adoption to super class implementation here to return multi elements
	final Iterable<IEObjectDescription> eObjectDescription = scope.getElements(qualifiedLinkName);
	int size = Iterables.size(eObjectDescription);
	if (size == 0)
		return Collections.emptyList();
	if (size == 1)
		return Collections.singletonList(Iterables.getFirst(eObjectDescription, null).getEObjectOrProxy());
	// Two operation with same name found here
	List<IEObjectDescription> candidates = new ArrayList<>();
	for (IEObjectDescription currentDescription : eObjectDescription) {
		if (currentDescription.getEClass().isSuperTypeOf(TypesPackage.Literals.OPERATION)) {
			candidates.add(currentDescription);
		}
	}
	Optional<Operation> operation = operationsLinker.linkOperation(candidates, context);
	if (operation.isPresent()) {
		return Collections.singletonList(operation.get());
	}
	//Link to first operation to get parameter errors instead of linking errors
	return Collections.singletonList(Iterables.getFirst(eObjectDescription, null).getEObjectOrProxy());
}
 
Example 11
Source File: XbaseBatchScopeProvider.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean isConstructorCallScope(EReference reference) {
	return reference.getEReferenceType() == TypesPackage.Literals.JVM_CONSTRUCTOR;
}
 
Example 12
Source File: TypesAwareDefaultGlobalScopeProvider.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected EClass getEReferenceType(Resource resource, EReference reference) {
	return reference.getEReferenceType();
}
 
Example 13
Source File: TypeAwareReferenceProposalCreator.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected EClass getEReferenceType(EObject context, EReference reference) {
	return reference.getEReferenceType();
}
 
Example 14
Source File: AbstractPolymorphicScopeProvider.java    From dsl-devkit with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Determines name functions to be used for scopes of a given context reference. Implementation delegates to {@link #getNameFunctions(EClass)}.
 *
 * @param ref
 *          context reference
 * @return name functions
 */
public Iterable<INameFunction> getNameFunctions(final EReference ref) {
  final EClass type = ref.getEReferenceType();
  return getNameFunctions(type);
}