Search in sources :

Example 1 with XMLAttributeFinder

use of aQute.bnd.xmlattribute.XMLAttributeFinder in project bnd by bndtools.

the class MetatypeAnnotations method analyzeJar.

public boolean analyzeJar(Analyzer analyzer) throws Exception {
    this.minVersion = MetatypeVersion.VERSION_1_2;
    Parameters header = OSGiHeader.parseHeader(analyzer.getProperty(Constants.METATYPE_ANNOTATIONS, "*"));
    if (header.size() == 0)
        return false;
    Parameters optionsHeader = OSGiHeader.parseHeader(analyzer.getProperty(Constants.METATYPE_ANNOTATIONS_OPTIONS));
    EnumSet<Options> options = EnumSet.noneOf(Options.class);
    for (Map.Entry<String, Attrs> entry : optionsHeader.entrySet()) {
        try {
            Options.parseOption(entry, options, this);
        } catch (IllegalArgumentException e) {
            analyzer.error("Unrecognized %s value %s with attributes %s, expected values are %s", Constants.METATYPE_ANNOTATIONS_OPTIONS, entry.getKey(), entry.getValue(), EnumSet.allOf(Options.class));
        }
    }
    Map<TypeRef, OCDDef> classToOCDMap = new HashMap<TypeRef, OCDDef>();
    Set<String> ocdIds = new HashSet<String>();
    Set<String> pids = new HashSet<String>();
    Instructions instructions = new Instructions(header);
    XMLAttributeFinder finder = new XMLAttributeFinder(analyzer);
    List<Clazz> list = Create.list();
    for (Clazz c : analyzer.getClassspace().values()) {
        for (Instruction instruction : instructions.keySet()) {
            if (instruction.matches(c.getFQN())) {
                if (!instruction.isNegated()) {
                    list.add(c);
                    OCDDef definition = OCDReader.getOCDDef(c, analyzer, options, finder, minVersion);
                    if (definition != null) {
                        classToOCDMap.put(c.getClassName(), definition);
                    }
                }
                break;
            }
        }
    }
    // process Designate annotations after OCD annotations
    for (Clazz c : list) {
        DesignateReader.getDesignate(c, analyzer, classToOCDMap, finder);
    }
    for (Map.Entry<TypeRef, OCDDef> entry : classToOCDMap.entrySet()) {
        TypeRef c = entry.getKey();
        OCDDef definition = entry.getValue();
        definition.prepare(analyzer);
        if (!ocdIds.add(definition.id)) {
            analyzer.error("Duplicate OCD id %s from class %s; known ids %s", definition.id, c.getFQN(), ocdIds);
        }
        for (DesignateDef dDef : definition.designates) {
            if (dDef.pid != null && !pids.add(dDef.pid)) {
                analyzer.error("Duplicate pid %s from class %s", dDef.pid, c.getFQN());
            }
        }
        String name = "OSGI-INF/metatype/" + analyzer.validResourcePath(definition.id, "Invalid resource name") + ".xml";
        analyzer.getJar().putResource(name, new TagResource(definition.getTag()));
    }
    return false;
}
Also used : Parameters(aQute.bnd.header.Parameters) HashMap(java.util.HashMap) TagResource(aQute.bnd.component.TagResource) TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Attrs(aQute.bnd.header.Attrs) Instructions(aQute.bnd.osgi.Instructions) Instruction(aQute.bnd.osgi.Instruction) XMLAttributeFinder(aQute.bnd.xmlattribute.XMLAttributeFinder) Clazz(aQute.bnd.osgi.Clazz) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with XMLAttributeFinder

use of aQute.bnd.xmlattribute.XMLAttributeFinder in project bnd by bndtools.

the class ClazzTest method testRecursiveAnnotation.

public void testRecursiveAnnotation() throws Exception {
    File file = IO.getFile("bin/test/ClazzTest$RecursiveAnno.class");
    try (Analyzer analyzer = new Analyzer()) {
        Clazz clazz = new Clazz(analyzer, file.getPath(), new FileResource(file));
        clazz.parseClassFile();
        analyzer.getClassspace().put(clazz.getClassName(), clazz);
        AnnotationReader.getDefinition(clazz, analyzer, EnumSet.noneOf(DSAnnotations.Options.class), new XMLAttributeFinder(analyzer), AnnotationReader.V1_3);
    }
}
Also used : XMLAttributeFinder(aQute.bnd.xmlattribute.XMLAttributeFinder) FileResource(aQute.bnd.osgi.FileResource) Clazz(aQute.bnd.osgi.Clazz) Analyzer(aQute.bnd.osgi.Analyzer) File(java.io.File)

Example 3 with XMLAttributeFinder

use of aQute.bnd.xmlattribute.XMLAttributeFinder in project bnd by bndtools.

the class DSAnnotations method analyzeJar.

public boolean analyzeJar(Analyzer analyzer) throws Exception {
    Parameters header = OSGiHeader.parseHeader(analyzer.getProperty(Constants.DSANNOTATIONS, "*"));
    if (header.size() == 0)
        return false;
    minVersion = AnnotationReader.V1_3;
    Parameters optionsHeader = OSGiHeader.parseHeader(analyzer.mergeProperties(Constants.DSANNOTATIONS_OPTIONS));
    EnumSet<Options> options = EnumSet.noneOf(Options.class);
    for (Map.Entry<String, Attrs> entry : optionsHeader.entrySet()) {
        try {
            Options.parseOption(entry, options, this);
        } catch (IllegalArgumentException e) {
            analyzer.error("Unrecognized %s value %s with attributes %s, expected values are %s", Constants.DSANNOTATIONS_OPTIONS, entry.getKey(), entry.getValue(), EnumSet.allOf(Options.class));
        }
    }
    // obsolete but backwards compatible, use the options instead
    if (Processor.isTrue(analyzer.getProperty("-dsannotations-inherit")))
        options.add(Options.inherit);
    if (Processor.isTrue(analyzer.getProperty("-ds-felix-extensions")))
        options.add(Options.felixExtensions);
    Instructions instructions = new Instructions(header);
    Collection<Clazz> list = analyzer.getClassspace().values();
    String sc = analyzer.getProperty(Constants.SERVICE_COMPONENT);
    List<String> names = new ArrayList<String>();
    if (sc != null && sc.trim().length() > 0)
        names.add(sc);
    TreeSet<String> provides = new TreeSet<String>();
    TreeSet<String> requires = new TreeSet<String>();
    Version maxVersion = AnnotationReader.V1_0;
    XMLAttributeFinder finder = new XMLAttributeFinder(analyzer);
    boolean componentProcessed = false;
    for (Clazz c : list) {
        for (Instruction instruction : instructions.keySet()) {
            if (instruction.matches(c.getFQN())) {
                if (instruction.isNegated())
                    break;
                ComponentDef definition = AnnotationReader.getDefinition(c, analyzer, options, finder, minVersion);
                if (definition != null) {
                    componentProcessed = true;
                    definition.sortReferences();
                    definition.prepare(analyzer);
                    String name = "OSGI-INF/" + analyzer.validResourcePath(definition.name, "Invalid component name") + ".xml";
                    names.add(name);
                    analyzer.getJar().putResource(name, new TagResource(definition.getTag()));
                    if (definition.service != null && !options.contains(Options.nocapabilities)) {
                        String[] objectClass = new String[definition.service.length];
                        for (int i = 0; i < definition.service.length; i++) {
                            Descriptors.TypeRef tr = definition.service[i];
                            objectClass[i] = tr.getFQN();
                        }
                        Arrays.sort(objectClass);
                        addServiceCapability(objectClass, provides);
                    }
                    if (!options.contains(Options.norequirements)) {
                        MergedRequirement serviceReqMerge = new MergedRequirement("osgi.service");
                        for (ReferenceDef ref : definition.references.values()) {
                            addServiceRequirement(ref, serviceReqMerge);
                        }
                        requires.addAll(serviceReqMerge.toStringList());
                    }
                    maxVersion = ComponentDef.max(maxVersion, definition.version);
                }
            }
        }
    }
    if (componentProcessed && (options.contains(Options.extender) || (maxVersion.compareTo(AnnotationReader.V1_3) >= 0))) {
        maxVersion = ComponentDef.max(maxVersion, AnnotationReader.V1_3);
        addExtenderRequirement(requires, maxVersion);
    }
    sc = Processor.append(names.toArray(new String[0]));
    analyzer.setProperty(Constants.SERVICE_COMPONENT, sc);
    updateHeader(analyzer, Constants.REQUIRE_CAPABILITY, requires);
    updateHeader(analyzer, Constants.PROVIDE_CAPABILITY, provides);
    return false;
}
Also used : Attrs(aQute.bnd.header.Attrs) ArrayList(java.util.ArrayList) Instruction(aQute.bnd.osgi.Instruction) XMLAttributeFinder(aQute.bnd.xmlattribute.XMLAttributeFinder) Version(aQute.bnd.version.Version) TreeSet(java.util.TreeSet) Clazz(aQute.bnd.osgi.Clazz) Descriptors(aQute.bnd.osgi.Descriptors) Parameters(aQute.bnd.header.Parameters) Instructions(aQute.bnd.osgi.Instructions) Map(java.util.Map)

Aggregations

Clazz (aQute.bnd.osgi.Clazz)3 XMLAttributeFinder (aQute.bnd.xmlattribute.XMLAttributeFinder)3 Attrs (aQute.bnd.header.Attrs)2 Parameters (aQute.bnd.header.Parameters)2 Instruction (aQute.bnd.osgi.Instruction)2 Instructions (aQute.bnd.osgi.Instructions)2 Map (java.util.Map)2 TagResource (aQute.bnd.component.TagResource)1 Analyzer (aQute.bnd.osgi.Analyzer)1 Descriptors (aQute.bnd.osgi.Descriptors)1 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)1 FileResource (aQute.bnd.osgi.FileResource)1 Version (aQute.bnd.version.Version)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1