Search in sources :

Example 21 with Clazz

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

the class Classpath method visit.

/**
	 * Visit each class on the class path.
	 * 
	 * @param visitor the visitor
	 */
public void visit(ClassVisitor visitor) throws Exception {
    try (Analyzer analyzer = new Analyzer()) {
        for (File f : entries) {
            try (Jar jar = new Jar(f)) {
                for (String path : jar.getResources().keySet()) {
                    if (path.endsWith(".class")) {
                        Resource r = jar.getResource(path);
                        Clazz c = new Clazz(analyzer, path, r);
                        c.parseClassFile();
                        visitor.visit(c);
                    }
                }
            }
        }
    }
}
Also used : Resource(aQute.bnd.osgi.Resource) Jar(aQute.bnd.osgi.Jar) Clazz(aQute.bnd.osgi.Clazz) Analyzer(aQute.bnd.osgi.Analyzer) File(java.io.File)

Example 22 with Clazz

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

the class Target method testComponentReader.

public static void testComponentReader() throws Exception {
    Analyzer analyzer = new Analyzer();
    File f = IO.getFile("bin/test/AnnotationsTest$MyComponent.class");
    Clazz c = new Clazz(analyzer, "test.AnnotationsTest.MyComponent", new FileResource(f));
    @SuppressWarnings("restriction") Map<String, String> map = ComponentAnnotationReader.getDefinition(c);
    System.err.println(map);
    assertEquals("mycomp", map.get("name:"));
    assertEquals("true", map.get("servicefactory:"));
    assertEquals("activatex", map.get("activate:"));
    assertEquals("deactivatex", map.get("deactivate:"));
    assertEquals("modifiedx", map.get("modified:"));
    assertEquals("org.osgi.service.log.LogService(abc=3)~", map.get("log/setLog"));
    assertEquals("org.osgi.service.packageadmin.PackageAdmin", map.get("packageAdmin/setPackageAdmin"));
    assertEquals("aprop= a prop ,aprop2=    really dumb value   ! ", map.get("properties:"));
}
Also used : FileResource(aQute.bnd.osgi.FileResource) Clazz(aQute.bnd.osgi.Clazz) Analyzer(aQute.bnd.osgi.Analyzer) File(java.io.File)

Example 23 with Clazz

use of aQute.bnd.osgi.Clazz 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 24 with Clazz

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

the class OCDReader method parseExtends.

private void parseExtends(Clazz clazz) {
    TypeRef[] inherits = clazz.getInterfaces();
    if (inherits != null) {
        for (TypeRef typeRef : inherits) {
            if (!typeRef.isJava() && analyzed.add(typeRef)) {
                try {
                    Clazz inherit = analyzer.findClass(typeRef);
                    if (inherit != null) {
                        inherit.parseClassFileWithCollector(new OCDDataCollector(ocd));
                        parseExtends(inherit);
                    } else {
                        analyzer.error("Could not obtain super class %s of class %s", typeRef.getFQN(), clazz.getClassName().getFQN());
                    }
                } catch (Exception e) {
                    analyzer.exception(e, "Could not obtain super class %s of class %s; exception %s", typeRef.getFQN(), clazz.getClassName().getFQN(), e);
                }
            }
        }
    }
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Clazz(aQute.bnd.osgi.Clazz)

Example 25 with Clazz

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

the class MetaTypeReader method parseOptionValues.

private String[] parseOptionValues(Clazz c) throws Exception {
    final List<String> values = Create.list();
    c.parseClassFileWithCollector(new ClassDataCollector() {

        @Override
        public void field(Clazz.FieldDef def) {
            if (def.isEnum()) {
                values.add(def.getName());
            }
        }
    });
    return values.toArray(new String[0]);
}
Also used : Clazz(aQute.bnd.osgi.Clazz) ClassDataCollector(aQute.bnd.osgi.ClassDataCollector)

Aggregations

Clazz (aQute.bnd.osgi.Clazz)56 Analyzer (aQute.bnd.osgi.Analyzer)14 ClassDataCollector (aQute.bnd.osgi.ClassDataCollector)14 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)12 PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)8 InputStream (java.io.InputStream)8 FileResource (aQute.bnd.osgi.FileResource)7 File (java.io.File)7 HashMap (java.util.HashMap)7 FileInputStream (java.io.FileInputStream)6 Resource (aQute.bnd.osgi.Resource)5 MethodDef (aQute.bnd.osgi.Clazz.MethodDef)4 Descriptors (aQute.bnd.osgi.Descriptors)4 Instruction (aQute.bnd.osgi.Instruction)4 Jar (aQute.bnd.osgi.Jar)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 TreeSet (java.util.TreeSet)4 Parameters (aQute.bnd.header.Parameters)3