Search in sources :

Example 1 with TypeRef

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

the class Analyzer method parsePackageInfoClass.

private Attrs parsePackageInfoClass(Resource r) throws Exception {
    final Attrs info = new Attrs();
    final Clazz clazz = new Clazz(this, "", r);
    clazz.parseClassFileWithCollector(new ClassDataCollector() {

        @Override
        public void annotation(Annotation a) {
            String name = a.getName().getFQN();
            switch(name) {
                case "aQute.bnd.annotation.Version":
                    warning("%s annotation used in class %s. Bnd versioning annotations are deprecated as of Bnd 3.2 and support will be removed in Bnd 4.0. Please change to use OSGi versioning annotations.", name, clazz);
                case "org.osgi.annotation.versioning.Version":
                    // Check version
                    String version = a.get("value");
                    if (!info.containsKey(Constants.VERSION_ATTRIBUTE)) {
                        if (version != null) {
                            version = getReplacer().process(version);
                            if (Verifier.VERSION.matcher(version).matches())
                                info.put(VERSION_ATTRIBUTE, version);
                            else
                                error("Version annotation in %s has invalid version info: %s", clazz, version);
                        }
                    } else {
                        // Verify this matches with packageinfo
                        String presentVersion = info.get(VERSION_ATTRIBUTE);
                        try {
                            Version av = new Version(presentVersion).getWithoutQualifier();
                            Version bv = new Version(version).getWithoutQualifier();
                            if (!av.equals(bv)) {
                                error("Version from annotation for %s differs with packageinfo or Manifest", clazz.getClassName().getFQN());
                            }
                        } catch (Exception e) {
                        // Ignore
                        }
                    }
                    break;
                case "aQute.bnd.annotation.Export":
                    // Check mandatory attributes
                    Attrs attrs = doAttrbutes((Object[]) a.get(Export.MANDATORY), clazz, getReplacer());
                    if (!attrs.isEmpty()) {
                        info.putAll(attrs);
                        info.put(MANDATORY_DIRECTIVE, Processor.join(attrs.keySet()));
                    }
                    // Check optional attributes
                    attrs = doAttrbutes((Object[]) a.get(Export.OPTIONAL), clazz, getReplacer());
                    if (!attrs.isEmpty()) {
                        info.putAll(attrs);
                    }
                    // Check Included classes
                    Object[] included = a.get(Export.INCLUDE);
                    if (included != null && included.length > 0) {
                        StringBuilder sb = new StringBuilder();
                        String del = "";
                        for (Object i : included) {
                            Matcher m = OBJECT_REFERENCE.matcher(((TypeRef) i).getFQN());
                            if (m.matches()) {
                                sb.append(del);
                                sb.append(m.group(2));
                                del = ",";
                            }
                        }
                        info.put(INCLUDE_DIRECTIVE, sb.toString());
                    }
                    // Check Excluded classes
                    Object[] excluded = a.get(Export.EXCLUDE);
                    if (excluded != null && excluded.length > 0) {
                        StringBuilder sb = new StringBuilder();
                        String del = "";
                        for (Object i : excluded) {
                            Matcher m = OBJECT_REFERENCE.matcher(((TypeRef) i).getFQN());
                            if (m.matches()) {
                                sb.append(del);
                                sb.append(m.group(2));
                                del = ",";
                            }
                        }
                        info.put(EXCLUDE_DIRECTIVE, sb.toString());
                    }
                    // Check Uses
                    Object[] uses = a.get(Export.USES);
                    if (uses != null && uses.length > 0) {
                        String old = info.get(USES_DIRECTIVE);
                        if (old == null)
                            old = "";
                        StringBuilder sb = new StringBuilder(old);
                        String del = sb.length() == 0 ? "" : ",";
                        for (Object use : uses) {
                            sb.append(del);
                            sb.append(use);
                            del = ",";
                        }
                        info.put(USES_DIRECTIVE, sb.toString());
                    }
                    break;
            }
        }
    });
    return info;
}
Also used : Version(aQute.bnd.version.Version) Matcher(java.util.regex.Matcher) TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Attrs(aQute.bnd.header.Attrs) ParseException(java.text.ParseException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 2 with TypeRef

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

the class Analyzer method getPackages.

public Collection<PackageRef> getPackages(Packages scope, String... args) throws Exception {
    List<PackageRef> pkgs = new LinkedList<PackageRef>();
    Packages.QUERY queryType;
    Instruction instr;
    if (args.length == 1) {
        queryType = null;
        instr = null;
    } else if (args.length >= 2) {
        queryType = Packages.QUERY.valueOf(args[1].toUpperCase());
        if (args.length > 2)
            instr = new Instruction(args[2]);
        else
            instr = null;
    } else {
        throw new IllegalArgumentException("${packages} macro: invalid argument count");
    }
    for (Entry<PackageRef, Attrs> entry : scope.entrySet()) {
        PackageRef pkg = entry.getKey();
        TypeRef pkgInfoTypeRef = getTypeRefFromFQN(pkg.getFQN() + ".package-info");
        Clazz pkgInfo = classspace.get(pkgInfoTypeRef);
        boolean accept = false;
        if (queryType != null) {
            switch(queryType) {
                case ANY:
                    accept = true;
                    break;
                case NAMED:
                    if (instr == null)
                        throw new IllegalArgumentException("Not enough arguments in ${packages} macro");
                    accept = instr.matches(pkg.getFQN()) ^ instr.isNegated();
                    break;
                case ANNOTATED:
                    if (instr == null)
                        throw new IllegalArgumentException("Not enough arguments in ${packages} macro");
                    accept = pkgInfo != null && pkgInfo.is(Clazz.QUERY.ANNOTATED, instr, this);
                    break;
                case VERSIONED:
                    accept = entry.getValue().getVersion() != null;
                    break;
            }
        } else {
            accept = true;
        }
        if (accept)
            pkgs.add(pkg);
    }
    return pkgs;
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Attrs(aQute.bnd.header.Attrs) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) LinkedList(java.util.LinkedList)

Example 3 with TypeRef

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

the class Analyzer method referToByBinaryName.

public void referToByBinaryName(String binaryClassName) {
    TypeRef ref = descriptors.getTypeRef(binaryClassName);
    referTo(ref);
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef)

Example 4 with TypeRef

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

the class Analyzer method assignable.

public boolean assignable(Clazz annoServiceClazz, Clazz inferredServiceClazz) {
    if (annoServiceClazz == null || inferredServiceClazz == null)
        // we don't know what one of the classes is, assume assignable.
        return true;
    if (annoServiceClazz.equals(inferredServiceClazz))
        return true;
    if (!inferredServiceClazz.isInterface()) {
        if (annoServiceClazz.isInterface())
            return false;
        TypeRef zuper = annoServiceClazz.getSuper();
        if (zuper == null)
            return false;
        try {
            return assignable(findClass(zuper), inferredServiceClazz);
        } catch (Exception e) {
            // can't tell
            return true;
        }
    }
    TypeRef[] intfs = annoServiceClazz.getInterfaces();
    if (intfs != null) {
        for (TypeRef intf : intfs) {
            try {
                if (assignable(findClass(intf), inferredServiceClazz))
                    return true;
            } catch (Exception e) {
                return true;
            }
        }
    }
    TypeRef superType = annoServiceClazz.getSuper();
    if (superType != null) {
        try {
            Clazz zuper = findClass(superType);
            if (zuper != null)
                return assignable(zuper, inferredServiceClazz);
            // cannot analyze super class
            return true;
        } catch (Exception e) {
            // cannot analyze super class
            return true;
        }
    }
    // no more superclasses, not assignable.
    return false;
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) ParseException(java.text.ParseException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 5 with TypeRef

use of aQute.bnd.osgi.Descriptors.TypeRef 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)

Aggregations

TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)42 Clazz (aQute.bnd.osgi.Clazz)12 PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)7 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 Analyzer (aQute.bnd.osgi.Analyzer)5 ClassDataCollector (aQute.bnd.osgi.ClassDataCollector)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 Matcher (java.util.regex.Matcher)5 DeclarativeServicesAnnotationError (aQute.bnd.component.error.DeclarativeServicesAnnotationError)4 Attrs (aQute.bnd.header.Attrs)4 Annotation (aQute.bnd.osgi.Annotation)4 MultiMap (aQute.lib.collections.MultiMap)4 MethodDef (aQute.bnd.osgi.Clazz.MethodDef)3 Instructions (aQute.bnd.osgi.Instructions)3 Resource (aQute.bnd.osgi.Resource)3 Tag (aQute.lib.tag.Tag)3