Search in sources :

Example 1 with RequirementWrapper

use of bndtools.model.resolution.RequirementWrapper in project bndtools by bndtools.

the class AnalyseBundleResolutionJobTest method testOptionalImport.

public void testOptionalImport() {
    AnalyseBundleResolutionJob job = new AnalyseBundleResolutionJob("resolve", Collections.singleton(new JarFileCapReqLoader(new File("test/example2.jar"))));
    IStatus status = job.run(new NullProgressMonitor());
    assertEquals(IStatus.OK, status.getCode());
    Map<String, List<RequirementWrapper>> reqs = job.getRequirements();
    assertEquals(2, reqs.size());
    List<RequirementWrapper> imports = reqs.get("osgi.wiring.package");
    assertEquals("optional", imports.get(0).requirement.getDirectives().get("resolution"));
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) List(java.util.List) RequirementWrapper(bndtools.model.resolution.RequirementWrapper) File(java.io.File)

Example 2 with RequirementWrapper

use of bndtools.model.resolution.RequirementWrapper in project bndtools by bndtools.

the class PackageDropAdapter method performDrop.

@Override
public boolean performDrop(Object data) {
    int insertionIndex = -1;
    Object target = getCurrentTarget();
    if (target != null) {
        insertionIndex = indexOf(target);
        int loc = getCurrentLocation();
        if (loc == LOCATION_ON || loc == LOCATION_AFTER)
            insertionIndex++;
    }
    List<T> newEntries = new ArrayList<T>();
    if (data instanceof IResource[]) {
        for (IResource resource : (IResource[]) data) {
            IJavaElement javaElement = JavaCore.create(resource);
            if (javaElement instanceof IPackageFragment) {
                newEntries.add(createNewEntry(javaElement.getElementName()));
            }
        }
    } else if (data instanceof IStructuredSelection) {
        Iterator<?> iterator = ((IStructuredSelection) data).iterator();
        while (iterator.hasNext()) {
            Object element = iterator.next();
            if (element instanceof IPackageFragment) {
                IPackageFragment pkg = (IPackageFragment) element;
                newEntries.add(createNewEntry(pkg.getElementName()));
            } else if (element instanceof Capability) {
                Capability cap = (Capability) element;
                String namespace = cap.getNamespace();
                if (PackageNamespace.PACKAGE_NAMESPACE.equals(namespace)) {
                    String pkgName = (String) cap.getAttributes().get(namespace);
                    newEntries.add(createNewEntry(pkgName));
                }
            } else if (element instanceof Requirement) {
                String pkgName = getPackageNameFromRequirement((Requirement) element);
                if (pkgName != null)
                    newEntries.add(createNewEntry(pkgName));
            } else if (element instanceof RequirementWrapper) {
                String pkgName = getPackageNameFromRequirement(((RequirementWrapper) element).requirement);
                if (pkgName != null)
                    newEntries.add(createNewEntry(pkgName));
            }
        }
    }
    addRows(insertionIndex, newEntries);
    return true;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Capability(org.osgi.resource.Capability) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Requirement(org.osgi.resource.Requirement) Iterator(java.util.Iterator) RequirementWrapper(bndtools.model.resolution.RequirementWrapper) IResource(org.eclipse.core.resources.IResource)

Example 3 with RequirementWrapper

use of bndtools.model.resolution.RequirementWrapper in project bndtools by bndtools.

the class BndBuilderCapReqLoader method loadRequirements.

@Override
public Map<String, List<RequirementWrapper>> loadRequirements() throws Exception {
    Builder builder = getBuilder();
    if (builder == null)
        return Collections.emptyMap();
    Jar jar = builder.getJar();
    if (jar == null)
        return Collections.emptyMap();
    Manifest manifest = jar.getManifest();
    if (manifest == null)
        return Collections.emptyMap();
    Attributes attribs = manifest.getMainAttributes();
    Map<String, List<RequirementWrapper>> requirements = new HashMap<String, List<RequirementWrapper>>();
    // Process imports
    String importPkgStr = attribs.getValue(Constants.IMPORT_PACKAGE);
    Parameters importsMap = new Parameters(importPkgStr);
    for (Entry<String, Attrs> entry : importsMap.entrySet()) {
        String pkgName = Processor.removeDuplicateMarker(entry.getKey());
        Attrs attrs = entry.getValue();
        CapReqBuilder rb = new CapReqBuilder(PackageNamespace.PACKAGE_NAMESPACE);
        String filter = createVersionFilter(PackageNamespace.PACKAGE_NAMESPACE, pkgName, attrs.get(Constants.VERSION_ATTRIBUTE), PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
        rb.addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter);
        if (Constants.RESOLUTION_OPTIONAL.equals(attrs.get(Constants.RESOLUTION_DIRECTIVE + ":")))
            rb.addDirective(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE, Namespace.RESOLUTION_OPTIONAL);
        Collection<Clazz> importers = findImportingClasses(pkgName, builder);
        RequirementWrapper rw = new RequirementWrapper();
        rw.requirement = rb.buildSyntheticRequirement();
        rw.requirers = importers;
        addRequirement(requirements, rw);
    }
    // Process require-bundle
    String requireBundleStr = attribs.getValue(Constants.REQUIRE_BUNDLE);
    Parameters requireBundles = new Parameters(requireBundleStr);
    for (Entry<String, Attrs> entry : requireBundles.entrySet()) {
        String bsn = Processor.removeDuplicateMarker(entry.getKey());
        Attrs attrs = entry.getValue();
        CapReqBuilder rb = new CapReqBuilder(BundleNamespace.BUNDLE_NAMESPACE);
        String filter = createVersionFilter(BundleNamespace.BUNDLE_NAMESPACE, bsn, attrs.get(Constants.BUNDLE_VERSION_ATTRIBUTE), AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
        rb.addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter);
        if (Constants.RESOLUTION_OPTIONAL.equals(attrs.get(Constants.RESOLUTION_DIRECTIVE + ":")))
            rb.addDirective(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE, Namespace.RESOLUTION_OPTIONAL);
        RequirementWrapper rw = new RequirementWrapper();
        rw.requirement = rb.buildSyntheticRequirement();
        addRequirement(requirements, rw);
    }
    // Process generic requires
    String requiresStr = attribs.getValue(Constants.REQUIRE_CAPABILITY);
    Parameters requires = new Parameters(requiresStr);
    for (Entry<String, Attrs> entry : requires.entrySet()) {
        String ns = Processor.removeDuplicateMarker(entry.getKey());
        Attrs attrs = entry.getValue();
        CapReqBuilder rb = new CapReqBuilder(ns);
        for (String key : attrs.keySet()) {
            if (key.endsWith(":"))
                rb.addDirective(key.substring(0, key.length() - 1), attrs.get(key));
            else
                rb.addAttribute(key, attrs.getTyped(key));
        }
        RequirementWrapper rw = new RequirementWrapper();
        rw.requirement = rb.buildSyntheticRequirement();
        addRequirement(requirements, rw);
    }
    return requirements;
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Parameters(aQute.bnd.header.Parameters) HashMap(java.util.HashMap) CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Builder(aQute.bnd.osgi.Builder) Attributes(java.util.jar.Attributes) Attrs(aQute.bnd.header.Attrs) Manifest(java.util.jar.Manifest) Jar(aQute.bnd.osgi.Jar) LinkedList(java.util.LinkedList) List(java.util.List) Clazz(aQute.bnd.osgi.Clazz) RequirementWrapper(bndtools.model.resolution.RequirementWrapper)

Example 4 with RequirementWrapper

use of bndtools.model.resolution.RequirementWrapper in project bndtools by bndtools.

the class ResourceCapReqLoader method loadRequirements.

@Override
public Map<String, List<RequirementWrapper>> loadRequirements() throws Exception {
    Map<String, List<RequirementWrapper>> result = new HashMap<String, List<RequirementWrapper>>();
    List<Requirement> reqs = resource.getRequirements(null);
    for (Requirement req : reqs) {
        String ns = req.getNamespace();
        List<RequirementWrapper> listForNamespace = result.get(ns);
        if (listForNamespace == null) {
            listForNamespace = new LinkedList<RequirementWrapper>();
            result.put(ns, listForNamespace);
        }
        RequirementWrapper wrapper = new RequirementWrapper();
        wrapper.requirement = req;
        listForNamespace.add(wrapper);
    }
    return result;
}
Also used : Requirement(org.osgi.resource.Requirement) HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList) RequirementWrapper(bndtools.model.resolution.RequirementWrapper)

Example 5 with RequirementWrapper

use of bndtools.model.resolution.RequirementWrapper in project bndtools by bndtools.

the class BundleCalculatedImportsPart method createSection.

private void createSection(Section section, FormToolkit toolkit) {
    // CREATE COMPONENTS
    section.setText(Messages.BundleCalculatedImportsPart_title);
    ToolBar toolbar = new ToolBar(section, SWT.FLAT);
    section.setTextClient(toolbar);
    final ToolItem showSelfImportsItem = new ToolItem(toolbar, SWT.CHECK);
    showSelfImportsItem.setImage(imgShowSelfImports);
    showSelfImportsItem.setToolTipText(Messages.BundleCalculatedImportsPart_tooltipShowSelfImports);
    Composite composite = toolkit.createComposite(section);
    section.setClient(composite);
    // toolkit.createLabel(composite,
    // Messages.BundleCalculatedImportsPart_description, SWT.WRAP);
    tree = toolkit.createTree(composite, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
    viewer = new TreeViewer(tree);
    viewer.setContentProvider(new CapReqMapContentProvider());
    viewer.setLabelProvider(new RequirementWrapperLabelProvider(true));
    ColumnViewerToolTipSupport.enableFor(viewer);
    nonPkgFilter = new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parent, Object element) {
            if (element instanceof RequirementWrapper)
                return PackageNamespace.PACKAGE_NAMESPACE.equals(((RequirementWrapper) element).requirement.getNamespace());
            return true;
        }
    };
    hideSelfImportsFilter = new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            if (element instanceof RequirementWrapper)
                return !((RequirementWrapper) element).resolved;
            return true;
        }
    };
    viewer.setFilters(new ViewerFilter[] { nonPkgFilter, hideSelfImportsFilter });
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            getManagedForm().fireSelectionChanged(BundleCalculatedImportsPart.this, event.getSelection());
        }
    });
    viewer.addDragSupport(DND.DROP_MOVE | DND.DROP_COPY, new Transfer[] { LocalSelectionTransfer.getTransfer() }, new DragSourceAdapter() {

        @Override
        public void dragSetData(DragSourceEvent event) {
            LocalSelectionTransfer transfer = LocalSelectionTransfer.getTransfer();
            if (transfer.isSupportedType(event.dataType))
                transfer.setSelection(viewer.getSelection());
        }
    });
    viewer.addOpenListener(new IOpenListener() {

        @Override
        public void open(OpenEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            for (Iterator<?> iter = selection.iterator(); iter.hasNext(); ) {
                Object item = iter.next();
                if (item instanceof Clazz) {
                    Clazz importUsedBy = (Clazz) item;
                    String className = importUsedBy.getFQN();
                    IType type = null;
                    IFile file = getEditorFile();
                    if (file != null) {
                        IJavaProject javaProject = JavaCore.create(file.getProject());
                        try {
                            type = javaProject.findType(className);
                        } catch (JavaModelException e) {
                            ErrorDialog.openError(tree.getShell(), Messages.BundleCalculatedImportsPart_error, Messages.BundleCalculatedImportsPart_errorFindingType, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, MessageFormat.format(Messages.BundleCalculatedImportsPart_errorOpeningClass, className), e));
                        }
                    }
                    try {
                        if (type != null)
                            JavaUI.openInEditor(type, true, true);
                    } catch (PartInitException e) {
                        ErrorDialog.openError(tree.getShell(), Messages.BundleCalculatedImportsPart_error, null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, MessageFormat.format(Messages.BundleCalculatedImportsPart_errorOpeningJavaEditor, className), e));
                    } catch (JavaModelException e) {
                        ErrorDialog.openError(tree.getShell(), Messages.BundleCalculatedImportsPart_error, null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, MessageFormat.format(Messages.BundleCalculatedImportsPart_errorOpeningClass, className), e));
                    }
                }
            }
        }
    });
    // LISTENERS
    showSelfImportsItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            boolean showSelfImports = showSelfImportsItem.getSelection();
            ViewerFilter[] filters = showSelfImports ? new ViewerFilter[] { nonPkgFilter } : new ViewerFilter[] { nonPkgFilter, hideSelfImportsFilter };
            viewer.setFilters(filters);
        }
    });
    // LAYOUT
    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.verticalSpacing = 2;
    composite.setLayout(layout);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.heightHint = 75;
    gd.widthHint = 75;
    tree.setLayoutData(gd);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) CapReqMapContentProvider(bndtools.model.resolution.CapReqMapContentProvider) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) TreeViewer(org.eclipse.jface.viewers.TreeViewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Viewer(org.eclipse.jface.viewers.Viewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IType(org.eclipse.jdt.core.IType) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) GridLayout(org.eclipse.swt.layout.GridLayout) Iterator(java.util.Iterator) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Clazz(aQute.bnd.osgi.Clazz) PartInitException(org.eclipse.ui.PartInitException) RequirementWrapper(bndtools.model.resolution.RequirementWrapper) LocalSelectionTransfer(org.eclipse.jface.util.LocalSelectionTransfer) ToolItem(org.eclipse.swt.widgets.ToolItem) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) DragSourceAdapter(org.eclipse.swt.dnd.DragSourceAdapter) Composite(org.eclipse.swt.widgets.Composite) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) RequirementWrapperLabelProvider(bndtools.model.resolution.RequirementWrapperLabelProvider) IOpenListener(org.eclipse.jface.viewers.IOpenListener) IJavaProject(org.eclipse.jdt.core.IJavaProject) ToolBar(org.eclipse.swt.widgets.ToolBar) GridData(org.eclipse.swt.layout.GridData) OpenEvent(org.eclipse.jface.viewers.OpenEvent)

Aggregations

RequirementWrapper (bndtools.model.resolution.RequirementWrapper)8 List (java.util.List)5 IStatus (org.eclipse.core.runtime.IStatus)4 Clazz (aQute.bnd.osgi.Clazz)3 File (java.io.File)3 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 Capability (org.osgi.resource.Capability)3 CapReqMapContentProvider (bndtools.model.resolution.CapReqMapContentProvider)2 RequirementWrapperLabelProvider (bndtools.model.resolution.RequirementWrapperLabelProvider)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 IFile (org.eclipse.core.resources.IFile)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Status (org.eclipse.core.runtime.Status)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 IType (org.eclipse.jdt.core.IType)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 IOpenListener (org.eclipse.jface.viewers.IOpenListener)2