Search in sources :

Example 41 with Type

use of org.eclipse.n4js.ts.types.Type 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 42 with Type

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

the class N4JSASTUtils method isSemiLegalAssignmentToFinalFieldInCtor.

/**
 * Returns true iff <code>expr</code> is a semi-legal assignment expression within a constructor to a final field of
 * the same class. Here, "semi"-legal means that one requirement for a fully legal such assignment is <b>not</b>
 * checked by this method: the requirement that the declaration of the assigned final field must not have an
 * initializer expression. For a fully legal assignment to a final field this has to be checked by client code.
 */
public static boolean isSemiLegalAssignmentToFinalFieldInCtor(EObject expr, TMember writtenMember) {
    if (!(expr instanceof AssignmentExpression))
        return false;
    final AssignmentExpression assExpr = (AssignmentExpression) expr;
    // left-hand side must be a property access to 'this'
    final Expression lhs = assExpr.getLhs();
    if (!(lhs instanceof ParameterizedPropertyAccessExpression))
        return false;
    final ParameterizedPropertyAccessExpression paExpr = (ParameterizedPropertyAccessExpression) lhs;
    if (!(paExpr.getTarget() instanceof ThisLiteral))
        return false;
    // BUT: check this only if we have a resolved property in paExpr (important if this method used from scoping)
    if (paExpr.getProperty() != null && !paExpr.getProperty().eIsProxy()) {
        if (writtenMember != null) {
            // case 1: writtenMember provided as argument -> must be identical
            if (paExpr.getProperty() != writtenMember)
                return false;
        } else {
            // case 2: writtenMember not provided -> take from paExpr
            if (paExpr.getProperty() instanceof TMember)
                writtenMember = (TMember) paExpr.getProperty();
        }
    }
    // written member must be a final field
    if (!(writtenMember instanceof TField))
        return false;
    final TField field = (TField) writtenMember;
    if (!field.isFinal())
        return false;
    // (IMPORTANT: we do not assert !field.isHasExpression() here, which would be required for a fully-legal write
    // access)
    // assExpr must be located in the constructor of the owner of 'field'
    // (a) find type model element for the function containing the assignment expression
    final FunctionDefinition containingFunction = getContainingFunction(assExpr);
    if (containingFunction == null)
        return false;
    final Type containingTFunction = containingFunction.getDefinedType();
    if (containingTFunction == null)
        return false;
    // (b) find constructor of the owner of the field
    final ContainerType<?> ownerOfField = field.getContainingType();
    if (ownerOfField == null)
        return false;
    final TMember ctorOfOwner = getOwnOrSuperCtor(ownerOfField);
    if (ctorOfOwner == null)
        return false;
    // (c) compare
    boolean simpleCompare = containingTFunction == ctorOfOwner;
    if (simpleCompare) {
        return true;
    }
    // filled type:
    if (containingTFunction.eContainer() instanceof TClass) {
        TClass containingTClass = (TClass) containingTFunction.eContainer();
        if (containingTClass.isStaticPolyfill() && containingTClass.getSuperClassRef() != null && containingTClass.getSuperClassRef().getDeclaredType() instanceof TClass) {
            // get replaced ctor:
            TClass filledClass = (TClass) containingTClass.getSuperClassRef().getDeclaredType();
            TMember replacedCtorOfFilledClass = getOwnOrSuperCtor(filledClass);
            // compare (incl. null)
            return replacedCtorOfFilledClass == ctorOfOwner;
        }
    }
    return false;
}
Also used : ContainerType(org.eclipse.n4js.ts.types.ContainerType) Type(org.eclipse.n4js.ts.types.Type) TField(org.eclipse.n4js.ts.types.TField) TMember(org.eclipse.n4js.ts.types.TMember) TClass(org.eclipse.n4js.ts.types.TClass)

Example 43 with Type

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

the class ParameterizedTypeRefStructuralImpl method getTypeRefAsString.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public String getTypeRefAsString() {
    TypingStrategy _typingStrategy = this.getTypingStrategy();
    Type _declaredType = this.getDeclaredType();
    String _rawTypeAsString = null;
    if (_declaredType != null) {
        _rawTypeAsString = _declaredType.getRawTypeAsString();
    }
    String _plus = (_typingStrategy + _rawTypeAsString);
    String _xifexpression = null;
    boolean _isEmpty = this.getTypeArgs().isEmpty();
    if (_isEmpty) {
        _xifexpression = "";
    } else {
        final Function1<TypeArgument, String> _function = new Function1<TypeArgument, String>() {

            public String apply(final TypeArgument it) {
                return it.getTypeRefAsString();
            }
        };
        String _join = IterableExtensions.join(XcoreEListExtensions.<TypeArgument, String>map(this.getTypeArgs(), _function), ",");
        String _plus_1 = ("<" + _join);
        _xifexpression = (_plus_1 + ">");
    }
    String _plus_2 = (_plus + _xifexpression);
    String _xifexpression_1 = null;
    boolean _isEmpty_1 = this.getStructuralMembers().isEmpty();
    if (_isEmpty_1) {
        _xifexpression_1 = "";
    } else {
        final Function1<TStructMember, String> _function_1 = new Function1<TStructMember, String>() {

            public String apply(final TStructMember it) {
                return it.getMemberAsString();
            }
        };
        String _join_1 = IterableExtensions.join(XcoreEListExtensions.<TStructMember, String>map(this.getStructuralMembers(), _function_1), "; ");
        String _plus_3 = (" with { " + _join_1);
        String _plus_4 = (_plus_3 + " }");
        String _xifexpression_2 = null;
        boolean _isEmpty_2 = this.getPostponedSubstitutions().isEmpty();
        if (_isEmpty_2) {
            _xifexpression_2 = "";
        } else {
            final Function1<TypeVariableMapping, String> _function_2 = new Function1<TypeVariableMapping, String>() {

                public String apply(final TypeVariableMapping it) {
                    String _typeAsString = it.getTypeVar().getTypeAsString();
                    String _plus = (_typeAsString + "->");
                    String _typeRefAsString = it.getTypeArg().getTypeRefAsString();
                    return (_plus + _typeRefAsString);
                }
            };
            String _join_2 = IterableExtensions.join(XcoreEListExtensions.<TypeVariableMapping, String>map(this.getPostponedSubstitutions(), _function_2), ", ");
            String _plus_5 = (" [[" + _join_2);
            _xifexpression_2 = (_plus_5 + "]]");
        }
        _xifexpression_1 = (_plus_4 + _xifexpression_2);
    }
    return (_plus_2 + _xifexpression_1);
}
Also used : TypingStrategy(org.eclipse.n4js.ts.types.TypingStrategy) Type(org.eclipse.n4js.ts.types.Type) TStructuralType(org.eclipse.n4js.ts.types.TStructuralType) TStructMember(org.eclipse.n4js.ts.types.TStructMember) TypeVariableMapping(org.eclipse.n4js.ts.typeRefs.TypeVariableMapping) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument)

Example 44 with Type

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

the class MethodDeclarationImpl method getDefinedTypeElement.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public TMember getDefinedTypeElement() {
    TMember _xifexpression = null;
    Type _definedType = this.getDefinedType();
    boolean _tripleEquals = (_definedType == null);
    if (_tripleEquals) {
        _xifexpression = null;
    } else {
        TMember _xifexpression_1 = null;
        Type _definedType_1 = this.getDefinedType();
        if ((_definedType_1 instanceof TMember)) {
            Type _definedType_2 = this.getDefinedType();
            _xifexpression_1 = ((TMember) _definedType_2);
        } else {
            throw new IllegalArgumentException("");
        }
        _xifexpression = _xifexpression_1;
    }
    return _xifexpression;
}
Also used : Type(org.eclipse.n4js.ts.types.Type) TMember(org.eclipse.n4js.ts.types.TMember)

Example 45 with Type

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

the class FunctionDeclarationImpl method setDefinedType.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public void setDefinedType(Type newDefinedType) {
    Type oldDefinedType = definedType;
    definedType = newDefinedType;
    if (eNotificationRequired())
        eNotify(new ENotificationImpl(this, Notification.SET, N4JSPackage.FUNCTION_DECLARATION__DEFINED_TYPE, oldDefinedType, definedType));
}
Also used : Type(org.eclipse.n4js.ts.types.Type) ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl)

Aggregations

Type (org.eclipse.n4js.ts.types.Type)84 ContainerType (org.eclipse.n4js.ts.types.ContainerType)32 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)28 TStructuralType (org.eclipse.n4js.ts.types.TStructuralType)26 EObject (org.eclipse.emf.ecore.EObject)21 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)21 PrimitiveType (org.eclipse.n4js.ts.types.PrimitiveType)19 AnyType (org.eclipse.n4js.ts.types.AnyType)18 UndefinedType (org.eclipse.n4js.ts.types.UndefinedType)18 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)17 NullType (org.eclipse.n4js.ts.types.NullType)17 VoidType (org.eclipse.n4js.ts.types.VoidType)17 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)16 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)16 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)16 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)16 ModuleNamespaceVirtualType (org.eclipse.n4js.ts.types.ModuleNamespaceVirtualType)16 StructuralTypingResult (org.eclipse.n4js.typesystem.StructuralTypingResult)16 Result (org.eclipse.xsemantics.runtime.Result)16 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)15