Search in sources :

Example 21 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class UserDataAwareScope method resolve.

private IEObjectDescription resolve(IEObjectDescription 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) {
            // --> still avoid loading from index; instead simply return 'original' as in other error cases
            return original;
        }
        if (resource == null) {
            resource = resourceSet.createResource(resourceURI);
        }
        if (resource instanceof N4JSResource) {
            if (resource.getContents().isEmpty()) {
                N4JSResource casted = (N4JSResource) resource;
                IResourceDescription resourceDescription = container.getResourceDescription(resourceURI);
                if (resourceDescription != null) {
                    if (casted.isLoaded()) {
                        // 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;
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) EObject(org.eclipse.emf.ecore.EObject) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) URI(org.eclipse.emf.common.util.URI)

Aggregations

N4JSResource (org.eclipse.n4js.resource.N4JSResource)21 Resource (org.eclipse.emf.ecore.resource.Resource)10 URI (org.eclipse.emf.common.util.URI)8 EObject (org.eclipse.emf.ecore.EObject)6 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)5 Script (org.eclipse.n4js.n4JS.Script)4 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)3 TModule (org.eclipse.n4js.ts.types.TModule)3 Optional (com.google.common.base.Optional)2 Iterables (com.google.common.collect.Iterables)2 File (java.io.File)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 Path (java.nio.file.Path)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 QualifiedName (org.eclipse.xtext.naming.QualifiedName)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2