use of aQute.lib.collections.MultiMap in project bnd by bndtools.
the class JavaElement method classElement.
/**
* Calculate the class element. This requires parsing the class file and
* finding all the methods that were added etc. The parsing will take super
* interfaces and super classes into account. For this reason it maintains a
* queue of classes/interfaces to parse.
*
* @param analyzer
* @param clazz
* @param infos
* @throws Exception
*/
Element classElement(final Clazz clazz) throws Exception {
Element e = cache.get(clazz);
if (e != null)
return e;
final Set<Element> members = Create.set();
final Set<MethodDef> methods = Create.set();
final Set<Clazz.FieldDef> fields = Create.set();
final MultiMap<Clazz.Def, Element> annotations = new MultiMap<Clazz.Def, Element>();
final TypeRef name = clazz.getClassName();
final String fqn = name.getFQN();
final String shortName = name.getShortName();
// Check if this clazz is actually a provider or not
// providers must be listed in the exported package in the
// PROVIDER_TYPE directive.
Instructions matchers = providerMatcher.get(name.getPackageRef());
boolean p = matchers != null && matchers.matches(shortName);
final AtomicBoolean provider = new AtomicBoolean(p);
//
// Check if we already had this clazz in the cache
//
// for super classes
Element before = cache.get(clazz);
if (before != null)
return before;
clazz.parseClassFileWithCollector(new ClassDataCollector() {
boolean memberEnd;
Clazz.FieldDef last;
@Override
public void version(int minor, int major) {
javas.add(Clazz.JAVA.getJava(major, minor));
}
@Override
public void method(MethodDef defined) {
if ((defined.isProtected() || defined.isPublic())) {
last = defined;
methods.add(defined);
} else {
last = null;
}
}
@Override
public void deprecated() {
if (memberEnd)
clazz.setDeprecated(true);
else if (last != null)
last.setDeprecated(true);
}
@Override
public void field(Clazz.FieldDef defined) {
if (defined.isProtected() || defined.isPublic()) {
last = defined;
fields.add(defined);
} else
last = null;
}
@Override
public void constant(Object o) {
if (last != null) {
// Must be accessible now
last.setConstant(o);
}
}
@Override
public void extendsClass(TypeRef name) throws Exception {
String comment = null;
if (!clazz.isInterface())
comment = inherit(members, name);
Clazz c = analyzer.findClass(name);
if ((c == null || c.isPublic()) && !name.isObject())
members.add(new Element(EXTENDS, name.getFQN(), null, MICRO, MAJOR, comment));
}
@Override
public void implementsInterfaces(TypeRef[] names) throws Exception {
// ignore type reordering
Arrays.sort(names);
for (TypeRef name : names) {
String comment = null;
if (clazz.isInterface() || clazz.isAbstract())
comment = inherit(members, name);
members.add(new Element(IMPLEMENTS, name.getFQN(), null, MINOR, MAJOR, comment));
}
}
/**
*/
Set<Element> OBJECT = Create.set();
public String inherit(final Set<Element> members, TypeRef name) throws Exception {
if (name.isObject()) {
if (OBJECT.isEmpty()) {
Clazz c = analyzer.findClass(name);
if (c == null) {
// available
return null;
}
Element s = classElement(c);
for (Element child : s.children) {
if (INHERITED.contains(child.type)) {
String n = child.getName();
if (child.type == METHOD) {
if (n.startsWith("<init>") || "getClass()".equals(child.getName()) || n.startsWith("wait(") || n.startsWith("notify(") || n.startsWith("notifyAll("))
continue;
}
if (isStatic(child))
continue;
OBJECT.add(child);
}
}
}
members.addAll(OBJECT);
} else {
Clazz c = analyzer.findClass(name);
if (c == null) {
return inherit(members, analyzer.getTypeRef("java/lang/Object"));
}
Element s = classElement(c);
for (Element child : s.children) {
if (isStatic(child))
continue;
if (INHERITED.contains(child.type) && !child.name.startsWith("<")) {
members.add(child);
}
}
}
return null;
}
private boolean isStatic(Element child) {
boolean isStatic = child.get("static") != null;
return isStatic;
}
/**
* Deprecated annotations and Provider/Consumer Type (both bnd and
* OSGi) are treated special. Other annotations are turned into a
* tree. Starting with ANNOTATED, and then properties. A property is
* a PROPERTY property or an ANNOTATED property if it is an
* annotation. If it is an array, the key is suffixed with the
* index.
*
* <pre>
* public @interface Outer { Inner[] value(); }
* public @interface Inner { String[] value(); } @Outer(
* { @Inner("1","2"}) } class Xyz {} ANNOTATED Outer
* (CHANGED/CHANGED) ANNOTATED Inner (CHANGED/CHANGED) PROPERTY
* value.0=1 (CHANGED/CHANGED) PROPERTY value.1=2 (CHANGED/CHANGED)
* </pre>
*/
@Override
public void annotation(Annotation annotation) {
if (Deprecated.class.getName().equals(annotation.getName().getFQN())) {
if (memberEnd)
clazz.setDeprecated(true);
else if (last != null)
last.setDeprecated(true);
return;
}
Element e = annotatedToElement(annotation);
if (memberEnd) {
members.add(e);
//
// Check for the provider/consumer. We use strings because
// these are not officially
// released yet
//
String name = annotation.getName().getFQN();
if ("aQute.bnd.annotation.ProviderType".equals(name) || "org.osgi.annotation.versioning.ProviderType".equals(name)) {
provider.set(true);
} else if ("aQute.bnd.annotation.ConsumerType".equals(name) || "org.osgi.annotation.versioning.ConsumerType".equals(name)) {
provider.set(false);
}
} else if (last != null)
annotations.add(last, e);
}
/*
* Return an ANNOTATED element for this annotation. An ANNOTATED
* element contains either PROPERTY children or ANNOTATED children.
*/
private Element annotatedToElement(Annotation annotation) {
Collection<Element> properties = Create.set();
for (String key : annotation.keySet()) {
addAnnotationMember(properties, key, annotation.get(key));
}
return new Element(ANNOTATED, annotation.getName().getFQN(), properties, CHANGED, CHANGED, null);
}
/*
* This method detects 3 cases: An Annotation, which means it
* creates a new child ANNOTATED element, an array, which means it
* will repeat recursively but suffixes the key with the index, or a
* simple value which is turned into a string.
*/
private void addAnnotationMember(Collection<Element> properties, String key, Object member) {
if (member instanceof Annotation) {
properties.add(annotatedToElement((Annotation) member));
} else if (member.getClass().isArray()) {
int l = Array.getLength(member);
for (int i = 0; i < l; i++) {
addAnnotationMember(properties, key + "." + i, Array.get(member, i));
}
} else {
StringBuilder sb = new StringBuilder();
sb.append(key);
sb.append('=');
if (member instanceof String) {
sb.append("'");
sb.append(member);
sb.append("'");
} else
sb.append(member);
properties.add(new Element(PROPERTY, sb.toString(), null, CHANGED, CHANGED, null));
}
}
@Override
public void innerClass(TypeRef innerClass, TypeRef outerClass, String innerName, int innerClassAccessFlags) throws Exception {
Clazz clazz = analyzer.findClass(innerClass);
if (clazz != null)
clazz.setInnerAccess(innerClassAccessFlags);
if (Modifier.isProtected(innerClassAccessFlags) || Modifier.isPublic(innerClassAccessFlags))
return;
notAccessible.add(innerClass);
}
@Override
public void memberEnd() {
memberEnd = true;
}
});
// This is the heart of the semantic versioning. If we
// add or remove a method from an interface then
Delta add;
Delta remove;
Type type;
if (clazz.isInterface())
if (clazz.isAnnotation())
type = ANNOTATION;
else
type = INTERFACE;
else if (clazz.isEnum())
type = ENUM;
else
type = CLASS;
if (type == INTERFACE) {
if (provider.get()) {
// Adding a method for a provider is not an issue
// because it must be aware of the changes
add = MINOR;
// Removing a method influences consumers since they
// tend to call this guy.
remove = MAJOR;
} else {
// Adding a method is a major change
// because the consumer has to implement it
// or the provider will call a non existent
// method on the consumer
add = MAJOR;
// Removing a method is not an issue for
// providers, however, consumers could potentially
// call through this interface :-(
remove = MAJOR;
}
} else {
// Adding a method to a class can never do any harm
// except when the class is extended and the new
// method clashes with the new method. That is
// why API classes in general should be final, at
// least not extended by consumers.
add = MINOR;
// Removing it will likely hurt consumers
remove = MAJOR;
}
for (MethodDef m : methods) {
if (m.isSynthetic()) {
// Ignore synthetic methods
continue;
}
Collection<Element> children = annotations.get(m);
if (children == null)
children = new HashSet<Element>();
access(children, m.getAccess(), m.isDeprecated(), provider.get());
// in a final class.
if (clazz.isFinal())
children.remove(FINAL);
children.add(getReturn(m.getType()));
if (clazz.isInterface() && !m.isAbstract()) {
//
// We have a Java 8 default method!
// Such a method is always a minor update
//
add = MINOR;
}
String signature = m.getName() + toString(m.getPrototype());
Element member = new Element(METHOD, signature, children, add, provider.get() && !m.isPublic() ? MINOR : remove, null);
if (!members.add(member)) {
members.remove(member);
members.add(member);
}
}
for (Clazz.FieldDef f : fields) {
if (f.isSynthetic()) {
// Ignore synthetic fields
continue;
}
Collection<Element> children = annotations.get(f);
if (children == null)
children = new HashSet<Element>();
// Fields can have a constant value, this is a new element
if (f.getConstant() != null) {
children.add(new Element(CONSTANT, f.getConstant().toString(), null, CHANGED, CHANGED, null));
}
access(children, f.getAccess(), f.isDeprecated(), provider.get());
children.add(getReturn(f.getType()));
Element member = new Element(FIELD, f.getName(), children, MINOR, provider.get() && !f.isPublic() ? MINOR : MAJOR, null);
if (!members.add(member)) {
members.remove(member);
members.add(member);
}
}
access(members, clazz.getAccess(), clazz.isDeprecated(), provider.get());
// And make the result
Element s = new Element(type, fqn, members, MINOR, MAJOR, null);
cache.put(clazz, s);
return s;
}
use of aQute.lib.collections.MultiMap in project bnd by bndtools.
the class BaselineCommands method _schema.
/**
* Create a schema of a set of jars outling the packages and their versions.
* This will create a list of packages with multiple versions, link to their
* specifications, and the deltas between versions.
*
* <pre>
* bnd package schema
* <file.jar>*
* </pre>
*
* @param opts
* @throws Exception
*/
public void _schema(schemaOptions opts) throws Exception {
MultiMap<String, PSpec> map = new MultiMap<String, PSpec>();
Tag top = new Tag("jschema");
int n = 1000;
for (String spec : opts._arguments()) {
File f = bnd.getFile(spec);
if (!f.isFile()) {
bnd.messages.NoSuchFile_(f);
} else {
// For each specification jar we found
logger.debug("spec {}", f);
// spec
Jar jar = new Jar(f);
Manifest m = jar.getManifest();
Attributes main = m.getMainAttributes();
Tag specTag = new Tag(top, "specification");
specTag.addAttribute("jar", spec);
specTag.addAttribute("name", main.getValue("Specification-Name"));
specTag.addAttribute("title", main.getValue("Specification-Title"));
specTag.addAttribute("jsr", main.getValue("Specification-JSR"));
specTag.addAttribute("url", main.getValue("Specification-URL"));
specTag.addAttribute("version", main.getValue("Specification-Version"));
specTag.addAttribute("vendor", main.getValue("Specification-Vendor"));
specTag.addAttribute("id", n);
specTag.addContent(main.getValue(Constants.BUNDLE_DESCRIPTION));
Parameters exports = OSGiHeader.parseHeader(m.getMainAttributes().getValue(Constants.EXPORT_PACKAGE));
// Create a map with versions. Ensure import ranges overwrite
// the
// exported versions
Parameters versions = new Parameters();
versions.putAll(exports);
versions.putAll(OSGiHeader.parseHeader(m.getMainAttributes().getValue(Constants.IMPORT_PACKAGE)));
Analyzer analyzer = new Analyzer();
analyzer.setJar(jar);
analyzer.analyze();
Tree tree = differ.tree(analyzer);
for (Entry<String, Attrs> entry : exports.entrySet()) {
// For each exported package in the specification JAR
Attrs attrs = entry.getValue();
String packageName = entry.getKey();
PackageRef packageRef = analyzer.getPackageRef(packageName);
String version = attrs.get(Constants.VERSION_ATTRIBUTE);
PSpec pspec = new PSpec();
pspec.packageName = packageName;
pspec.version = new Version(version);
pspec.id = n;
pspec.attrs = attrs;
pspec.tree = tree;
Collection<PackageRef> uses = analyzer.getUses().get(packageRef);
if (uses != null) {
for (PackageRef x : uses) {
if (x.isJava())
continue;
String imp = x.getFQN();
if (imp.equals(packageName))
continue;
String v = null;
if (versions.containsKey(imp))
v = versions.get(imp).get(Constants.VERSION_ATTRIBUTE);
pspec.uses.put(imp, v);
}
}
map.add(packageName, pspec);
}
jar.close();
n++;
}
}
// We now gather all the information about all packages in the map.
// Next phase is generating the XML. Sorting the packages is
// important because XSLT is brain dead.
SortedList<String> names = new SortedList<String>(map.keySet());
Tag packagesTag = new Tag(top, "packages");
Tag baselineTag = new Tag(top, "baseline");
for (String pname : names) {
// For each distinct package name
SortedList<PSpec> specs = new SortedList<PSpec>(map.get(pname));
PSpec older = null;
Parameters olderExport = null;
for (PSpec newer : specs) {
// For each package in the total set
Tag pack = new Tag(packagesTag, "package");
pack.addAttribute("name", newer.packageName);
pack.addAttribute("version", newer.version);
pack.addAttribute("spec", newer.id);
Parameters newerExport = new Parameters();
newerExport.put(pname, newer.attrs);
if (older != null) {
String compareId = newer.packageName + "-" + newer.id + "-" + older.id;
pack.addAttribute("delta", compareId);
logger.debug(" newer={} older={}", newerExport, olderExport);
Set<Info> infos = baseline.baseline(newer.tree, newerExport, older.tree, olderExport, new Instructions(pname));
for (Info info : infos) {
Tag tag = getTag(info);
tag.addAttribute("id", compareId);
tag.addAttribute("newerSpec", newer.id);
tag.addAttribute("olderSpec", older.id);
baselineTag.addContent(tag);
}
older.tree = null;
older.attrs = null;
older = newer;
}
for (Entry<String, String> uses : newer.uses.entrySet()) {
Tag reference = new Tag(pack, "import");
reference.addAttribute("name", uses.getKey());
reference.addAttribute("version", uses.getValue());
}
older = newer;
olderExport = newerExport;
}
}
String o = opts.output("schema.xml");
File of = bnd.getFile(o);
File pof = of.getParentFile();
IO.mkdirs(pof);
try (PrintWriter pw = IO.writer(of, UTF_8)) {
pw.print("<?xml version='1.0' encoding='UTF-8'?>\n");
top.print(0, pw);
}
if (opts.xsl() != null) {
URL home = bnd.getBase().toURI().toURL();
URL xslt = new URL(home, opts.xsl());
String path = of.getAbsolutePath();
if (path.endsWith(".xml"))
path = path.substring(0, path.length() - 4);
path = path + ".html";
File html = new File(path);
logger.debug("xslt {} {} {} {}", xslt, of, html, html.exists());
try (OutputStream out = IO.outputStream(html);
InputStream in = xslt.openStream()) {
Transformer transformer = transformerFactory.newTransformer(new StreamSource(in));
transformer.transform(new StreamSource(of), new StreamResult(out));
}
}
}
use of aQute.lib.collections.MultiMap in project bnd by bndtools.
the class RepoCommand method _diff.
@Description("Diff jars (or show tree)")
public void _diff(diffOptions options) throws UnsupportedEncodingException, IOException, Exception {
List<String> args = options._arguments();
String newer = args.remove(0);
String older = args.size() > 0 ? args.remove(0) : null;
RepositoryPlugin rnewer = findRepo(newer);
RepositoryPlugin rolder = older == null ? null : findRepo(older);
if (rnewer == null) {
bnd.messages.NoSuchRepository_(newer);
return;
}
if (older != null && rolder == null) {
bnd.messages.NoSuchRepository_(newer);
return;
}
PrintWriter pw = IO.writer(bnd.out, UTF_8);
Tree tNewer = RepositoryElement.getTree(rnewer);
if (rolder == null) {
if (options.json())
codec.enc().to(pw).put(tNewer.serialize()).flush();
else
DiffCommand.show(pw, tNewer, 0);
} else {
Tree tOlder = RepositoryElement.getTree(rolder);
Diff diff = new DiffImpl(tNewer, tOlder);
MultiMap<String, String> map = new MultiMap<String, String>();
for (Diff bsn : diff.getChildren()) {
for (Diff version : bsn.getChildren()) {
if (version.getDelta() == Delta.UNCHANGED)
continue;
if (options.remove() == false && options.added() == false || (//
options.remove() && version.getDelta() == Delta.REMOVED) || (options.added() && version.getDelta() == Delta.ADDED)) {
map.add(bsn.getName(), version.getName());
}
}
}
if (options.json())
codec.enc().to(pw).put(map).flush();
else if (!options.diff())
bnd.printMultiMap(map);
else
DiffCommand.show(pw, diff, 0, !options.full());
}
pw.flush();
}
use of aQute.lib.collections.MultiMap in project bnd by bndtools.
the class SubsystemExporter method export.
@Override
public Map.Entry<String, Resource> export(String type, final Project project, Map<String, String> options) throws Exception {
Jar jar = new Jar(".");
project.addClose(jar);
Manifest manifest = new Manifest();
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
manifest.getMainAttributes().putValue("Subsystem-ManifestVersion", "1");
List<Container> distro = project.getBundles(Strategy.LOWEST, project.getProperty(Constants.DISTRO), Constants.DISTRO);
List<File> distroFiles = getBundles(distro, project);
List<File> files = getBundles(project.getRunbundles(), project);
MultiMap<String, Attrs> imports = new MultiMap<String, Attrs>();
MultiMap<String, Attrs> exports = new MultiMap<String, Attrs>();
Parameters requirements = new Parameters();
Parameters capabilities = new Parameters();
for (File file : files) {
Domain domain = Domain.domain(file);
String bsn = domain.getBundleSymbolicName().getKey();
String version = domain.getBundleVersion();
for (Entry<String, Attrs> e : domain.getImportPackage().entrySet()) {
imports.add(e.getKey(), e.getValue());
}
for (Entry<String, Attrs> e : domain.getExportPackage().entrySet()) {
exports.add(e.getKey(), e.getValue());
}
String path = bsn + "-" + version + ".jar";
jar.putResource(path, new FileResource(file));
}
headers(project, manifest.getMainAttributes());
set(manifest.getMainAttributes(), SUBSYSTEM_TYPE, OSGI_SUBSYSTEM_FEATURE);
String ssn = project.getName();
Collection<String> bsns = project.getBsns();
if (bsns.size() > 0) {
ssn = bsns.iterator().next();
}
set(manifest.getMainAttributes(), SUBSYSTEM_SYMBOLIC_NAME, ssn);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
manifest.write(bout);
jar.putResource(OSGI_INF_SUBSYSTEM_MF, new EmbeddedResource(bout.toByteArray(), 0));
final JarResource jarResource = new JarResource(jar);
final String name = ssn + ".esa";
return new Map.Entry<String, Resource>() {
@Override
public String getKey() {
return name;
}
@Override
public Resource getValue() {
return jarResource;
}
@Override
public Resource setValue(Resource arg0) {
throw new UnsupportedOperationException();
}
};
}
use of aQute.lib.collections.MultiMap in project bnd by bndtools.
the class AggregateRepository method findProviders.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Map<Requirement, Collection<Capability>> findProviders(Collection<? extends Requirement> requirements) {
MultiMap<Requirement, Capability> result = new MultiMap<>();
for (Repository repository : repositories) {
Map<Requirement, Collection<Capability>> capabilities = repository.findProviders(requirements);
result.addAll(capabilities);
}
return (Map) result;
}
Aggregations