Search in sources :

Example 26 with TypeRef

use of aQute.bnd.osgi.Descriptors.TypeRef in project bnd by bndtools.

the class ComponentAnnotationReader method annotation.

@Override
public void annotation(Annotation annotation) {
    String fqn = annotation.getName().getFQN();
    if (fqn.equals("aQute.bnd.annotation.component.Component")) {
        if (!mismatchedAnnotations.isEmpty()) {
            String componentName = annotation.get(Component.NAME);
            componentName = (componentName == null) ? className.getFQN() : componentName;
            for (Entry<String, List<DeclarativeServicesAnnotationError>> e : mismatchedAnnotations.entrySet()) {
                for (DeclarativeServicesAnnotationError errorDetails : e.getValue()) {
                    if (errorDetails.fieldName != null) {
                        reporter.error("The DS component %s uses bnd annotations to declare it as a component, but also uses the standard DS annotation: %s on field %s. It is an error to mix these two types of annotations", componentName, e.getKey(), errorDetails.fieldName).details(errorDetails);
                    } else if (errorDetails.methodName != null) {
                        reporter.error("The DS component %s uses bnd annotations to declare it as a component, but also uses the standard DS annotation: %s on method %s with signature %s. It is an error to mix these two types of annotations", componentName, e.getKey(), errorDetails.methodName, errorDetails.methodSignature).details(errorDetails);
                    } else {
                        reporter.error("The DS component %s uses bnd annotations to declare it as a component, but also uses the standard DS annotation: %s. It is an error to mix these two types of annotations", componentName, e.getKey()).details(errorDetails);
                    }
                }
            }
            return;
        }
        set(COMPONENT_NAME, annotation.get(Component.NAME), "<>");
        set(COMPONENT_FACTORY, annotation.get(Component.FACTORY), false);
        setBoolean(COMPONENT_ENABLED, annotation.get(Component.ENABLED), true);
        setBoolean(COMPONENT_IMMEDIATE, annotation.get(Component.IMMEDIATE), false);
        setBoolean(COMPONENT_SERVICEFACTORY, annotation.get(Component.SERVICEFACTORY), false);
        if (annotation.get(Component.DESIGNATE) != null) {
            TypeRef configs = annotation.get(Component.DESIGNATE);
            if (configs != null) {
                set(COMPONENT_DESIGNATE, configs.getFQN(), "");
            }
        }
        if (annotation.get(Component.DESIGNATE_FACTORY) != null) {
            TypeRef configs = annotation.get(Component.DESIGNATE_FACTORY);
            if (configs != null) {
                set(COMPONENT_DESIGNATEFACTORY, configs.getFQN(), "");
            }
        }
        setVersion((String) annotation.get(Component.VERSION));
        String configurationPolicy = annotation.get(Component.CONFIGURATION_POLICY);
        if (configurationPolicy != null)
            set(COMPONENT_CONFIGURATION_POLICY, configurationPolicy.toLowerCase(), "");
        doProperties(annotation);
        Object[] provides = (Object[]) annotation.get(Component.PROVIDE);
        String[] p;
        if (provides == null) {
            // fqn.
            if (interfaces != null) {
                List<String> result = new ArrayList<String>();
                for (int i = 0; i < interfaces.length; i++) {
                    if (!interfaces[i].getBinary().equals("scala/ScalaObject"))
                        result.add(interfaces[i].getFQN());
                }
                p = result.toArray(EMPTY);
            } else
                p = EMPTY;
        } else {
            // We have explicit interfaces set
            p = new String[provides.length];
            for (int i = 0; i < provides.length; i++) {
                p[i] = ((TypeRef) provides[i]).getFQN();
            }
        }
        if (p.length > 0) {
            set(COMPONENT_PROVIDE, Processor.join(Arrays.asList(p)), "<>");
        }
    } else if (fqn.equals("aQute.bnd.annotation.component.Activate")) {
        if (!checkMethod())
            setVersion(V1_1);
        if (!ACTIVATEDESCRIPTOR.matcher(method.getDescriptor().toString()).matches())
            reporter.error("Activate method for %s does not have an acceptable prototype, only Map, ComponentContext, or BundleContext is allowed. Found: %s", className, method.getDescriptor()).details(new DeclarativeServicesAnnotationError(className.getFQN(), method.getName(), method.getDescriptor().toString(), ErrorType.ACTIVATE_SIGNATURE_ERROR));
        if (method.getName().equals("activate") && OLDACTIVATEDESCRIPTOR.matcher(method.getDescriptor().toString()).matches()) {
        // this is the default!
        } else {
            setVersion(V1_1);
            set(COMPONENT_ACTIVATE, method, "<>");
        }
    } else if (fqn.equals("aQute.bnd.annotation.component.Deactivate")) {
        if (!checkMethod())
            setVersion(V1_1);
        if (!ACTIVATEDESCRIPTOR.matcher(method.getDescriptor().toString()).matches())
            reporter.error("Deactivate method for %s does not have an acceptable prototype, only Map, ComponentContext, or BundleContext is allowed. Found: %s", className, method.getDescriptor()).details(new DeclarativeServicesAnnotationError(className.getFQN(), method.getName(), method.getDescriptor().toString(), ErrorType.DEACTIVATE_SIGNATURE_ERROR));
        if (method.getName().equals("deactivate") && OLDACTIVATEDESCRIPTOR.matcher(method.getDescriptor().toString()).matches()) {
        // This is the default!
        } else {
            setVersion(V1_1);
            set(COMPONENT_DEACTIVATE, method, "<>");
        }
    } else if (fqn.equals("aQute.bnd.annotation.component.Modified")) {
        if (!ACTIVATEDESCRIPTOR.matcher(method.getDescriptor().toString()).matches())
            reporter.error("Modified method for %s does not have an acceptable prototype, only Map, ComponentContext, or BundleContext is allowed. Found: %s", className, method.getDescriptor()).details(new DeclarativeServicesAnnotationError(className.getFQN(), method.getName(), method.getDescriptor().toString(), ErrorType.MODIFIED_SIGNATURE_ERROR));
        set(COMPONENT_MODIFIED, method, "<>");
        setVersion(V1_1);
    } else if (fqn.equals("aQute.bnd.annotation.component.Reference")) {
        String name = (String) annotation.get("aQute.bnd.annotation.component.Reference");
        String bind = method.getName();
        String unbind = null;
        if (name == null) {
            Matcher m = BINDMETHOD.matcher(method.getName());
            if (m.matches()) {
                name = m.group(2).toLowerCase() + m.group(3);
            } else {
                name = method.getName().toLowerCase();
            }
        }
        String simpleName = name;
        unbind = annotation.get(Reference.UNBIND);
        if (bind != null) {
            name = name + "/" + bind;
            if (unbind != null)
                name = name + "/" + unbind;
        }
        String service = null;
        TypeRef serviceTR = annotation.get(Reference.SERVICE);
        if (serviceTR != null)
            service = serviceTR.getFQN();
        if (service == null) {
            // We have to find the type of the current method to
            // link it to the referenced service.
            Matcher m = BINDDESCRIPTOR.matcher(method.getDescriptor().toString());
            if (m.matches()) {
                service = Descriptors.binaryToFQN(m.group(1));
            } else
                throw new IllegalArgumentException("Cannot detect the type of a Component Reference from the descriptor: " + method.getDescriptor());
        }
        // Check if we have a target, this must be a filter
        String target = annotation.get(Reference.TARGET);
        if (target != null) {
            String error = Verifier.validateFilter(target);
            if (error != null) {
                reporter.error("Invalid target filter %s for %s: %s", target, name, error).details(new DeclarativeServicesAnnotationError(className.getFQN(), bind, method.getDescriptor().toString(), ErrorType.INVALID_TARGET_FILTER));
            }
            service = service + target;
        }
        Integer c = annotation.get(Reference.TYPE);
        if (c != null && !c.equals(0) && !c.equals((int) '1')) {
            service = service + (char) c.intValue();
        }
        if (map.containsKey(name))
            reporter.error("In component %s, Multiple references with the same name: %s. Previous def: %s, this def: %s", name, map.get(name), service, "").details(new DeclarativeServicesAnnotationError(className.getFQN(), null, null, ErrorType.MULTIPLE_REFERENCES_SAME_NAME));
        map.put(name, service);
        if (isTrue(annotation.get(Reference.MULTIPLE)))
            multiple.add(simpleName);
        if (isTrue(annotation.get(Reference.OPTIONAL)))
            optional.add(simpleName);
        if (isTrue(annotation.get(Reference.DYNAMIC)))
            dynamic.add(simpleName);
        if (!checkMethod())
            setVersion(V1_1);
        else if (REFERENCEBINDDESCRIPTOR.matcher(method.getDescriptor().toString()).matches() || !OLDBINDDESCRIPTOR.matcher(method.getDescriptor().toString()).matches())
            setVersion(V1_1);
    } else if (fqn.startsWith("org.osgi.service.component.annotations")) {
        DeclarativeServicesAnnotationError errorDetails;
        switch(annotation.getElementType()) {
            case METHOD:
                errorDetails = new DeclarativeServicesAnnotationError(className.getFQN(), method.getName(), method.getDescriptor().toString(), ErrorType.MIXED_USE_OF_DS_ANNOTATIONS_BND);
                break;
            case FIELD:
                errorDetails = new DeclarativeServicesAnnotationError(className.getFQN(), field.getName(), ErrorType.MIXED_USE_OF_DS_ANNOTATIONS_BND);
                break;
            default:
                errorDetails = new DeclarativeServicesAnnotationError(className.getFQN(), null, ErrorType.MIXED_USE_OF_DS_ANNOTATIONS_BND);
        }
        List<DeclarativeServicesAnnotationError> errors = mismatchedAnnotations.get(fqn);
        if (errors == null) {
            errors = new ArrayList<DeclarativeServicesAnnotationError>();
            mismatchedAnnotations.put(fqn, errors);
        }
        errors.add(errorDetails);
    }
}
Also used : Matcher(java.util.regex.Matcher) DeclarativeServicesAnnotationError(aQute.bnd.component.error.DeclarativeServicesAnnotationError) TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 27 with TypeRef

use of aQute.bnd.osgi.Descriptors.TypeRef in project bnd by bndtools.

the class AnnotationReader method doComponent.

/**
	 * @param annotation
	 * @throws Exception
	 */
@SuppressWarnings("deprecation")
protected void doComponent(Component comp, Annotation annotation) throws Exception {
    if (!mismatchedAnnotations.isEmpty()) {
        String componentName = comp.name();
        componentName = (componentName == null) ? className.getFQN() : componentName;
        for (Entry<String, List<DeclarativeServicesAnnotationError>> e : mismatchedAnnotations.entrySet()) {
            for (DeclarativeServicesAnnotationError errorDetails : e.getValue()) {
                if (errorDetails.fieldName != null) {
                    analyzer.error("The DS component %s uses standard annotations to declare it as a component, but also uses the bnd DS annotation: %s on field %s. It is an error to mix these two types of annotations", componentName, e.getKey(), errorDetails.fieldName).details(errorDetails);
                } else if (errorDetails.methodName != null) {
                    analyzer.error("The DS component %s uses standard annotations to declare it as a component, but also uses the bnd DS annotation: %s on method %s with signature %s. It is an error to mix these two types of annotations", componentName, e.getKey(), errorDetails.methodName, errorDetails.methodSignature).details(errorDetails);
                } else {
                    analyzer.error("The DS component %s uses standard annotations to declare it as a component, but also uses the bnd DS annotation: %s. It is an error to mix these two types of annotations", componentName, e.getKey()).details(errorDetails);
                }
            }
        }
        return;
    }
    // Check if we are doing a super class
    if (component.implementation != null)
        return;
    component.implementation = clazz.getClassName();
    component.name = comp.name();
    component.factory = comp.factory();
    component.configurationPolicy = comp.configurationPolicy();
    if (annotation.get("enabled") != null)
        component.enabled = comp.enabled();
    if (annotation.get("factory") != null)
        component.factory = comp.factory();
    if (annotation.get("immediate") != null)
        component.immediate = comp.immediate();
    if (annotation.get("servicefactory") != null)
        component.scope = comp.servicefactory() ? ServiceScope.BUNDLE : ServiceScope.SINGLETON;
    if (annotation.get("scope") != null && comp.scope() != ServiceScope.DEFAULT) {
        component.scope = comp.scope();
        if (comp.scope() == ServiceScope.PROTOTYPE) {
            component.updateVersion(V1_3);
        }
    }
    if (annotation.get("configurationPid") != null) {
        component.configurationPid = comp.configurationPid();
        if (component.configurationPid.length > 1) {
            component.updateVersion(V1_3);
        } else {
            component.updateVersion(V1_2);
        }
    }
    if (annotation.get("xmlns") != null)
        component.xmlns = comp.xmlns();
    String[] properties = comp.properties();
    if (properties != null)
        for (String entry : properties) {
            if (entry.contains("=")) {
                analyzer.error("Found an = sign in an OSGi DS Component annotation on %s. In the bnd annotation " + "this is an actual property but in the OSGi, this element must refer to a path with Java properties. " + "However, found a path with an '=' sign which looks like a mixup (%s) with the 'property' element.", clazz, entry).details(new DeclarativeServicesAnnotationError(className.getFQN(), null, null, ErrorType.COMPONENT_PROPERTIES_ERROR));
            }
            component.properties.add(entry);
        }
    doProperty(comp.property());
    Object[] x = annotation.get("service");
    if (x == null) {
        // fqn.
        if (interfaces != null) {
            List<TypeRef> result = new ArrayList<TypeRef>();
            for (int i = 0; i < interfaces.length; i++) {
                if (!interfaces[i].equals(analyzer.getTypeRef("scala/ScalaObject")))
                    result.add(interfaces[i]);
            }
            component.service = result.toArray(EMPTY);
        }
    } else {
        // We have explicit interfaces set
        component.service = new TypeRef[x.length];
        for (int i = 0; i < x.length; i++) {
            TypeRef typeRef = (TypeRef) x[i];
            Clazz service = analyzer.findClass(typeRef);
            if (!analyzer.assignable(clazz, service)) {
                analyzer.error("Class %s is not assignable to specified service %s", clazz.getFQN(), typeRef.getFQN()).details(new DeclarativeServicesAnnotationError(className.getFQN(), null, null, ErrorType.INCOMPATIBLE_SERVICE));
            }
            component.service[i] = typeRef;
        }
    }
    // make sure reference processing knows this is a Reference in Component
    member = null;
    Object[] refAnnotations = annotation.get("reference");
    if (refAnnotations != null) {
        for (Object o : refAnnotations) {
            Annotation refAnnotation = (Annotation) o;
            Reference ref = refAnnotation.getAnnotation();
            doReference(ref, refAnnotation);
        }
    }
}
Also used : DeclarativeServicesAnnotationError(aQute.bnd.component.error.DeclarativeServicesAnnotationError) TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Reference(org.osgi.service.component.annotations.Reference) ArrayList(java.util.ArrayList) Annotation(aQute.bnd.osgi.Annotation) ArrayList(java.util.ArrayList) List(java.util.List) Clazz(aQute.bnd.osgi.Clazz)

Example 28 with TypeRef

use of aQute.bnd.osgi.Descriptors.TypeRef in project bnd by bndtools.

the class HeaderReader method provide.

/**
	 * @param cd
	 * @param provides
	 */
void provide(ComponentDef cd, String provides, String impl) {
    if (provides != null) {
        StringTokenizer st = new StringTokenizer(provides, ",");
        List<TypeRef> provide = new ArrayList<TypeRef>();
        while (st.hasMoreTokens()) {
            String interfaceName = st.nextToken();
            TypeRef ref = analyzer.getTypeRefFromFQN(interfaceName);
            provide.add(ref);
            analyzer.referTo(ref);
        // TODO verifies the impl. class extends or implements the
        // interface
        }
        cd.service = provide.toArray(new TypeRef[0]);
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) ArrayList(java.util.ArrayList)

Example 29 with TypeRef

use of aQute.bnd.osgi.Descriptors.TypeRef in project bnd by bndtools.

the class MetaTypeReader method handleInheritedClasses.

private void handleInheritedClasses(Clazz child) throws Exception {
    TypeRef[] ifaces = child.getInterfaces();
    if (ifaces != null) {
        for (TypeRef ref : ifaces) {
            parseAndMergeInheritedMetadata(ref, child);
        }
    }
    TypeRef superClazz = child.getSuper();
    if (superClazz != null) {
        parseAndMergeInheritedMetadata(superClazz, child);
    }
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef)

Example 30 with TypeRef

use of aQute.bnd.osgi.Descriptors.TypeRef in project bnd by bndtools.

the class MetaTypeReader method addMethod.

private void addMethod(MethodDef method, Meta.AD ad) throws Exception {
    if (method.isStatic())
        return;
    // Set all the defaults.
    String rtype = method.getGenericReturnType();
    String id = Configurable.mangleMethodName(method.getName());
    String name = Clazz.unCamel(id);
    int cardinality = 0;
    if (rtype.endsWith("[]")) {
        cardinality = Integer.MAX_VALUE;
        rtype = rtype.substring(0, rtype.length() - 2);
    }
    if (rtype.indexOf('<') > 0) {
        if (cardinality != 0)
            reporter.error("AD for %s.%s uses an array of collections in return type (%s), Metatype allows either Vector or array", clazz.getClassName().getFQN(), method.getName(), method.getType().getFQN());
        Matcher m = COLLECTION.matcher(rtype);
        if (m.matches()) {
            rtype = Clazz.objectDescriptorToFQN(m.group(3));
            cardinality = Integer.MIN_VALUE;
        }
    }
    Meta.Type type = getType(rtype);
    boolean required = ad == null || ad.required();
    String deflt = null;
    String max = null;
    String min = null;
    String[] optionLabels = null;
    String[] optionValues = null;
    String description = null;
    TypeRef typeRef = reporter.getTypeRefFromFQN(rtype);
    Clazz c = reporter.findClass(typeRef);
    if (c != null && c.isEnum()) {
        optionValues = parseOptionValues(c);
    }
    if (ad != null) {
        if (ad.id() != null)
            id = ad.id();
        if (ad.name() != null)
            name = ad.name();
        if (ad.cardinality() != 0)
            cardinality = ad.cardinality();
        if (ad.type() != null)
            type = ad.type();
        if (ad.description() != null)
            description = ad.description();
        if (ad.optionLabels() != null)
            optionLabels = ad.optionLabels();
        if (ad.optionValues() != null)
            optionValues = ad.optionValues();
        if (ad.min() != null)
            min = ad.min();
        if (ad.max() != null)
            max = ad.max();
        if (ad.deflt() != null)
            deflt = ad.deflt();
    }
    if (optionValues != null) {
        if (optionLabels == null || optionLabels.length == 0) {
            optionLabels = new String[optionValues.length];
            for (int i = 0; i < optionValues.length; i++) optionLabels[i] = Clazz.unCamel(optionValues[i]);
        }
        if (optionLabels.length != optionValues.length) {
            reporter.error("Option labels and option values not the same length for %s", id);
            optionLabels = optionValues;
        }
    }
    Tag adt = new Tag(this.ocd, "AD");
    adt.addAttribute("name", name);
    adt.addAttribute("id", id);
    adt.addAttribute("cardinality", cardinality);
    adt.addAttribute("required", required);
    adt.addAttribute("default", deflt);
    adt.addAttribute("type", type);
    adt.addAttribute("max", max);
    adt.addAttribute("min", min);
    adt.addAttribute("description", description);
    if (optionLabels != null && optionValues != null) {
        for (int i = 0; i < optionLabels.length; i++) {
            Tag option = new Tag(adt, "Option");
            option.addAttribute("label", optionLabels[i]);
            option.addAttribute("value", optionValues[i]);
        }
    }
}
Also used : Meta(aQute.bnd.annotation.metatype.Meta) Matcher(java.util.regex.Matcher) TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Clazz(aQute.bnd.osgi.Clazz) Tag(aQute.lib.tag.Tag)

Aggregations

TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)42 Clazz (aQute.bnd.osgi.Clazz)12 PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)7 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 Analyzer (aQute.bnd.osgi.Analyzer)5 ClassDataCollector (aQute.bnd.osgi.ClassDataCollector)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 Matcher (java.util.regex.Matcher)5 DeclarativeServicesAnnotationError (aQute.bnd.component.error.DeclarativeServicesAnnotationError)4 Attrs (aQute.bnd.header.Attrs)4 Annotation (aQute.bnd.osgi.Annotation)4 MultiMap (aQute.lib.collections.MultiMap)4 MethodDef (aQute.bnd.osgi.Clazz.MethodDef)3 Instructions (aQute.bnd.osgi.Instructions)3 Resource (aQute.bnd.osgi.Resource)3 Tag (aQute.lib.tag.Tag)3