Search in sources :

Example 6 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.

the class PolyfillValidatorFragment method holdsSinglePolyfillSource.

/**
 * Constraints 129 (Applying Polyfills) No member must be filled by more than one polyfill.
 */
private boolean holdsSinglePolyfillSource(PolyfillValidationState state) {
    EList<TMember> myPolyMember = state.polyType.getOwnedMembers();
    // a) find references to the filled type
    // b) check, that they are in the same Project
    // c) search for clashing contributions.
    XtextResource res = (XtextResource) state.polyType.eResource();
    IResourceDescriptions index = resourceDescriptionsProvider.getResourceDescriptions(res);
    // a+b) all polyfills to same calssifier in same project:
    IContainer container = containerManager.getContainer(res.getResourceServiceProvider().getResourceDescriptionManager().getResourceDescription(res), index);
    // Iterable over all exported Polyfills
    Iterable<IEObjectDescription> iterEObj = container.getExportedObjects(TypesPackage.Literals.TCLASSIFIER, N4TSQualifiedNameProvider.getPolyfillFQN(state.filledType, qualifiedNameProvider), false);
    // collection of involved TModules for each Member.
    ListMultimap<TMember, TModule> clashProviders = LinkedListMultimap.create();
    for (IEObjectDescription pivotObjectDescription : iterEObj) {
        EObject eob = pivotObjectDescription.getEObjectOrProxy();
        // Resolve
        if (eob.eIsProxy()) {
            eob = EcoreUtil.resolve(eob, res);
        }
        if (eob == state.polyType) {
            // saw myself .-.
            continue;
        }
        EList<TMember> pivotPolyMember = ((TClassifier) eob).getOwnedMembers();
        ListMultimap<TMember, TMember> clashing = findClashingMembersByName(myPolyMember, pivotPolyMember);
        for (TMember myMember : clashing.keySet()) {
            // only interested in the module, so first is sufficient
            clashProviders.put(myMember, clashing.get(myMember).get(0).getContainingModule());
        }
    }
    List<TMember> sortedMembers = clashProviders.keySet().stream().sorted().collect(Collectors.toList());
    for (TMember myMember : sortedMembers) {
        // Combine list of Modules involved in the polyfill clash.
        String uris = Stream.concat(Stream.of(myMember.getContainingModule()), clashProviders.get(myMember).stream()).map(u -> u.getQualifiedName().toString()).sorted().reduce("", (a, b) -> a + PREFIX_LIST + b);
        if (uris.startsWith(PREFIX_LIST))
            uris = uris.substring(PREFIX_LIST.length());
        int lastPrefix_idx = uris.lastIndexOf(PREFIX_LIST);
        if (lastPrefix_idx >= 0) {
            StringBuffer sb = new StringBuffer(uris);
            uris = sb.replace(lastPrefix_idx, lastPrefix_idx + PREFIX_LIST.length(), " and ").toString();
        }
        // give Qualified name filled in Property.
        String memberAxis = myMember.getContainingType().getName() + "." + myMember.getName();
        // Issue on filled Member-name declaration:
        String msg = IssueCodes.getMessageForCLF_POLYFILL_MULTIPOLYFILLS_MEMBER_CONFLICT(uris, memberAxis);
        state.host.addIssue(msg, myMember.getAstElement(), N4JSPackage.Literals.PROPERTY_NAME_OWNER__DECLARED_NAME, IssueCodes.CLF_POLYFILL_MULTIPOLYFILLS_MEMBER_CONFLICT);
    }
    return true;
}
Also used : TClassifier(org.eclipse.n4js.ts.types.TClassifier) XtextResource(org.eclipse.xtext.resource.XtextResource) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) EObject(org.eclipse.emf.ecore.EObject) TMember(org.eclipse.n4js.ts.types.TMember) IContainer(org.eclipse.xtext.resource.IContainer) TModule(org.eclipse.n4js.ts.types.TModule)

Example 7 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.

the class FindReferenceHelper method findReferences.

/**
 * @return all references to the given declaration. Respect editor states.
 */
public List<EObject> findReferences(EObject declaration) {
    declaration = getDeclaration(declaration);
    TargetURIs targets = getTargets(declaration);
    Resource eResource = declaration.eResource();
    SimpleResourceAccess resourceAccess = new SimpleResourceAccess(eResource.getResourceSet());
    IResourceDescriptions index = resourceDescriptionsProvider.getResourceDescriptions(eResource);
    ReferenceAcceptor acceptor = new ReferenceAcceptor();
    referenceFinder.findAllReferences(targets, resourceAccess, index, acceptor, null);
    return acceptor.results;
}
Also used : SimpleResourceAccess(org.eclipse.n4js.ts.findReferences.SimpleResourceAccess) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) Resource(org.eclipse.emf.ecore.resource.Resource) TargetURIs(org.eclipse.xtext.findReferences.TargetURIs)

Example 8 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.

the class ProjectCompareHelper method compareModules.

/**
 * Get the comparison for a module in a specific implementation specified by it's ID.
 *
 * @param module
 *            can be either Implementation or API
 * @param implementationID
 *            the current context to compare with
 *
 * @param includePolyfills
 *            {@code true} if polyfills should be considered as the part of the API and the implementation.
 *            Otherwise {@code false}.
 * @return a comparison between API and implementation with implementationID, or <code>null</code> if no project
 *         could be found, the module is not part of an API/IMPL ...
 */
public ProjectComparisonEntry compareModules(TModule module, String implementationID, final boolean includePolyfills) {
    Optional<? extends IN4JSProject> opt = n4jsCore.findProject(module.eResource().getURI());
    if (!opt.isPresent()) {
        return null;
    }
    IN4JSProject project = opt.get();
    IN4JSProject implProject = null;
    IN4JSProject apiProject = null;
    TModule apiModule = null;
    TModule apiImplModule = null;
    if (!project.getImplementationId().isPresent()) {
        // this is NOT an implementation project, so assume we have the api
        // need to load the correct implementation. Since there might be multiple implementors
        // TODO replace with central instance
        final ApiImplMapping mapping = ApiImplMapping.of(n4jsCore);
        implProject = mapping.getImpl(project.getProjectId(), implementationID);
        if (implProject == null) {
            // no implementation found.
            return null;
        }
        apiProject = project;
        apiModule = module;
        URI impUri = artifactHelper.findArtifact(implProject, apiModule.getQualifiedName(), Optional.of(N4JSGlobals.N4JS_FILE_EXTENSION));
        if (impUri != null) {
            IResourceDescriptions xtextIndex = n4jsCore.getXtextIndex(module.eResource().getResourceSet());
            IResourceDescription resourceDescription = xtextIndex.getResourceDescription(impUri);
            if (resourceDescription != null) {
                apiImplModule = n4jsCore.loadModuleFromIndex(module.eResource().getResourceSet(), resourceDescription, false);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("...ouch nothing in index for " + impUri);
                }
                Resource implResource = module.eResource().getResourceSet().getResource(impUri, true);
                apiImplModule = (TModule) implResource.getContents().get(1);
                // .get(1);
                if (logger.isDebugEnabled()) {
                    logger.debug("TModule loaded from Resource: " + apiImplModule);
                }
            }
        } else {
            // No implementation present.
            if (logger.isDebugEnabled()) {
                logger.debug("No implementation given. For " + apiModule.getQualifiedName());
            }
        }
    } else {
        // check that the implementation ID matches.
        if (implementationID.equals(project.getImplementationId().get())) {
            implProject = project;
            apiImplModule = module;
            ImmutableList<? extends IN4JSProject> apiProjects = implProject.getImplementedProjects();
            labelA: for (IN4JSProject ap : apiProjects) {
                URI apiURI = artifactHelper.findArtifact(ap, apiImplModule.getQualifiedName(), Optional.of(N4JSGlobals.N4JSD_FILE_EXTENSION));
                if (apiURI != null) {
                    IResourceDescriptions xtextIndex = n4jsCore.getXtextIndex(apiImplModule.eResource().getResourceSet());
                    IResourceDescription resourceDescription = xtextIndex.getResourceDescription(apiURI);
                    if (resourceDescription != null) {
                        apiModule = n4jsCore.loadModuleFromIndex(apiImplModule.eResource().getResourceSet(), resourceDescription, false);
                        if (apiModule != null)
                            break labelA;
                    }
                }
            }
        } else {
            return null;
        }
    }
    if (apiModule != null) {
        return compareModules(apiProject, apiModule, implProject, apiImplModule, includePolyfills);
    } else {
        // no apiModule --> this is not an implementation of API.
        return null;
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) Resource(org.eclipse.emf.ecore.resource.Resource) TModule(org.eclipse.n4js.ts.types.TModule) URI(org.eclipse.emf.common.util.URI)

Example 9 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.

the class TestDiscoveryHelper method collectTestLocations.

/**
 * Most clients should use method {@link #collectTests(URI...)} instead!
 * <p>
 * Low-level method to collect all test modules, i.e. N4JS files containing classes containing at least one method
 * annotated with &#64;Test, as {@link IResourceDescription}s.
 */
private Stream<URI> collectTestLocations(final IResourceDescriptions index, final ResourceSet resSet, final URI location) {
    if (null == location) {
        return Stream.empty();
    }
    // does location point to an N4JS project?
    if (isProject(location)) {
        // yes
        // --> collect all test modules (files containing test classes) located in source containers of type "test"
        final IN4JSProject p = n4jsCore.create(location);
        return p.getSourceContainers().stream().filter(IN4JSSourceContainer::isTest).flatMap(// note: IN4JSSourceContainer is an Iterable<URI>
        TestDiscoveryHelper::stream).filter(// filter out everything but N4JS files.
        uri -> isTestFile(uri)).filter(uri -> isTestModule(resSet, index.getResourceDescription(uri)));
    }
    // does location point to an n4js file?
    final IResourceDescription resDesc = index.getResourceDescription(location.trimFragment());
    if (resDesc != null) {
        // yes --> is it a test module? (i.e. does it contain test classes and the class is not abstract?)
        if (isTestModule(resSet, resDesc)) {
            // yes --> is it contained in a source container of type "test"?
            final IN4JSSourceContainer srcContainer = n4jsCore.findN4JSSourceContainer(location.trimFragment()).orNull();
            if (srcContainer != null && srcContainer.isTest()) {
                // return location with fragment! (if any)
                return Stream.of(location);
            }
        }
        return Stream.empty();
    }
    // does location point to a source container (or sub-folder)?
    final IN4JSSourceContainer srcContainer = n4jsCore.findN4JSSourceContainer(location).orNull();
    if (srcContainer != null) {
        // yes --> is this a source container of type "test"?
        if (srcContainer.isTest()) {
            // yes --> collect all test modules (files containing test classes) in this source container
            final String locationStr = location.toString();
            return // note: IN4JSSourceContainer is an Iterable<URI>
            stream(srcContainer).filter(// TODO improve?
            uri -> uri.toString().startsWith(locationStr)).filter(uri -> isTestModule(resSet, index.getResourceDescription(uri)));
        }
        return Stream.empty();
    }
    // invalid location URI
    return Stream.empty();
}
Also used : IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) Inject(com.google.inject.Inject) TClass(org.eclipse.n4js.ts.types.TClass) ResourceNameComputer(org.eclipse.n4js.utils.ResourceNameComputer) IN4JSCore(org.eclipse.n4js.projectModel.IN4JSCore) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) Collections.singletonList(java.util.Collections.singletonList) Optional.fromNullable(com.google.common.base.Optional.fromNullable) IN4JSSourceContainer(org.eclipse.n4js.projectModel.IN4JSSourceContainer) Type(org.eclipse.n4js.ts.types.Type) HashMultimap(com.google.common.collect.HashMultimap) Optional(com.google.common.base.Optional) FluentIterable.from(com.google.common.collect.FluentIterable.from) Map(java.util.Map) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) TEST_METHOD(org.eclipse.n4js.AnnotationDefinition.TEST_METHOD) EcoreUtil2.getContainerOfType(org.eclipse.xtext.EcoreUtil2.getContainerOfType) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Collections.emptyList(java.util.Collections.emptyList) EXPORTED_CLASS_KEY(org.eclipse.n4js.resource.N4JSResourceDescriptionStrategy.EXPORTED_CLASS_KEY) Collection(java.util.Collection) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) EObject(org.eclipse.emf.ecore.EObject) TestCase(org.eclipse.n4js.tester.domain.TestCase) TMethod(org.eclipse.n4js.ts.types.TMethod) TestSuite(org.eclipse.n4js.tester.domain.TestSuite) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Stream(java.util.stream.Stream) N4MethodDeclaration(org.eclipse.n4js.n4JS.N4MethodDeclaration) TRUE(java.lang.Boolean.TRUE) URI(org.eclipse.emf.common.util.URI) TEST_CLASS_KEY(org.eclipse.n4js.resource.N4JSResourceDescriptionStrategy.TEST_CLASS_KEY) Optional.absent(com.google.common.base.Optional.absent) FileExtensionsRegistry(org.eclipse.n4js.fileextensions.FileExtensionsRegistry) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) TModule(org.eclipse.n4js.ts.types.TModule) TestTree(org.eclipse.n4js.tester.domain.TestTree) Strings(com.google.common.base.Strings) ContainerTypesHelper(org.eclipse.n4js.utils.ContainerTypesHelper) EClass(org.eclipse.emf.ecore.EClass) FileExtensionType(org.eclipse.n4js.fileextensions.FileExtensionType) StreamSupport(java.util.stream.StreamSupport) ID(org.eclipse.n4js.tester.domain.ID) Iterables.isEmpty(com.google.common.collect.Iterables.isEmpty) ABSTRACT_KEY(org.eclipse.n4js.resource.N4JSResourceDescriptionStrategy.ABSTRACT_KEY) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) TMember(org.eclipse.n4js.ts.types.TMember) N4IDLGlobals(org.eclipse.n4js.n4idl.N4IDLGlobals) UUID.randomUUID(java.util.UUID.randomUUID) N4JSResource(org.eclipse.n4js.resource.N4JSResource) String.valueOf(java.lang.String.valueOf) TypesPackage(org.eclipse.n4js.ts.types.TypesPackage) Comparator(java.util.Comparator) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) IN4JSSourceContainer(org.eclipse.n4js.projectModel.IN4JSSourceContainer)

Example 10 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project statecharts by Yakindu.

the class STextGlobalScopeProvider method getScope.

/**
 * Overidden to avoid scope nesting which is not required and slows down because
 * of shadowing testing.
 */
@Override
protected IScope getScope(Resource resource, boolean ignoreCase, EClass type, Predicate<IEObjectDescription> filter) {
    final LinkedHashSet<URI> uniqueImportURIs = getImportedUris(resource);
    IResourceDescriptions descriptions = getResourceDescriptions(resource, uniqueImportURIs);
    List<URI> urisAsList = Lists.newArrayList(uniqueImportURIs);
    Collections.reverse(urisAsList);
    List<IEObjectDescription> objectDescriptions = new ArrayList<IEObjectDescription>();
    for (URI uri : urisAsList) {
        IScope scope = createLazyResourceScope(IScope.NULLSCOPE, uri, descriptions, type, filter, ignoreCase);
        Iterables.addAll(objectDescriptions, scope.getAllElements());
    }
    return MapBasedScope.createScope(IScope.NULLSCOPE, objectDescriptions);
}
Also used : IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ArrayList(java.util.ArrayList) IScope(org.eclipse.xtext.scoping.IScope) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)46 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)23 URI (org.eclipse.emf.common.util.URI)18 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)15 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)13 Resource (org.eclipse.emf.ecore.resource.Resource)12 EObject (org.eclipse.emf.ecore.EObject)11 QualifiedName (org.eclipse.xtext.naming.QualifiedName)7 IContainer (org.eclipse.xtext.resource.IContainer)7 List (java.util.List)6 Test (org.junit.Test)6 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)5 IScope (org.eclipse.xtext.scoping.IScope)5 IProject (org.eclipse.core.resources.IProject)4 EClass (org.eclipse.emf.ecore.EClass)4 XtextResource (org.eclipse.xtext.resource.XtextResource)4 IFile (org.eclipse.core.resources.IFile)3 CoreException (org.eclipse.core.runtime.CoreException)3 EPackage (org.eclipse.emf.ecore.EPackage)3 TModule (org.eclipse.n4js.ts.types.TModule)3