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(), "<");
}
}
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();
}
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());
}
}
}
}
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");
}
}
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"));
}
}
Aggregations