Search in sources :

Example 81 with IEObjectDescription

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);
}
Also used : QualifiedNamePattern(com.avaloq.tools.ddk.xtext.naming.QualifiedNamePattern) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 82 with IEObjectDescription

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;
}
Also used : RandomAccess(java.util.RandomAccess) List(java.util.List) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 83 with IEObjectDescription

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);
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 84 with IEObjectDescription

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());
}
Also used : ValidModel(com.avaloq.tools.ddk.xtext.valid.valid.ValidModel) NativeContext(com.avaloq.tools.ddk.xtext.valid.valid.NativeContext) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) AbstractScopingTest(com.avaloq.tools.ddk.xtext.test.scoping.AbstractScopingTest) Test(org.junit.Test)

Example 85 with IEObjectDescription

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());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EObject(org.eclipse.emf.ecore.EObject) QualifiedName(org.eclipse.xtext.naming.QualifiedName) ArrayList(java.util.ArrayList) IScope(org.eclipse.xtext.scoping.IScope) Operation(org.yakindu.base.types.Operation) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)324 Test (org.junit.Test)95 EObject (org.eclipse.emf.ecore.EObject)82 QualifiedName (org.eclipse.xtext.naming.QualifiedName)79 IScope (org.eclipse.xtext.scoping.IScope)56 URI (org.eclipse.emf.common.util.URI)41 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)33 EClass (org.eclipse.emf.ecore.EClass)30 Resource (org.eclipse.emf.ecore.resource.Resource)28 EObjectDescription (org.eclipse.xtext.resource.EObjectDescription)22 AliasedEObjectDescription (org.eclipse.xtext.resource.impl.AliasedEObjectDescription)22 ArrayList (java.util.ArrayList)19 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)14 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 StyledString (org.eclipse.jface.viewers.StyledString)12 XtextResource (org.eclipse.xtext.resource.XtextResource)11 StringInputStream (org.eclipse.xtext.util.StringInputStream)11 EReference (org.eclipse.emf.ecore.EReference)10 List (java.util.List)9