Search in sources :

Example 11 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class Analyzer method removeTransitive.

/**
	 * Transitively remove all elemens from unreachable through the uses link.
	 * 
	 * @param name
	 * @param unreachable
	 */
void removeTransitive(PackageRef name, Set<PackageRef> unreachable) {
    if (!unreachable.contains(name))
        return;
    unreachable.remove(name);
    List<PackageRef> ref = uses.get(name);
    if (ref != null) {
        for (Iterator<PackageRef> r = ref.iterator(); r.hasNext(); ) {
            PackageRef element = r.next();
            removeTransitive(element, unreachable);
        }
    }
}
Also used : PackageRef(aQute.bnd.osgi.Descriptors.PackageRef)

Example 12 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class Clazz method referTo.

/**
	 * Add a new package reference.
	 * 
	 * @param packageRef A '.' delimited package name
	 */
void referTo(TypeRef typeRef, int modifiers) {
    if (xref != null)
        xref.add(typeRef);
    if (typeRef.isPrimitive())
        return;
    PackageRef packageRef = typeRef.getPackageRef();
    if (packageRef.isPrimitivePackage())
        return;
    imports.add(packageRef);
    if (api != null && (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)))
        api.add(packageRef);
    if (cd != null)
        cd.referTo(typeRef, modifiers);
}
Also used : PackageRef(aQute.bnd.osgi.Descriptors.PackageRef)

Example 13 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class Builder method cleanupVersion.

public void cleanupVersion(Packages packages, String defaultVersion) {
    if (defaultVersion != null) {
        Matcher m = Verifier.VERSION.matcher(defaultVersion);
        if (m.matches()) {
            // Strip qualifier from default package version
            defaultVersion = Version.parseVersion(defaultVersion).getWithoutQualifier().toString();
        }
    }
    for (Map.Entry<PackageRef, Attrs> entry : packages.entrySet()) {
        Attrs attributes = entry.getValue();
        String v = attributes.get(Constants.VERSION_ATTRIBUTE);
        if (v == null && defaultVersion != null) {
            if (!isTrue(getProperty(Constants.NODEFAULTVERSION))) {
                v = defaultVersion;
                if (isPedantic())
                    warning("Used bundle version %s for exported package %s", v, entry.getKey());
            } else {
                if (isPedantic())
                    warning("No export version for exported package %s", entry.getKey());
            }
        }
        if (v != null)
            attributes.put(Constants.VERSION_ATTRIBUTE, cleanupVersion(v));
    }
}
Also used : Matcher(java.util.regex.Matcher) Attrs(aQute.bnd.header.Attrs) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map)

Example 14 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class bnd method doPrint.

private void doPrint(Jar jar, int options, printOptions po) throws ZipException, IOException, Exception {
    if ((options & VERIFY) != 0) {
        Verifier verifier = new Verifier(jar);
        verifier.setPedantic(isPedantic());
        verifier.verify();
        getInfo(verifier);
    }
    if ((options & MANIFEST) != 0) {
        Manifest manifest = jar.getManifest();
        if (manifest == null)
            warning("JAR has no manifest %s", jar);
        else {
            err.println("[MANIFEST " + jar.getName() + "]");
            printManifest(manifest);
        }
        out.println();
    }
    if ((options & IMPEXP) != 0) {
        out.println("[IMPEXP]");
        Manifest m = jar.getManifest();
        Domain domain = Domain.domain(m);
        if (m != null) {
            Parameters imports = domain.getImportPackage();
            Parameters exports = domain.getExportPackage();
            for (String p : exports.keySet()) {
                if (imports.containsKey(p)) {
                    Attrs attrs = imports.get(p);
                    if (attrs.containsKey(VERSION_ATTRIBUTE)) {
                        exports.get(p).put("imported-as", attrs.get(VERSION_ATTRIBUTE));
                    }
                }
            }
            print(Constants.IMPORT_PACKAGE, new TreeMap<String, Attrs>(imports));
            print(Constants.EXPORT_PACKAGE, new TreeMap<String, Attrs>(exports));
        } else
            warning("File has no manifest");
    }
    if ((options & CAPABILITIES) != 0) {
        out.println("[CAPABILITIES]");
        Manifest m = jar.getManifest();
        Domain domain = Domain.domain(m);
        if (m != null) {
            Parameters provide = domain.getProvideCapability();
            Parameters require = domain.getRequireCapability();
            print(Constants.PROVIDE_CAPABILITY, new TreeMap<String, Attrs>(provide));
            print(Constants.REQUIRE_CAPABILITY, new TreeMap<String, Attrs>(require));
        } else
            warning("File has no manifest");
    }
    if ((options & (USES | USEDBY | API)) != 0) {
        out.println();
        try (Analyzer analyzer = new Analyzer()) {
            analyzer.setPedantic(isPedantic());
            analyzer.setJar(jar);
            Manifest m = jar.getManifest();
            if (m != null) {
                String s = m.getMainAttributes().getValue(Constants.EXPORT_PACKAGE);
                if (s != null)
                    analyzer.setExportPackage(s);
            }
            analyzer.analyze();
            boolean java = po.java();
            Packages exports = analyzer.getExports();
            if ((options & API) != 0) {
                Map<PackageRef, List<PackageRef>> apiUses = analyzer.cleanupUses(analyzer.getAPIUses(), !po.java());
                if (!po.xport()) {
                    if (exports.isEmpty())
                        warning("Not filtering on exported only since exports are empty");
                    else
                        apiUses.keySet().retainAll(analyzer.getExports().keySet());
                }
                out.println("[API USES]");
                printMultiMap(apiUses);
                Set<PackageRef> privates = analyzer.getPrivates();
                for (PackageRef export : exports.keySet()) {
                    Map<Def, List<TypeRef>> xRef = analyzer.getXRef(export, privates, Modifier.PROTECTED + Modifier.PUBLIC);
                    if (!xRef.isEmpty()) {
                        out.println();
                        out.printf("%s refers to private Packages (not good)\n\n", export);
                        for (Entry<Def, List<TypeRef>> e : xRef.entrySet()) {
                            TreeSet<PackageRef> refs = new TreeSet<Descriptors.PackageRef>();
                            for (TypeRef ref : e.getValue()) refs.add(ref.getPackageRef());
                            refs.retainAll(privates);
                            //
                            out.printf(//
                            "%60s %-40s %s\n", //
                            e.getKey().getOwnerType().getFQN(), e.getKey().getName(), refs);
                        }
                        out.println();
                    }
                }
                out.println();
            }
            Map<PackageRef, List<PackageRef>> uses = analyzer.cleanupUses(analyzer.getUses(), !po.java());
            if ((options & USES) != 0) {
                out.println("[USES]");
                printMultiMap(uses);
                out.println();
            }
            if ((options & USEDBY) != 0) {
                out.println("[USEDBY]");
                MultiMap<PackageRef, PackageRef> usedBy = new MultiMap<Descriptors.PackageRef, Descriptors.PackageRef>(uses).transpose();
                printMultiMap(usedBy);
            }
        }
    }
    if ((options & COMPONENT) != 0) {
        printComponents(out, jar);
    }
    if ((options & METATYPE) != 0) {
        printMetatype(out, jar);
    }
    if ((options & LIST) != 0) {
        out.println("[LIST]");
        for (Map.Entry<String, Map<String, Resource>> entry : jar.getDirectories().entrySet()) {
            String name = entry.getKey();
            Map<String, Resource> contents = entry.getValue();
            out.println(name);
            if (contents != null) {
                for (String element : contents.keySet()) {
                    int n = element.lastIndexOf('/');
                    if (n > 0)
                        element = element.substring(n + 1);
                    out.print("  ");
                    out.print(element);
                    String path = element;
                    if (name.length() != 0)
                        path = name + "/" + element;
                    Resource r = contents.get(path);
                    if (r != null) {
                        String extra = r.getExtra();
                        if (extra != null) {
                            out.print(" extra='" + escapeUnicode(extra) + "'");
                        }
                    }
                    out.println();
                }
            } else {
                out.println(name + " <no contents>");
            }
        }
        out.println();
    }
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Attrs(aQute.bnd.header.Attrs) Verifier(aQute.bnd.osgi.Verifier) Analyzer(aQute.bnd.osgi.Analyzer) Packages(aQute.bnd.osgi.Packages) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ExtList(aQute.lib.collections.ExtList) List(java.util.List) SortedList(aQute.lib.collections.SortedList) Descriptors(aQute.bnd.osgi.Descriptors) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) Parameters(aQute.bnd.header.Parameters) Def(aQute.bnd.osgi.Clazz.Def) FileResource(aQute.bnd.osgi.FileResource) Resource(aQute.bnd.osgi.Resource) Manifest(java.util.jar.Manifest) PomFromManifest(aQute.bnd.maven.PomFromManifest) Domain(aQute.bnd.osgi.Domain) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 15 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class bnd method _xref.

/**
	 * Cross reference every class in the jar file to the files it references
	 */
@Description("Show a cross references for all classes in a set of jars.")
public void _xref(xrefOptions options) throws IOException, Exception {
    Analyzer analyzer = new Analyzer();
    final MultiMap<TypeRef, TypeRef> table = new MultiMap<TypeRef, TypeRef>();
    final MultiMap<PackageRef, PackageRef> packages = new MultiMap<PackageRef, PackageRef>();
    Set<TypeRef> set = Create.set();
    Instructions filter = new Instructions(options.match());
    for (String arg : options._arguments()) {
        try {
            File file = new File(arg);
            try (Jar jar = new Jar(file.getName(), file)) {
                for (Map.Entry<String, Resource> entry : jar.getResources().entrySet()) {
                    String key = entry.getKey();
                    Resource r = entry.getValue();
                    if (key.endsWith(".class")) {
                        TypeRef ref = analyzer.getTypeRefFromPath(key);
                        if (filter.matches(ref.toString())) {
                            set.add(ref);
                            try (InputStream in = r.openInputStream()) {
                                Clazz clazz = new Clazz(analyzer, key, r);
                                // TODO use the proper bcp instead
                                // of using the default layout
                                Set<TypeRef> s = clazz.parseClassFile();
                                for (Iterator<TypeRef> t = s.iterator(); t.hasNext(); ) {
                                    TypeRef tr = t.next();
                                    if (tr.isJava() || tr.isPrimitive())
                                        t.remove();
                                    else
                                        packages.add(ref.getPackageRef(), tr.getPackageRef());
                                }
                                table.addAll(ref, s);
                                set.addAll(s);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    boolean to = options.to();
    boolean from = options.from();
    if (to == false && from == false)
        to = from = true;
    if (options.classes()) {
        if (to)
            printxref(table, ">");
        if (from)
            printxref(table.transpose(), "<");
    } else {
        if (to)
            printxref(packages, ">");
        if (from)
            printxref(packages.transpose(), "<");
    }
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) FileResource(aQute.bnd.osgi.FileResource) Resource(aQute.bnd.osgi.Resource) Instructions(aQute.bnd.osgi.Instructions) Analyzer(aQute.bnd.osgi.Analyzer) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ZipException(java.util.zip.ZipException) MultiMap(aQute.lib.collections.MultiMap) Jar(aQute.bnd.osgi.Jar) Clazz(aQute.bnd.osgi.Clazz) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) File(java.io.File) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) Description(aQute.lib.getopt.Description)

Aggregations

PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)40 Attrs (aQute.bnd.header.Attrs)18 Analyzer (aQute.bnd.osgi.Analyzer)9 Clazz (aQute.bnd.osgi.Clazz)8 Map (java.util.Map)8 Parameters (aQute.bnd.header.Parameters)7 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)7 MultiMap (aQute.lib.collections.MultiMap)7 HashSet (java.util.HashSet)7 TreeSet (java.util.TreeSet)7 Manifest (java.util.jar.Manifest)7 Jar (aQute.bnd.osgi.Jar)6 Resource (aQute.bnd.osgi.Resource)6 Descriptors (aQute.bnd.osgi.Descriptors)5 ClassDataCollector (aQute.bnd.osgi.ClassDataCollector)4 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 Entry (java.util.Map.Entry)4