Search in sources :

Example 11 with TClass

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

the class ScriptApiTracker method computeMissingApiFields.

/**
 * Computes the list of virtual AccessorTuples for missing fields.
 *
 * @return List of {@link VirtualApiTField}
 */
private List<AccessorTuple> computeMissingApiFields(TClass declaration) {
    Optional<ProjectComparisonAdapter> optAdapt = firstProjectComparisonAdapter(declaration.eResource());
    if (optAdapt.isPresent()) {
        ProjectComparisonEntry compareEntry = optAdapt.get().getEntryFor(EcoreUtil2.getContainerOfType(declaration, TModule.class));
        ProjectComparisonEntry typeCompare = compareEntry.getChildForElementImpl(declaration);
        if (typeCompare != null) {
            return typeCompare.allChildren().filter(pce -> pce.getElementAPI() instanceof TField).filter(// only go for empty impl.
            pce -> pce.getElementImpl()[0] == null).map(pce -> {
                TField apiField = (TField) pce.getElementAPI();
                VirtualApiMissingFieldAccessorTuple ret = createVirtFieldAccessorTuple(apiField);
                return ret;
            }).collect(Collectors.toList());
        }
    }
    return emptyList();
}
Also used : ProjectComparisonEntry(org.eclipse.n4js.compare.ProjectComparisonEntry) Inject(com.google.inject.Inject) TClass(org.eclipse.n4js.ts.types.TClass) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) TSetter(org.eclipse.n4js.ts.types.TSetter) Logger(org.apache.log4j.Logger) TGetterImpl(org.eclipse.n4js.ts.types.impl.TGetterImpl) TMethodImpl(org.eclipse.n4js.ts.types.impl.TMethodImpl) Type(org.eclipse.n4js.ts.types.Type) MemberList(org.eclipse.n4js.ts.types.util.MemberList) N4InterfaceDeclaration(org.eclipse.n4js.n4JS.N4InterfaceDeclaration) TFieldImpl(org.eclipse.n4js.ts.types.impl.TFieldImpl) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) TField(org.eclipse.n4js.ts.types.TField) EObject(org.eclipse.emf.ecore.EObject) PROVIDES_DEFAULT_IMPLEMENTATION(org.eclipse.n4js.AnnotationDefinition.PROVIDES_DEFAULT_IMPLEMENTATION) PROVIDES_INITIALZER(org.eclipse.n4js.AnnotationDefinition.PROVIDES_INITIALZER) TMethod(org.eclipse.n4js.ts.types.TMethod) Collectors(java.util.stream.Collectors) TGetter(org.eclipse.n4js.ts.types.TGetter) List(java.util.List) Stream(java.util.stream.Stream) TClassifier(org.eclipse.n4js.ts.types.TClassifier) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) Pair(org.eclipse.xtext.xbase.lib.Pair) TypesFactory(org.eclipse.n4js.ts.types.TypesFactory) Singleton(com.google.inject.Singleton) HashMap(java.util.HashMap) TypeUtils(org.eclipse.n4js.ts.utils.TypeUtils) AccessorTuple(org.eclipse.n4js.ts.types.util.AccessorTuple) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TModule(org.eclipse.n4js.ts.types.TModule) TN4Classifier(org.eclipse.n4js.ts.types.TN4Classifier) TInterface(org.eclipse.n4js.ts.types.TInterface) ProjectCompareHelper(org.eclipse.n4js.compare.ProjectCompareHelper) EcoreUtil2(org.eclipse.xtext.EcoreUtil2) TAnnotableElement(org.eclipse.n4js.ts.types.TAnnotableElement) MemberCollector(org.eclipse.n4js.utils.ContainerTypesHelper.MemberCollector) LinkedHashSet(java.util.LinkedHashSet) Iterator(java.util.Iterator) TMember(org.eclipse.n4js.ts.types.TMember) Script(org.eclipse.n4js.n4JS.Script) Consumer(java.util.function.Consumer) TypesPackage(org.eclipse.n4js.ts.types.TypesPackage) AdapterImpl(org.eclipse.emf.common.notify.impl.AdapterImpl) TSetterImpl(org.eclipse.n4js.ts.types.impl.TSetterImpl) Collections(java.util.Collections) TField(org.eclipse.n4js.ts.types.TField) TModule(org.eclipse.n4js.ts.types.TModule) ProjectComparisonEntry(org.eclipse.n4js.compare.ProjectComparisonEntry)

Example 12 with TClass

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

the class ScriptApiTracker method computeMethodDiff.

/**
 * Find last missing methods, which the implType would have if it would follow the inheritance as defined in the API
 *
 * @param implType
 *            Type of implementation project, calculated from AST
 * @param collector
 *            member collector for the project.
 * @param ownedAndMixedInConcreteMember
 *            already computed for implType
 * @param missingApiMethods2
 *            already computed for implType
 * @return list of virtual Methods
 */
public List<VirtualApiTMethod> computeMethodDiff(TClass implType, MemberCollector collector, List<TMember> ownedAndMixedInConcreteMember, MemberList<TMethod> missingApiMethods2) {
    Optional<ProjectComparisonAdapter> optAdapt = firstProjectComparisonAdapter(implType.eResource());
    if (optAdapt.isPresent()) {
        ProjectComparisonEntry compareEntry = optAdapt.get().getEntryFor(EcoreUtil2.getContainerOfType(implType, TModule.class));
        ProjectComparisonEntry typeCompare = compareEntry.getChildForElementImpl(implType);
        if (typeCompare != null && typeCompare.getElementAPI() != null) {
            TClass apiType = (TClass) typeCompare.getElementAPI();
            MemberList<TMember> implMembers = collector.allMembers(implType, false, true);
            MemberList<TMember> apiMembers = collector.allMembers(apiType, false, true);
            final HashSet<String> methodNamesAlreadyDefined = new HashSet<>();
            Stream.concat(implMembers.stream(), Stream.concat(ownedAndMixedInConcreteMember.stream(), missingApiMethods2.stream())).forEach(m -> {
                if (m instanceof TMethod) {
                    methodNamesAlreadyDefined.add(m.getName());
                }
            });
            return apiMembers.stream().filter(t -> t instanceof TMethod).filter(m -> !methodNamesAlreadyDefined.contains(((TMethod) m).getName())).map(m2 -> {
                TMethod m = (TMethod) m2;
                VirtualApiTMethod vMethod = new VirtualApiTMethod(m.getName(), TypeUtils.copyPartial(m, TypesPackage.Literals.SYNTAX_RELATED_TELEMENT__AST_ELEMENT));
                return vMethod;
            }).collect(Collectors.toList());
        }
    }
    return emptyList();
}
Also used : ProjectComparisonEntry(org.eclipse.n4js.compare.ProjectComparisonEntry) Inject(com.google.inject.Inject) TClass(org.eclipse.n4js.ts.types.TClass) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) TSetter(org.eclipse.n4js.ts.types.TSetter) Logger(org.apache.log4j.Logger) TGetterImpl(org.eclipse.n4js.ts.types.impl.TGetterImpl) TMethodImpl(org.eclipse.n4js.ts.types.impl.TMethodImpl) Type(org.eclipse.n4js.ts.types.Type) MemberList(org.eclipse.n4js.ts.types.util.MemberList) N4InterfaceDeclaration(org.eclipse.n4js.n4JS.N4InterfaceDeclaration) TFieldImpl(org.eclipse.n4js.ts.types.impl.TFieldImpl) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) TField(org.eclipse.n4js.ts.types.TField) EObject(org.eclipse.emf.ecore.EObject) PROVIDES_DEFAULT_IMPLEMENTATION(org.eclipse.n4js.AnnotationDefinition.PROVIDES_DEFAULT_IMPLEMENTATION) PROVIDES_INITIALZER(org.eclipse.n4js.AnnotationDefinition.PROVIDES_INITIALZER) TMethod(org.eclipse.n4js.ts.types.TMethod) Collectors(java.util.stream.Collectors) TGetter(org.eclipse.n4js.ts.types.TGetter) List(java.util.List) Stream(java.util.stream.Stream) TClassifier(org.eclipse.n4js.ts.types.TClassifier) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) Pair(org.eclipse.xtext.xbase.lib.Pair) TypesFactory(org.eclipse.n4js.ts.types.TypesFactory) Singleton(com.google.inject.Singleton) HashMap(java.util.HashMap) TypeUtils(org.eclipse.n4js.ts.utils.TypeUtils) AccessorTuple(org.eclipse.n4js.ts.types.util.AccessorTuple) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TModule(org.eclipse.n4js.ts.types.TModule) TN4Classifier(org.eclipse.n4js.ts.types.TN4Classifier) TInterface(org.eclipse.n4js.ts.types.TInterface) ProjectCompareHelper(org.eclipse.n4js.compare.ProjectCompareHelper) EcoreUtil2(org.eclipse.xtext.EcoreUtil2) TAnnotableElement(org.eclipse.n4js.ts.types.TAnnotableElement) MemberCollector(org.eclipse.n4js.utils.ContainerTypesHelper.MemberCollector) LinkedHashSet(java.util.LinkedHashSet) Iterator(java.util.Iterator) TMember(org.eclipse.n4js.ts.types.TMember) Script(org.eclipse.n4js.n4JS.Script) Consumer(java.util.function.Consumer) TypesPackage(org.eclipse.n4js.ts.types.TypesPackage) AdapterImpl(org.eclipse.emf.common.notify.impl.AdapterImpl) TSetterImpl(org.eclipse.n4js.ts.types.impl.TSetterImpl) Collections(java.util.Collections) TMethod(org.eclipse.n4js.ts.types.TMethod) TModule(org.eclipse.n4js.ts.types.TModule) TMember(org.eclipse.n4js.ts.types.TMember) TClass(org.eclipse.n4js.ts.types.TClass) ProjectComparisonEntry(org.eclipse.n4js.compare.ProjectComparisonEntry) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 13 with TClass

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

the class AllSuperTypeRefsCollector method doProcess.

@Override
protected void doProcess(ContainerType<?> containerType) {
    if (containerType instanceof TClass) {
        TClass casted = (TClass) containerType;
        ParameterizedTypeRef superType = casted.getSuperClassRef();
        result.addAll(casted.getImplementedInterfaceRefs());
        if (superType != null) {
            result.add(superType);
        }
    } else if (containerType instanceof TInterface) {
        TInterface casted = (TInterface) containerType;
        result.addAll(casted.getSuperInterfaceRefs());
    } else if (containerType instanceof TObjectPrototype) {
        TObjectPrototype casted = (TObjectPrototype) containerType;
        ParameterizedTypeRef superType = casted.getSuperType();
        if (superType != null) {
            result.add(superType);
        }
    } else if (containerType instanceof PrimitiveType) {
        PrimitiveType assignmentCompatible = ((PrimitiveType) containerType).getAssignmentCompatible();
        if (assignmentCompatible != null) {
            ParameterizedTypeRef typeRef = TypeRefsFactory.eINSTANCE.createParameterizedTypeRef();
            typeRef.setDeclaredType(assignmentCompatible);
            result.add(typeRef);
        }
    }
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) TInterface(org.eclipse.n4js.ts.types.TInterface) TObjectPrototype(org.eclipse.n4js.ts.types.TObjectPrototype) PrimitiveType(org.eclipse.n4js.ts.types.PrimitiveType) TClass(org.eclipse.n4js.ts.types.TClass)

Example 14 with TClass

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

the class FindOwnedMemberBenchmarkTest method setup.

/**
 */
@Before
public void setup() {
    @SuppressWarnings("hiding") TClass type = TypesFactory.eINSTANCE.createTClass();
    @SuppressWarnings("hiding") List<String> names = Lists.newArrayList();
    for (int i = 0; i < members; i++) {
        // Member();
        TMember member = TypesFactory.eINSTANCE.createTField();
        member.setName("member" + i);
        names.add(member.getName());
        type.getOwnedMembers().add(member);
    }
    this.type = type;
    this.names = names;
}
Also used : TMember(org.eclipse.n4js.ts.types.TMember) TClass(org.eclipse.n4js.ts.types.TClass) Before(org.junit.Before)

Example 15 with TClass

use of org.eclipse.n4js.ts.types.TClass 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)

Aggregations

TClass (org.eclipse.n4js.ts.types.TClass)27 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)16 Type (org.eclipse.n4js.ts.types.Type)15 TMember (org.eclipse.n4js.ts.types.TMember)11 TClassifier (org.eclipse.n4js.ts.types.TClassifier)10 TInterface (org.eclipse.n4js.ts.types.TInterface)9 TMethod (org.eclipse.n4js.ts.types.TMethod)8 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 EObject (org.eclipse.emf.ecore.EObject)5 PrimitiveType (org.eclipse.n4js.ts.types.PrimitiveType)5 TField (org.eclipse.n4js.ts.types.TField)5 MemberList (org.eclipse.n4js.ts.types.util.MemberList)5 Collections.emptyList (java.util.Collections.emptyList)4 HashMap (java.util.HashMap)4 Stream (java.util.stream.Stream)4 N4InterfaceDeclaration (org.eclipse.n4js.n4JS.N4InterfaceDeclaration)4 Script (org.eclipse.n4js.n4JS.Script)4