Search in sources :

Example 6 with ProjectComparisonEntry

use of org.eclipse.n4js.tooling.compare.ProjectComparisonEntry in project n4js by eclipse.

the class ScriptApiTracker method computeMissingApiFields.

/**
 * Computes the list of virtual fields.
 *
 * @return List of {@link VirtualApiTField}
 */
public List<TField> computeMissingApiFields(N4InterfaceDeclaration declaration) {
    Optional<ProjectComparisonAdapter> optAdapt = firstProjectComparisonAdapter(declaration.eResource());
    if (optAdapt.isPresent()) {
        ProjectComparisonEntry compareEntry = optAdapt.get().getEntryFor(EcoreUtil2.getContainerOfType(declaration.getDefinedType(), TModule.class));
        ProjectComparisonEntry typeCompare = compareEntry.getChildForElementImpl(declaration.getDefinedTypeAsInterface());
        if (typeCompare != null) {
            return typeCompare.allChildren().filter(pce -> pce.getElementAPI() instanceof TField).filter(pce -> pce.getElementImpl()[0] == null).map(pce -> {
                TField apiMethod = (TField) pce.getElementAPI();
                // do not touch AST on copy!:
                TField copy = TypeUtils.copyPartial(apiMethod, TypesPackage.Literals.SYNTAX_RELATED_TELEMENT__AST_ELEMENT);
                TField ret = new VirtualApiTField(apiMethod.getName(), copy);
                return ret;
            }).collect(Collectors.toList());
        }
    }
    return emptyList();
}
Also used : Inject(com.google.inject.Inject) TClass(org.eclipse.n4js.ts.types.TClass) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) 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) AbstractModule(org.eclipse.n4js.ts.types.AbstractModule) 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) 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) ProjectComparisonEntry(org.eclipse.n4js.tooling.compare.ProjectComparisonEntry) TMember(org.eclipse.n4js.ts.types.TMember) Script(org.eclipse.n4js.n4JS.Script) ProjectCompareHelper(org.eclipse.n4js.tooling.compare.ProjectCompareHelper) Consumer(java.util.function.Consumer) TypeUtils(org.eclipse.n4js.types.utils.TypeUtils) 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.tooling.compare.ProjectComparisonEntry)

Example 7 with ProjectComparisonEntry

use of org.eclipse.n4js.tooling.compare.ProjectComparisonEntry in project n4js by eclipse.

the class ScriptApiTracker method interfaceApiSupertypeWalker.

/**
 * This methods takes an API-Element and pulls the projectComparisons along the suptertype chain.
 *
 * Does nothing if apiElement is null.
 *
 * @param filter
 *            applied to each encountered projectComparison
 * @param actionProvider
 *            function to apply on filtered ProjectComparinsonEntries.
 * @param apiElement
 *            concrete API element to start from.
 */
public <T extends TClassifier> void interfaceApiSupertypeWalker(Predicate<? super ProjectComparisonEntry> filter, Function<T, Consumer<? super ProjectComparisonEntry>> actionProvider, ProjectComparisonAdapter projectComparisonAdapter, /* ProjectComparisonEntry compareEntry, */
T apiElement, Class<T> castGuard) {
    if (apiElement == null) {
        // from projects
        return;
    }
    LinkedHashSet<T> toBeProcessedSuperInterfaces = new LinkedHashSet<>();
    LinkedHashSet<T> processedSuperInterfaces = new LinkedHashSet<>();
    // Yes it is correct ~Not correct~, since we need VirtualMethods for the direct missing parts:: //
    toBeProcessedSuperInterfaces.add(apiElement);
    while (!toBeProcessedSuperInterfaces.isEmpty()) {
        Iterator<T> iter = toBeProcessedSuperInterfaces.iterator();
        T pivot = iter.next();
        iter.remove();
        // do not process built-in types
        if (TypeUtils.isBuiltIn(pivot)) {
            continue;
        }
        // collect to be processed:
        includeAdditionsSuperInterfaces2(toBeProcessedSuperInterfaces, processedSuperInterfaces, pivot, castGuard);
        // go over methods.
        // Is the superInterface from the same Project ? If not it cannot be an API problem of this
        // implementation.
        AbstractModule superModule = pivot.getContainingModule();
        if (superModule instanceof TModule) {
            // not required for TDeclaredModule
            ProjectComparisonEntry useCompareEntry = projectComparisonAdapter.getEntryFor((TModule) superModule);
            if (useCompareEntry == null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("No comparison found for pivot = " + pivot.getName());
                }
            } else {
                // Is there an API entry at all ? --> If not it was just a normal implementation of some library ?
                ProjectComparisonEntry superInterfaceCompareEntry = useCompareEntry.getChildForElementAPI(pivot);
                if (superInterfaceCompareEntry != null) {
                    // Is there a difference between this API and the implementations ?
                    if (superInterfaceCompareEntry.hasChildren()) {
                        // get the ones which are missing implementations; others are real errors and will not be
                        // touched!
                        superInterfaceCompareEntry.allChildren().filter(filter).forEach(actionProvider.apply(pivot));
                    }
                }
            // end if superInterfaceCompareEntry != null
            }
        } else if (superModule == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("-#- could not get module for super-classifier: " + pivot.getName() + " of type " + pivot.getTypeAsString() + " providedByRuntime=" + pivot.isProvidedByRuntime());
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TModule(org.eclipse.n4js.ts.types.TModule) ProjectComparisonEntry(org.eclipse.n4js.tooling.compare.ProjectComparisonEntry) AbstractModule(org.eclipse.n4js.ts.types.AbstractModule)

Example 8 with ProjectComparisonEntry

use of org.eclipse.n4js.tooling.compare.ProjectComparisonEntry in project n4js by eclipse.

the class ScriptApiTracker method computeMissingApiFields.

/**
 * Computes the list of virtual AccessorTuples for missing static fields on Interfaces
 *
 * @return List of {@link VirtualApiTField}
 */
private List<AccessorTuple> computeMissingApiFields(TInterface declaration) {
    Optional<ProjectComparisonAdapter> optAdapt = firstProjectComparisonAdapter(declaration.eResource());
    if (optAdapt.isPresent()) {
        ProjectComparisonAdapter projectComparisonAdapter = optAdapt.get();
        ProjectComparisonEntry compareEntry = projectComparisonAdapter.getEntryFor(EcoreUtil2.getContainerOfType(declaration, TModule.class));
        ProjectComparisonEntry typeCompare = compareEntry.getChildForElementImpl(declaration);
        if (typeCompare != null) {
            ArrayList<AccessorTuple> collectedMissingFields = new ArrayList<>();
            Predicate<ProjectComparisonEntry> filter = (pce -> pce.getElementAPI() instanceof TField);
            filter = filter.and(pce -> pce.getElementImpl()[0] == null).and(pce -> PROVIDES_INITIALZER.hasAnnotation((TField) pce.getElementAPI()));
            Function<TInterface, Consumer<? super ProjectComparisonEntry>> actionProvider = pivot -> pce -> {
                TField apiField = ((TField) pce.getElementAPI());
                VirtualApiMissingFieldAccessorTuple ret = createVirtFieldAccessorTuple(apiField);
                collectedMissingFields.add(ret);
            };
            if (!checkInterfaceImplementsInterface(declaration, typeCompare.getElementAPI())) {
                return emptyList();
            }
            // Call the supertype iterations scaffolding:
            interfaceApiSupertypeWalker(filter, actionProvider, projectComparisonAdapter, (TInterface) typeCompare.getElementAPI(), TInterface.class);
            return collectedMissingFields;
        }
    }
    return emptyList();
}
Also used : Inject(com.google.inject.Inject) TClass(org.eclipse.n4js.ts.types.TClass) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) 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) AbstractModule(org.eclipse.n4js.ts.types.AbstractModule) 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) 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) ProjectComparisonEntry(org.eclipse.n4js.tooling.compare.ProjectComparisonEntry) TMember(org.eclipse.n4js.ts.types.TMember) Script(org.eclipse.n4js.n4JS.Script) ProjectCompareHelper(org.eclipse.n4js.tooling.compare.ProjectCompareHelper) Consumer(java.util.function.Consumer) TypeUtils(org.eclipse.n4js.types.utils.TypeUtils) 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) TInterface(org.eclipse.n4js.ts.types.TInterface) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) AccessorTuple(org.eclipse.n4js.ts.types.util.AccessorTuple) TModule(org.eclipse.n4js.ts.types.TModule) ProjectComparisonEntry(org.eclipse.n4js.tooling.compare.ProjectComparisonEntry)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)8 ProjectComparisonEntry (org.eclipse.n4js.tooling.compare.ProjectComparisonEntry)8 AbstractModule (org.eclipse.n4js.ts.types.AbstractModule)8 TModule (org.eclipse.n4js.ts.types.TModule)8 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)7 Inject (com.google.inject.Inject)7 Singleton (com.google.inject.Singleton)7 ArrayList (java.util.ArrayList)7 Collections (java.util.Collections)7 Collections.emptyList (java.util.Collections.emptyList)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Iterator (java.util.Iterator)7 List (java.util.List)7 Optional (java.util.Optional)7 Consumer (java.util.function.Consumer)7 Function (java.util.function.Function)7 Predicate (java.util.function.Predicate)7 Collectors (java.util.stream.Collectors)7 Stream (java.util.stream.Stream)7