Search in sources :

Example 6 with ProjectComparisonEntry

use of org.eclipse.n4js.compare.ProjectComparisonEntry 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 7 with ProjectComparisonEntry

use of org.eclipse.n4js.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.
        TModule superModule = pivot.getContainingModule();
        if (superModule != null) {
            ProjectComparisonEntry useCompareEntry = projectComparisonAdapter.getEntryFor(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 // end if null-check for module...
        {
            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.compare.ProjectComparisonEntry)

Example 8 with ProjectComparisonEntry

use of org.eclipse.n4js.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 : 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) 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.compare.ProjectComparisonEntry)

Example 9 with ProjectComparisonEntry

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

the class ApiCompareView method makeActions.

private void makeActions() {
    actionUpdate = new Action() {

        @Override
        public void run() {
            updateComparison();
        }
    };
    actionUpdate.setText("Update");
    actionUpdate.setToolTipText("Recompute comparison for all API project and their implementation projects in the workspace.");
    // action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
    // getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
    actionOpenInEditor = new Action() {

        @Override
        public void run() {
            ISelection selection = viewer.getSelection();
            Object obj = ((IStructuredSelection) selection).getFirstElement();
            if (obj instanceof ProjectComparisonEntry)
                showInEditor((ProjectComparisonEntry) obj, true, true);
        }
    };
    actionOpenInEditor.setText("Open in Editor");
    actionOpenInEditor.setToolTipText("Open the currently selected API element and its implementations in N4JS editors.");
}
Also used : Action(org.eclipse.jface.action.Action) ISelection(org.eclipse.jface.viewers.ISelection) EObject(org.eclipse.emf.ecore.EObject) ProjectComparisonEntry(org.eclipse.n4js.compare.ProjectComparisonEntry)

Example 10 with ProjectComparisonEntry

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

the class ProjectCompareTreeHelper method readDocumentation.

/**
 * Read documentation from the special <code>&#64;api</code> tag in the JSdoc of all types and members in the given
 * comparison. Documentation from the API source code will always be included; documentation from the implementation
 * source code will only be included for the given implementation indices. Strings will be stored in the given map
 * on a per-entry basis.
 */
public Map<ProjectComparisonEntry, String> readDocumentation(ProjectComparison comparison, int[] implIndices) {
    final Map<ProjectComparisonEntry, String> result = new HashMap<>();
    final ResourceSet resourceSetForDocRetrieval = n4jsCore.createResourceSet(Optional.absent());
    comparison.getAllEntries().forEach(currE -> {
        final String doc = readDocumentation(resourceSetForDocRetrieval, currE, implIndices);
        if (doc != null) {
            result.put(currE, doc);
        }
    });
    return result;
}
Also used : HashMap(java.util.HashMap) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ProjectComparisonEntry(org.eclipse.n4js.compare.ProjectComparisonEntry)

Aggregations

ProjectComparisonEntry (org.eclipse.n4js.compare.ProjectComparisonEntry)10 HashMap (java.util.HashMap)8 LinkedHashSet (java.util.LinkedHashSet)8 EObject (org.eclipse.emf.ecore.EObject)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 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