Search in sources :

Example 6 with Analyzer

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

Example 7 with Analyzer

use of aQute.bnd.osgi.Analyzer in project bnd by bndtools.

the class bnd method _create.

/**
	 * Create jar file
	 * 
	 * <pre>
	 *  jar c[v0M]f jarfile [-C dir] inputfiles [-Joption]
	 * jar c[v0]mf manifest jarfile [-C dir] inputfiles [-Joption] jar c[v0M]
	 * [-C dir] inputfiles [-Joption] jar c[v0]m manifest [-C dir] inputfiles
	 * [-Joption]
	 * </pre>
	 * 
	 * @param options
	 * @throws Exception
	 */
@Description("Create jar, used to support backward compatible java jar commands")
public void _create(createOptions options) throws Exception {
    Jar jar = new Jar("dot");
    File dir = getBase().getAbsoluteFile();
    String sdir = options.cdir();
    if (sdir != null)
        dir = getFile(sdir);
    if (options._arguments().isEmpty())
        add(jar, dir, ".", options.verbose());
    else
        for (String f : options._arguments()) {
            f = f.replace(File.separatorChar, '/');
            add(jar, dir, f, options.verbose());
        }
    String manifest = options.manifest();
    if (manifest != null) {
        if (options.verbose())
            err.printf("Adding manifest from %s\n", manifest);
        jar.setManifest(getFile(manifest));
    }
    if (options.Manifest()) {
        jar.setManifest((Manifest) null);
    } else {
        if (options.wrap()) {
            Analyzer w = new Analyzer(this);
            addClose(w);
            w.setBase(getBase());
            w.use(this);
            w.setDefaults(options.bsn(), options.version());
            w.calcManifest();
            getInfo(w);
            w.setJar((Jar) null);
            w.close();
        }
    }
    if (options.nocompression())
        jar.setCompression(Jar.Compression.STORE);
    if (isOk()) {
        String jarFile = options.file();
        if (jarFile == null)
            jar.write(System.out);
        else
            jar.write(jarFile);
    }
    jar.close();
}
Also used : Jar(aQute.bnd.osgi.Jar) Analyzer(aQute.bnd.osgi.Analyzer) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 8 with Analyzer

use of aQute.bnd.osgi.Analyzer in project bnd by bndtools.

the class bnd method _ees.

/**
	 * Show the class versions used in a JAR
	 * 
	 * @throws Exception
	 */
@Description("Show the Execution Environments of a JAR")
public void _ees(EEOptions options) throws Exception {
    for (String path : options._arguments()) {
        File f = getFile(path);
        if (!f.isFile()) {
            error("Not a file");
        } else {
            try (Analyzer a = new Analyzer(this)) {
                a.setJar(f);
                a.analyze();
                out.printf("%s %s%n", a.getJar().getName(), a.getEEs());
            }
        }
    }
}
Also used : Analyzer(aQute.bnd.osgi.Analyzer) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 9 with Analyzer

use of aQute.bnd.osgi.Analyzer in project bnd by bndtools.

the class AnalyzerTest method testDsSkipOsgiImport.

public static void testDsSkipOsgiImport() throws Exception {
    Properties base = new Properties();
    base.put(Analyzer.IMPORT_PACKAGE, "!org.osgi.*, *");
    base.put(Analyzer.EXPORT_PACKAGE, "*;-noimport:=true");
    File tmp = IO.getFile("jar/ds.jar");
    try (Analyzer h = new Analyzer()) {
        h.setJar(tmp);
        h.setProperties(base);
        h.calcManifest().write(System.err);
        assertPresent(h.getImports().keySet(), "org.xml.sax, " + " javax.xml.parsers," + " org.xml.sax.helpers," + " org.eclipse.osgi.util");
        System.err.println("IMports " + h.getImports());
        assertNotPresent(h.getImports().keySet(), "org.osgi.service.packageadmin, " + "org.osgi.service.log," + " org.osgi.framework," + " org.osgi.util.tracker, " + "org.osgi.service.component, " + "org.osgi.service.cm");
        assertPresent(h.getExports().keySet(), "org.eclipse.equinox.ds.parser, " + "org.eclipse.equinox.ds.tracker, " + "org.eclipse.equinox.ds, " + "org.eclipse.equinox.ds.instance, " + "org.eclipse.equinox.ds.model, " + "org.eclipse.equinox.ds.resolver, " + "org.eclipse.equinox.ds.workqueue");
    }
}
Also used : Properties(java.util.Properties) Analyzer(aQute.bnd.osgi.Analyzer) File(java.io.File)

Example 10 with Analyzer

use of aQute.bnd.osgi.Analyzer in project bnd by bndtools.

the class AnalyzerTest method testClasspath.

public static void testClasspath() throws Exception {
    Properties base = new Properties();
    base.put(Analyzer.IMPORT_PACKAGE, "*");
    base.put(Analyzer.EXPORT_PACKAGE, "*;-noimport:=true");
    File tmp = IO.getFile("jar/ds.jar");
    File osgi = IO.getFile("jar/osgi.jar");
    try (Analyzer h = new Analyzer()) {
        h.setJar(tmp);
        h.setProperties(base);
        h.setClasspath(new File[] { osgi });
        h.calcManifest().write(System.err);
        assertEquals("Version from osgi.jar", "[1.2,2)", get(h.getImports(), h.getPackageRef("org.osgi.service.packageadmin"), "version"));
        assertEquals("Version from osgi.jar", "[1.3,2)", get(h.getImports(), h.getPackageRef("org.osgi.util.tracker"), "version"));
        assertEquals("Version from osgi.jar", null, get(h.getImports(), h.getPackageRef("org.xml.sax"), "version"));
    }
}
Also used : Properties(java.util.Properties) Analyzer(aQute.bnd.osgi.Analyzer) File(java.io.File)

Aggregations

Analyzer (aQute.bnd.osgi.Analyzer)55 File (java.io.File)21 Clazz (aQute.bnd.osgi.Clazz)14 Properties (java.util.Properties)14 Manifest (java.util.jar.Manifest)12 Jar (aQute.bnd.osgi.Jar)11 PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)9 ClassDataCollector (aQute.bnd.osgi.ClassDataCollector)8 Parameters (aQute.bnd.header.Parameters)7 FileResource (aQute.bnd.osgi.FileResource)7 FileInputStream (java.io.FileInputStream)6 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)5 Description (aQute.lib.getopt.Description)5 Attrs (aQute.bnd.header.Attrs)4 Descriptors (aQute.bnd.osgi.Descriptors)4 Packages (aQute.bnd.osgi.Packages)4 Resource (aQute.bnd.osgi.Resource)4 Map (java.util.Map)4 PomFromManifest (aQute.bnd.maven.PomFromManifest)3 Domain (aQute.bnd.osgi.Domain)3