Java Code Examples for org.eclipse.xtext.EcoreUtil2#isAssignableFrom()

The following examples show how to use org.eclipse.xtext.EcoreUtil2#isAssignableFrom() . 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: PatternAwareEObjectDescriptionLookUp.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Iterable<IEObjectDescription> getExportedObjects(final EClass type, final QualifiedName name, final boolean ignoreCase) {
  QualifiedName lowerCase = name.toLowerCase(); // NOPMD UseLocaleWithCaseConversions not a String!
  QualifiedNameLookup<IEObjectDescription> lookup = getNameToObjectsLookup();
  Collection<IEObjectDescription> values;
  final boolean isPattern = lowerCase instanceof QualifiedNamePattern;
  if (isPattern) {
    values = lookup.get((QualifiedNamePattern) lowerCase, false);
  } else {
    values = lookup.get(lowerCase);
  }
  if (values == null) {
    return Collections.emptyList();
  }
  Predicate<IEObjectDescription> predicate = ignoreCase ? input -> EcoreUtil2.isAssignableFrom(type, input.getEClass())
      : input -> isPattern ? EcoreUtil2.isAssignableFrom(type, input.getEClass()) && ((QualifiedNamePattern) name).matches(name)
          : name.equals(input.getName()) && EcoreUtil2.isAssignableFrom(type, input.getEClass());
  return Collections2.filter(values, predicate);
}
 
Example 2
Source File: UnresolvedFeatureCallTypeAwareMessageProvider.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @Nullable
 */
protected String getTypeName(final EClass c, final EStructuralFeature referingFeature) {
  if ((referingFeature == XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE)) {
    return " to an annotation type";
  }
  if ((c == TypesPackage.Literals.JVM_ENUMERATION_TYPE)) {
    return " to an enum type";
  }
  boolean _isAssignableFrom = EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_TYPE, c);
  if (_isAssignableFrom) {
    return " to a type";
  }
  if ((c == TypesPackage.Literals.JVM_OPERATION)) {
    return " to an operation";
  }
  return "";
}
 
Example 3
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 4
Source File: AbstractElementFinder.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public List<CrossReference> findCrossReferences(EClassifier... targetEClassifiers) {
	Set<EClassifier> classifiers = new HashSet<EClassifier>(Arrays.asList(targetEClassifiers));
	Collection<EClass> classes = Lists.newArrayList(Iterables.filter(classifiers, EClass.class));
	ArrayList<CrossReference> r = new ArrayList<CrossReference>();
	for (AbstractRule ar : getRules()) {
		TreeIterator<EObject> i = ar.eAllContents();
		while (i.hasNext()) {
			EObject o = i.next();
			if (o instanceof CrossReference) {
				CrossReference c = (CrossReference) o;
				if (classifiers.contains(c.getType().getClassifier()))
					r.add(c);
				else if (c.getType().getClassifier() instanceof EClass)
					for (EClass cls : classes)
						if (EcoreUtil2.isAssignableFrom(cls,(EClass) c.getType().getClassifier())) {
							r.add(c);
							break;
						}
				i.prune();
			}
		}
	}
	return r;

}
 
Example 5
Source File: LocalUniqueNameContext.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Iterable<IEObjectDescription> getExportedObjects(EClass type, QualifiedName name, boolean ignoreCase) {
	Preconditions.checkArgument(!ignoreCase);
	Object result = index.get(name.getFirstSegment());
	if (result == null) {
		return Collections.emptyList();
	}
	if (result instanceof IEObjectDescription) {
		if (EcoreUtil2.isAssignableFrom(type, ((IEObjectDescription) result).getEClass())) {
			return Collections.singletonList((IEObjectDescription)result);
		}
		return Collections.emptyList();
	}
	@SuppressWarnings("unchecked")
	List<IEObjectDescription> casted = (List<IEObjectDescription>) result;
	if (type == EcorePackage.Literals.EOBJECT) {
		return casted;
	}
	return FluentIterable.from(casted).filter(it -> EcoreUtil2.isAssignableFrom(type, it.getEClass()));
}
 
Example 6
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 7
Source File: GamlScopeProvider.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Iterable<IEObjectDescription> getExportedObjects(final EClass type, final QualifiedName name,
		final boolean ignoreCase) {
	if (nameToObjects == null) { return Collections.emptyList(); }
	if (nameToObjects.containsKey(name)) {
		for (final IEObjectDescription desc : nameToObjects.get(name)) {
			if (EcoreUtil2.isAssignableFrom(type, desc.getEClass())) { return Collections.singleton(desc); }
		}
	}
	return Collections.emptyList();
}
 
Example 8
Source File: TargetURIKey.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param eReferenceType
 *            the type of the reference that is inspected.
 * @return <code>true</code> if this set contains a subtype of the given eReferenceType.
 */
public boolean isEReferenceTypeApplicable(EClass eReferenceType) {
	if (applicableTypes.isEmpty())
		return true;
	for (EClass applicableType : applicableTypes) {
		if (EcoreUtil2.isAssignableFrom(eReferenceType, applicableType)) {
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: AssignmentFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected Set<AbstractElement> findValidAssignmentsForCrossRef(EObject semanticObj,
		Iterable<AbstractElement> assignedElements, EObject value, INode node) {
	Set<AbstractElement> result = Sets.newLinkedHashSet();
	for (AbstractElement ass : assignedElements) {
		CrossReference crossref = GrammarUtil.containingCrossReference(ass);
		EReference eref = GrammarUtil.getReference(crossref, semanticObj.eClass());
		if (EcoreUtil2.isAssignableFrom(eref.getEReferenceType(), value.eClass())
				&& crossRefSerializer.isValid(semanticObj, crossref, value, node, null))
			result.add(ass);
	}
	return result;
}
 
Example 10
Source File: XbaseReferenceUpdater.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected String resolveNameConflict(EObject referringElement, EReference reference, EObject newTargetElement, IRefactoringUpdateAcceptor updateAcceptor) {
	if (EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_IDENTIFIABLE_ELEMENT, reference.getEReferenceType())) {
		if (newTargetElement instanceof JvmType) {
			JvmType type = (JvmType) newTargetElement;
			
			if (canLinkJvmType(referringElement, type)) {
				return toString(qualifiedNameProvider.getFullyQualifiedName(type));
			}
		}
		if (newTargetElement instanceof JvmFeature) {
			JvmFeature feature = (JvmFeature) newTargetElement;
			if (feature.isStatic() && !isStaticExtensionFeatureCall(referringElement, reference, newTargetElement)) {
				JvmDeclaredType declaringType = feature.getDeclaringType();
				
				if (canLinkJvmType(referringElement, declaringType)) {
					JvmDeclaredType parentType = declaringType;
					
					List<String> segments = new LinkedList<String>();
					segments.add(feature.getSimpleName());
					segments.add(0, parentType.getSimpleName());
					while (!hasImportedType(updateAcceptor, parentType)) {
						parentType = parentType.getDeclaringType();
						if (parentType == null) {
							return toString(qualifiedNameProvider.getFullyQualifiedName(feature));
						}
						segments.add(0, parentType.getSimpleName());
					}
					return toString(QualifiedName.create(segments));
				}
			}
		}
	}
	return super.resolveNameConflict(referringElement, reference, newTargetElement, updateAcceptor);
}
 
Example 11
Source File: XbaseReferenceProposalCreator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Function<IEObjectDescription, ICompletionProposal> getWrappedFactory(EObject model, EReference reference,
		final Function<IEObjectDescription, ICompletionProposal> proposalFactory) {
	if (!TypesPackage.Literals.JVM_TYPE.isSuperTypeOf(getEReferenceType(model, reference))) {
		return new Function<IEObjectDescription, ICompletionProposal>() {

			@Override
			public ICompletionProposal apply(IEObjectDescription from) {
				ICompletionProposal result = proposalFactory.apply(from);
				if (EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_FEATURE, from.getEClass())) {
					if (result instanceof ConfigurableCompletionProposal) {
						EObject eObjectOrProxy = from.getEObjectOrProxy();
						if (eObjectOrProxy.eIsProxy()) {
							Image img = null;
							if (EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_CONSTRUCTOR, from.getEClass())) {
								img = computeConstructorImage(false, false, Flags.AccPublic, JavaElementImageDescriptor.CONSTRUCTOR);
							} else if (EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_OPERATION, from.getEClass())) {
								img = computeMethodImage(false, Flags.AccPublic, 0);
							} else if (EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_FIELD, from.getEClass())) {
								img = computeFieldImage(false, Flags.AccPublic, 0);
							}
							if (img != null) {
								((ConfigurableCompletionProposal) result).setImage(img);
							}
						} else {
							((ConfigurableCompletionProposal) result).setImage(computeImage((JvmFeature)eObjectOrProxy));
						}
					}
				} 
				else if (from instanceof SimpleIdentifiableElementDescription && isLocalVarOrFormalParameter(from)){
					if (result instanceof ConfigurableCompletionProposal) {
						((ConfigurableCompletionProposal) result).setImage(JavaPlugin.getImageDescriptorRegistry().get(JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE));
					}
				}
				return result;
			}
		};
	}
	return super.getWrappedFactory(model, reference, proposalFactory);
}
 
Example 12
Source File: TypesAwareDefaultGlobalScopeProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IScope getScope(Resource resource, EReference reference, Predicate<IEObjectDescription> filter) {
	EClass referenceType = getEReferenceType(resource, reference);
	if (EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_TYPE, referenceType)) {
		return typeScopeProvider.getScope(resource, reference, filter);
	}
	if (EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_CONSTRUCTOR, referenceType)) {
		return typeScopeProvider.getScope(resource, reference, filter);
	}
	return super.getScope(resource, reference, filter);
}
 
Example 13
Source File: XbaseReferenceUpdater.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isReferenceToJvmType(EObject newTargetElement, EReference reference) {
	return newTargetElement instanceof JvmType && EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_TYPE, reference.getEReferenceType());
}
 
Example 14
Source File: LazyLinkingResource.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.4
 */
protected EObject getEObject(String uriFragment, Triple<EObject, EReference, INode> triple) throws AssertionError {
	cyclicLinkingDetectionCounter++;
	if (cyclicLinkingDetectionCounter > cyclicLinkingDectectionCounterLimit) {
		if (!resolving.add(triple)) {
			return handleCyclicResolution(triple);
		}
	}
	try {
		Set<String> unresolveableProxies = getUnresolvableURIFragments();
		if (unresolveableProxies.contains(uriFragment))
			return null;
		EReference reference = triple.getSecond();
		try {
			List<EObject> linkedObjects = getLinkingService().getLinkedObjects(
					triple.getFirst(), 
					reference,
					triple.getThird());

			if (linkedObjects.isEmpty()) {
				if (isUnresolveableProxyCacheable(triple))
					unresolveableProxies.add(uriFragment);
				createAndAddDiagnostic(triple);
				return null;
			}
			if (linkedObjects.size() > 1)
				throw new IllegalStateException("linkingService returned more than one object for fragment "
						+ uriFragment);
			EObject result = linkedObjects.get(0);
			if (!EcoreUtil2.isAssignableFrom(reference.getEReferenceType(), result.eClass())) {
				log.error("An element of type " + result.getClass().getName()
						+ " is not assignable to the reference " + reference.getEContainingClass().getName()
						+ "." + reference.getName());
				if (isUnresolveableProxyCacheable(triple))
					unresolveableProxies.add(uriFragment);
				createAndAddDiagnostic(triple);
				return null;
			}
			// remove previously added error markers, since everything should be fine now
			unresolveableProxies.remove(uriFragment);
			removeDiagnostic(triple);
			return result;
		} catch (CyclicLinkingException e) {
			if (e.triple.equals(triple)) {
				log.error(e.getMessage(), e);
				if (isUnresolveableProxyCacheable(triple))
					unresolveableProxies.add(uriFragment);
				createAndAddDiagnostic(triple);
				return null;
			} else {
				throw e;
			}
		}
	} catch (IllegalNodeException ex) {
		createAndAddDiagnostic(triple, ex);
		return null;
	} finally {
		if (cyclicLinkingDetectionCounter > cyclicLinkingDectectionCounterLimit) {
			resolving.remove(triple);
		}
		cyclicLinkingDetectionCounter--;
	}
}
 
Example 15
Source File: LazyLinker.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private boolean isInstantiatableSubType(EClass c, EClass superType) {
	return !c.isAbstract() && !c.isInterface() && EcoreUtil2.isAssignableFrom(superType, c);
}
 
Example 16
Source File: AbstractHierarchyBuilder.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isAssignable(EClass superType, EClassifier type) {
	if (type instanceof EClass) {
		return EcoreUtil2.isAssignableFrom(superType, ((EClass) type));
	}
	return false;
}
 
Example 17
Source File: UserDataAwareScope.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
private IEObjectDescription resolve(IEObjectDescription original) {
	if (original instanceof ResolvedDescription) {
		return original;
	}
	if (original != null && original.getEObjectOrProxy().eIsProxy()
			&& EcoreUtil2.isAssignableFrom(type, original.getEClass())) {
		final URI objectURI = original.getEObjectURI();
		final URI resourceURI = objectURI.trimFragment();
		Resource resource = resourceSet.getResource(resourceURI, false);
		if (resource != null && resource.isLoaded()) {
			return original;
		}
		final boolean mustLoadFromSource = canLoadFromDescriptionHelper.mustLoadFromSource(resourceURI,
				resourceSet);
		resource = resourceSet.getResource(resourceURI, mustLoadFromSource);
		if (resource != null && resource.isLoaded()) {
			return original;
		}
		if (mustLoadFromSource) {
			// error case: forced loading failed
			// --> still avoid loading from index; instead simply return 'original' as in other error cases
			return original;
		}
		if (resource == null) {
			resource = canLoadFromDescriptionHelper.createResource(resourceSet, resourceURI);
		}
		if (resource instanceof N4JSResource) {
			if (resource.getContents().isEmpty()) {
				N4JSResource casted = (N4JSResource) resource;
				IResourceDescription resourceDescription = container.getResourceDescription(resourceURI);
				if (resourceDescription != null) {
					if (casted.isLoaded()) {
						// LiveShadowingResourceDescriptions (part of Xtext's rename refactoring)
						// will load that stuff on #getResourceDescription
						return original;
					}
					try {
						if (!casted.loadFromDescription(resourceDescription)) {
							return original;
						}
					} catch (Exception e) {
						casted.unload();
						return original;
					}
				} else {
					return original;
				}
			}
			// resolveProxy is implemented recursively thus we have to avoid
			// that here by decorating the
			// description and return the resolved instance instead
			EObject resolved = resolveObject(objectURI, resource);
			if (resolved == null) {
				return original;
			}
			return new ResolvedDescription(resolved, original);
		}
	}
	return original;
}
 
Example 18
Source File: N4JSLinker.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isInstantiatableSubType(EClass c, EClass superType) {
	return !c.isAbstract() && !c.isInterface() && EcoreUtil2.isAssignableFrom(superType, c);
}
 
Example 19
Source File: EClasses.java    From dsl-devkit with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Determine whether a type is an instantiable subtype of another type.
 *
 * @param c
 *          EClass to check.
 * @param superType
 *          of which c should be a subtype.
 * @return {@code true}, if c is instantiable and assignment-compatible with {@code superType}.
 */
private static boolean isInstantiatableSubType(final EClass c, final EClass superType) {
  return !c.isAbstract() && !c.isInterface() && EcoreUtil2.isAssignableFrom(superType, c);
}