Search in sources :

Example 36 with TModule

use of org.eclipse.n4js.ts.types.TModule in project n4js by eclipse.

the class N4JSResource method discardStateFromDescription.

/**
 * Returns the {@link TModule} from the contents list and optionally proxifies the instance. Robust against broken
 * state of the resource, e.g. can handle cases where the second element in the list is not of type TModule.
 *
 * @param unload
 *            set to true if the module should be proxified.
 * @return the module at index 1 of the contents list (if any). Otherwise null.
 * @throws IllegalStateException
 *             if the contents list has more than two elements.
 */
protected TModule discardStateFromDescription(boolean unload) {
    ModuleAwareContentsList theContents = (ModuleAwareContentsList) contents;
    if (theContents != null && !theContents.isEmpty()) {
        if (theContents.size() == 2) {
            EObject eObject = theContents.get(1);
            if (eObject instanceof TModule) {
                TModule module = (TModule) eObject;
                if (unload) {
                    getUnloader().unloadRoot(module);
                }
                return module;
            }
            getUnloader().unloadRoot(eObject);
            return null;
        } else if (theContents.size() > 2) {
            throw new IllegalStateException("Unexpected size: " + theContents);
        }
    }
    return null;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) TModule(org.eclipse.n4js.ts.types.TModule)

Example 37 with TModule

use of org.eclipse.n4js.ts.types.TModule in project n4js by eclipse.

the class N4JSResourceDescription method getImportedNames.

@Override
public Iterable<QualifiedName> getImportedNames() {
    if (null == lazyImportedNames) {
        synchronized (this) {
            if (null == lazyImportedNames) {
                // get imported names collected during global scoping
                // the scope provider registers every request in scoping so that by this
                // also all names are collected that cannot be resolved
                Iterable<QualifiedName> superImportedNames = super.getImportedNames();
                // use sorted set to ensure order of items
                final SortedSet<QualifiedName> importedNames;
                if (superImportedNames != null) {
                    importedNames = Sets.newTreeSet(superImportedNames);
                } else {
                    importedNames = Sets.<QualifiedName>newTreeSet();
                }
                // import our own module name to get a proper change notification
                Resource resource = getResource();
                List<EObject> contents = resource.getContents();
                if (contents.size() > 1) {
                    TModule module = (TModule) contents.get(1);
                    importedNames.add(qualifiedNameProvider.getFullyQualifiedName(module));
                }
                final Set<EObject> crossRefTypes = Sets.newHashSet();
                IAcceptor<EObject> acceptor = getCrossRefTypeAcceptor(crossRefTypes);
                crossReferenceComputer.computeCrossRefs(resource, acceptor);
                for (EObject type : crossRefTypes) {
                    if (type instanceof Type) {
                        handleType(importedNames, type);
                    } else if (type instanceof TVariable) {
                        handleTVariable(importedNames, (TVariable) type);
                    } else if (type instanceof TEnumLiteral) {
                        handleTEnumLiteral(importedNames, (TEnumLiteral) type);
                    }
                }
                this.lazyImportedNames = importedNames;
            }
        }
    }
    return lazyImportedNames;
}
Also used : ContainerType(org.eclipse.n4js.ts.types.ContainerType) Type(org.eclipse.n4js.ts.types.Type) TVariable(org.eclipse.n4js.ts.types.TVariable) TEnumLiteral(org.eclipse.n4js.ts.types.TEnumLiteral) QualifiedName(org.eclipse.xtext.naming.QualifiedName) EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) TModule(org.eclipse.n4js.ts.types.TModule)

Example 38 with TModule

use of org.eclipse.n4js.ts.types.TModule in project n4js by eclipse.

the class N4JSResourceDescriptionStrategy method createEObjectDescriptions.

@Override
public boolean createEObjectDescriptions(final EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
    if (getQualifiedNameProvider() == null)
        return false;
    if (eObject instanceof TModule) {
        TModule module = (TModule) eObject;
        internalCreateEObjectDescriptionForRoot(module, acceptor);
        for (Type type : module.getTopLevelTypes()) {
            internalCreateEObjectDescription(type, acceptor);
        }
        for (TVariable variable : module.getVariables()) {
            internalCreateEObjectDescription(variable, acceptor);
        }
    }
    // export is only possible for top-level elements
    return false;
}
Also used : Type(org.eclipse.n4js.ts.types.Type) TVariable(org.eclipse.n4js.ts.types.TVariable) TModule(org.eclipse.n4js.ts.types.TModule)

Example 39 with TModule

use of org.eclipse.n4js.ts.types.TModule in project n4js by eclipse.

the class UserdataMapper method getDeserializedModuleFromDescription.

/**
 * Reads the TModule stored in the given IEObjectDescription.
 */
public static TModule getDeserializedModuleFromDescription(IEObjectDescription eObjectDescription, URI uri) {
    final String serializedData = eObjectDescription.getUserData(USERDATA_KEY_SERIALIZED_SCRIPT);
    if (Strings.isNullOrEmpty(serializedData)) {
        return null;
    }
    final XMIResource xres = new XMIResourceImpl(uri);
    try {
        final boolean binary = !serializedData.startsWith("<");
        final ByteArrayInputStream bais = new ByteArrayInputStream(binary ? XMLTypeFactory.eINSTANCE.createBase64Binary(serializedData) : serializedData.getBytes(TRANSFORMATION_CHARSET_NAME));
        xres.load(bais, getOptions(uri, binary));
    } catch (Exception e) {
        // $NON-NLS-1$
        LOGGER.warn("error deserializing module from IEObjectDescription: " + uri);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("error deserializing module from IEObjectDescription=" + eObjectDescription + ": " + uri, // $NON-NLS-1$
            e);
        }
        // IDE, so the format could be out of date (after an update of the IDE))
        return null;
    }
    final List<EObject> contents = xres.getContents();
    if (contents.isEmpty() || !(contents.get(0) instanceof TModule)) {
        return null;
    }
    final TModule module = (TModule) contents.get(0);
    xres.getContents().clear();
    return module;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) EObject(org.eclipse.emf.ecore.EObject) XMIResource(org.eclipse.emf.ecore.xmi.XMIResource) TModule(org.eclipse.n4js.ts.types.TModule) XMIResourceImpl(org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 40 with TModule

use of org.eclipse.n4js.ts.types.TModule in project n4js by eclipse.

the class AbstractTypeVisibilityChecker method isPublicInternalVisible.

private boolean isPublicInternalVisible(Resource contextResource, final T element) {
    if (contextResource != null) {
        final TModule contextModule = N4JSResource.getModule(contextResource);
        final TModule elementModule = N4JSResource.getModule(element.eResource());
        return elementModule == null || Strings.equal(contextModule.getVendorID(), elementModule.getVendorID());
    }
    return false;
}
Also used : TModule(org.eclipse.n4js.ts.types.TModule)

Aggregations

TModule (org.eclipse.n4js.ts.types.TModule)48 EObject (org.eclipse.emf.ecore.EObject)12 Resource (org.eclipse.emf.ecore.resource.Resource)8 URI (org.eclipse.emf.common.util.URI)7 Type (org.eclipse.n4js.ts.types.Type)7 TMember (org.eclipse.n4js.ts.types.TMember)6 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)5 InternalEObject (org.eclipse.emf.ecore.InternalEObject)4 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)4 Script (org.eclipse.n4js.n4JS.Script)4 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)4 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)4 N4JSResource (org.eclipse.n4js.resource.N4JSResource)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CoreException (org.eclipse.core.runtime.CoreException)2 XMIResource (org.eclipse.emf.ecore.xmi.XMIResource)2 XMIResourceImpl (org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl)2 ImportDeclaration (org.eclipse.n4js.n4JS.ImportDeclaration)2 ContainerType (org.eclipse.n4js.ts.types.ContainerType)2