use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.
the class PatternAwareEObjectDescriptionLookUp method getExportedObjects.
@Override
public Iterable<IEObjectDescription> getExportedObjects(final EClass type, final QualifiedName name, final boolean ignoreCase) {
if (Iterables.isEmpty(getExportedObjects())) {
return Collections.emptyList();
}
// NOPMD UseLocaleWithCaseConversions not a String!
QualifiedName lowerCase = name.toLowerCase();
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 ? new Predicate<IEObjectDescription>() {
@Override
public boolean apply(final IEObjectDescription input) {
return EcoreUtil2.isAssignableFrom(type, input.getEClass());
}
} : new Predicate<IEObjectDescription>() {
@Override
public boolean apply(final IEObjectDescription input) {
if (isPattern) {
return EcoreUtil2.isAssignableFrom(type, input.getEClass()) && ((QualifiedNamePattern) name).matches(name);
} else {
return name.equals(input.getName()) && EcoreUtil2.isAssignableFrom(type, input.getEClass());
}
}
};
return Collections2.filter(values, predicate);
}
use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.
the class PatternAwareEObjectDescriptionLookUp method getNameToObjectsLookup.
/**
* {@inheritDoc}
*/
protected QualifiedNameLookup<IEObjectDescription> getNameToObjectsLookup() {
if (nameToObjectsLookup == null) {
synchronized (this) {
// CHECKSTYLE:OFF with volatile it is ok
if (nameToObjectsLookup == null) {
// CHECKSTYLE:ON with volatile it is ok
Iterable<IEObjectDescription> allDescriptions = getExportedObjects();
// $NON-NLS-1$
QualifiedNameLookup<IEObjectDescription> localMap = CacheManager.getInstance().createNameLookupCache("PatternAwareEObjectDescriptionLookUp#localMap", IEObjectDescription.class, false);
if (allDescriptions instanceof RandomAccess) {
List<IEObjectDescription> asList = (List<IEObjectDescription>) allDescriptions;
for (int i = 0; i < asList.size(); i++) {
IEObjectDescription description = asList.get(i);
localMap.put(description.getName().toLowerCase(), description);
}
} else {
for (IEObjectDescription description : allDescriptions) {
localMap.put(description.getName().toLowerCase(), description);
}
}
this.nameToObjectsLookup = localMap;
}
}
}
return this.nameToObjectsLookup;
}
use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.
the class PrefixedContainerBasedScope method getSingleElement.
// Using QualifiedName#toLowerCase() not String#toLowerCase()
@SuppressWarnings("PMD.UseLocaleWithCaseConversions")
@Override
public synchronized IEObjectDescription getSingleElement(final QualifiedName name) {
final boolean ignoreCase = isIgnoreCase();
final QualifiedName lookupName = ignoreCase ? name.toLowerCase() : name;
final IEObjectDescription result = contentByNameCache.get(lookupName);
if (result != null && result != NULL_DESCRIPTION) {
return result;
} else if (result == null) {
final ContainerQuery copy = ((ContainerQuery.Builder) criteria).copy().name(prefix.append(lookupName)).ignoreCase(ignoreCase);
final Iterable<IEObjectDescription> res = copy.execute(container);
IEObjectDescription desc = Iterables.getFirst(res, null);
if (desc != null) {
IEObjectDescription aliased = new AliasingEObjectDescription(name, desc);
contentByNameCache.put(lookupName, aliased);
return aliased;
}
contentByNameCache.put(lookupName, NULL_DESCRIPTION);
}
// in case of aliasing revert to normal ContainerBasedScope behavior (using name pattern)
if (nameFunctions.size() > 1) {
return super.getSingleElement(name);
}
return getParent().getSingleElement(name);
}
use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.
the class ValidScopingTest method testEStructuralFeatureScope.
/**
* Tests that validations may be declared on existing EClass and EStructuralFeature instances.
*/
@Test
public void testEStructuralFeatureScope() throws IOException {
final ValidModel validModel = (ValidModel) getTestSource().getModel();
final NativeContext context = getXtextTestUtil().getFirstInstanceOf(validModel, NativeContext.class);
// Check context feature reference
IScope scope = scopeProvider.getScope(context, ValidPackage.Literals.CONTEXT__CONTEXT_FEATURE);
IEObjectDescription name = scope.getSingleElement(QualifiedName.create("name"));
assertNotNull("Found valid EStructuralFeature \"name\"", name);
final EObject resolvedName = name.getEObjectOrProxy();
assertNotNull("Valid EStructuralFeature \"name\" can be resolved", resolvedName);
// Check context type reference
scope = scopeProvider.getScope(context, ValidPackage.Literals.CONTEXT__CONTEXT_TYPE);
assertEquals("Scope provider returns correct context type", context.getContextType(), scope.getSingleElement(QualifiedName.create("Model")).getEObjectOrProxy());
assertEquals("Container of \"name\" reference instance is \"Model\" instance", resolvedName.eContainer(), scope.getSingleElement(QualifiedName.create("Model")).getEObjectOrProxy());
// Check marker type reference
scope = scopeProvider.getScope(context, ValidPackage.Literals.NATIVE_CONTEXT__MARKER_TYPE);
assertEquals("Scope provider returns correct marker type", context.getMarkerType(), scope.getSingleElement(QualifiedName.create("Element")).getEObjectOrProxy());
// Check marker feature reference
scope = scopeProvider.getScope(context, ValidPackage.Literals.NATIVE_CONTEXT__MARKER_FEATURE);
assertEquals("Scope provider returns correct marker feature", context.getMarkerFeature(), scope.getSingleElement(QualifiedName.create("name")).getEObjectOrProxy());
}
use of org.eclipse.xtext.resource.IEObjectDescription in project statecharts by Yakindu.
the class OperationOverloadingLinkingService method getLinkedOperation.
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());
}
Aggregations