Search in sources :

Example 6 with MethodDef

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

the class AnnotationReader method doModified.

/**
	 * 
	 */
protected void doModified() {
    String methodDescriptor = member.getDescriptor().toString();
    DeclarativeServicesAnnotationError details = new DeclarativeServicesAnnotationError(className.getFQN(), member.getName(), methodDescriptor, ErrorType.MODIFIED_SIGNATURE_ERROR);
    if (!(member instanceof MethodDef)) {
        analyzer.error("Modified annotation on a field %s.%s", clazz, member.getDescriptor()).details(details);
        return;
    }
    boolean hasMapReturnType = false;
    Matcher m = LIFECYCLEDESCRIPTORDS11.matcher(methodDescriptor);
    if (m.matches()) {
        component.modified = member.getName();
        component.updateVersion(V1_1);
        hasMapReturnType = m.group(6) != null;
    } else {
        m = LIFECYCLEDESCRIPTORDS13.matcher(methodDescriptor);
        if (m.matches()) {
            component.modified = member.getName();
            component.updateVersion(V1_3);
            hasMapReturnType = m.group(4) != null;
            processAnnotationArguments(methodDescriptor, details);
        } else
            analyzer.error("Modified method for %s descriptor %s is not acceptable.", clazz, member.getDescriptor()).details(details);
    }
    checkMapReturnType(hasMapReturnType, details);
}
Also used : MethodDef(aQute.bnd.osgi.Clazz.MethodDef) Matcher(java.util.regex.Matcher) DeclarativeServicesAnnotationError(aQute.bnd.component.error.DeclarativeServicesAnnotationError)

Example 7 with MethodDef

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

the class CoverageResource method toTag.

public static Tag toTag(Map<MethodDef, List<MethodDef>> catalog) {
    Tag coverage = new Tag("coverage");
    String currentClass = null;
    Tag classTag = null;
    for (Map.Entry<MethodDef, List<MethodDef>> m : catalog.entrySet()) {
        String className = m.getKey().getContainingClass().getFQN();
        if (!className.equals(currentClass)) {
            classTag = new Tag("class");
            classTag.addAttribute("name", className);
            classTag.addAttribute("package", Descriptors.getPackage(className));
            classTag.addAttribute("short", Descriptors.getShortName(className));
            coverage.addContent(classTag);
            currentClass = className;
        }
        Tag method = doMethod(new Tag("method"), m.getKey());
        if (classTag != null)
            classTag.addContent(method);
        for (MethodDef r : m.getValue()) {
            Tag ref = doMethod(new Tag("ref"), r);
            method.addContent(ref);
        }
    }
    return coverage;
}
Also used : MethodDef(aQute.bnd.osgi.Clazz.MethodDef) List(java.util.List) Tag(aQute.lib.tag.Tag) Map(java.util.Map)

Example 8 with MethodDef

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

the class MetaTypeReader method parseAndMergeInheritedMetadata.

private void parseAndMergeInheritedMetadata(TypeRef ref, Clazz child) throws Exception {
    if (ref.isJava())
        return;
    Clazz ec = reporter.findClass(ref);
    if (ec == null) {
        reporter.error("Missing inherited class for Metatype annotations: %s from %s", ref, child.getClassName());
    } else {
        @SuppressWarnings("resource") MetaTypeReader mtr = new MetaTypeReader(ec, reporter);
        mtr.setDesignate(designatePid, factory);
        mtr.finish();
        for (Map.Entry<MethodDef, Meta.AD> entry : mtr.methods.entrySet()) addMethod(entry.getKey(), entry.getValue());
        handleInheritedClasses(ec);
    }
}
Also used : MethodDef(aQute.bnd.osgi.Clazz.MethodDef) Clazz(aQute.bnd.osgi.Clazz) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with MethodDef

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

the class HeaderReader method createComponentTag.

public Tag createComponentTag(String name, String impl, Map<String, String> info) throws Exception {
    final ComponentDef cd = new ComponentDef(null, AnnotationReader.V1_0);
    cd.name = name;
    if (info.get(COMPONENT_ENABLED) != null)
        cd.enabled = Boolean.valueOf(info.get(COMPONENT_ENABLED));
    cd.factory = info.get(COMPONENT_FACTORY);
    if (info.get(COMPONENT_IMMEDIATE) != null)
        cd.immediate = Boolean.valueOf(info.get(COMPONENT_IMMEDIATE));
    if (info.get(COMPONENT_CONFIGURATION_POLICY) != null)
        cd.configurationPolicy = ConfigurationPolicy.valueOf(info.get(COMPONENT_CONFIGURATION_POLICY).toUpperCase());
    cd.activate = checkIdentifier(COMPONENT_ACTIVATE, info.get(COMPONENT_ACTIVATE));
    cd.deactivate = checkIdentifier(COMPONENT_DEACTIVATE, info.get(COMPONENT_DEACTIVATE));
    cd.modified = checkIdentifier(COMPONENT_MODIFIED, info.get(COMPONENT_MODIFIED));
    cd.implementation = analyzer.getTypeRefFromFQN(impl == null ? name : impl);
    String provides = info.get(COMPONENT_PROVIDE);
    if (info.get(COMPONENT_SERVICEFACTORY) != null) {
        if (provides != null)
            cd.scope = Boolean.valueOf(info.get(COMPONENT_SERVICEFACTORY)) ? ServiceScope.BUNDLE : ServiceScope.SINGLETON;
        else
            warning("The servicefactory:=true directive is set but no service is provided, ignoring it");
    }
    if (cd.scope == ServiceScope.BUNDLE && cd.immediate != null && cd.immediate) {
        // TODO can become error() if it is up to me
        warning("For a Service Component, the immediate option and the servicefactory option are mutually exclusive for %s(%s)", name, impl);
    }
    // analyze the class for suitable methods.
    final Map<String, MethodDef> lifecycleMethods = new HashMap<String, MethodDef>();
    final Map<String, MethodDef> bindmethods = new HashMap<String, MethodDef>();
    TypeRef typeRef = analyzer.getTypeRefFromFQN(impl);
    Clazz clazz = analyzer.findClass(typeRef);
    boolean privateAllowed = true;
    boolean defaultAllowed = true;
    String topPackage = typeRef.getPackageRef().getFQN();
    while (clazz != null) {
        final boolean pa = privateAllowed;
        final boolean da = defaultAllowed;
        final Map<String, MethodDef> classLifecyclemethods = new HashMap<String, MethodDef>();
        final Map<String, MethodDef> classBindmethods = new HashMap<String, MethodDef>();
        clazz.parseClassFileWithCollector(new ClassDataCollector() {

            @Override
            public void method(MethodDef md) {
                Set<String> allowedParams = allowed;
                String lifecycleName = null;
                boolean isLifecycle = (cd.activate == null ? "activate" : cd.activate).equals(md.getName()) || md.getName().equals(cd.modified);
                if (!isLifecycle && (cd.deactivate == null ? "deactivate" : cd.deactivate).equals(md.getName())) {
                    isLifecycle = true;
                    allowedParams = allowedDeactivate;
                }
                if (isLifecycle && !lifecycleMethods.containsKey(md.getName()) && (md.isPublic() || md.isProtected() || (md.isPrivate() && pa) || (!md.isPrivate()) && da) && isBetter(md, classLifecyclemethods.get(md.getName()), allowedParams)) {
                    classLifecyclemethods.put(md.getName(), md);
                }
                if (!bindmethods.containsKey(md.getName()) && (md.isPublic() || md.isProtected() || (md.isPrivate() && pa) || (!md.isPrivate()) && da) && isBetterBind(md, classBindmethods.get(md.getName()))) {
                    classBindmethods.put(md.getName(), md);
                }
            }

            private boolean isBetter(MethodDef test, MethodDef existing, Set<String> allowedParams) {
                int testRating = rateLifecycle(test, allowedParams);
                if (existing == null)
                    // ignore invalid methods
                    return testRating < 6;
                if (testRating < rateLifecycle(existing, allowedParams))
                    return true;
                return false;
            }

            private boolean isBetterBind(MethodDef test, MethodDef existing) {
                int testRating = rateBind(test);
                if (existing == null)
                    // ignore invalid methods
                    return testRating < 6;
                if (testRating < rateBind(existing))
                    return true;
                return false;
            }
        });
        lifecycleMethods.putAll(classLifecyclemethods);
        bindmethods.putAll(classBindmethods);
        typeRef = clazz.getSuper();
        if (typeRef == null)
            break;
        clazz = analyzer.findClass(typeRef);
        privateAllowed = false;
        defaultAllowed = defaultAllowed && topPackage.equals(typeRef.getPackageRef().getFQN());
    }
    if (cd.activate != null && !lifecycleMethods.containsKey(cd.activate)) {
        error("in component %s, activate method %s specified but not found", cd.implementation.getFQN(), cd.activate);
        cd.activate = null;
    }
    if (cd.deactivate != null && !lifecycleMethods.containsKey(cd.deactivate)) {
        error("in component %s, deactivate method %s specified but not found", cd.implementation.getFQN(), cd.deactivate);
        cd.activate = null;
    }
    if (cd.modified != null && !lifecycleMethods.containsKey(cd.modified)) {
        error("in component %s, modified method %s specified but not found", cd.implementation.getFQN(), cd.modified);
        cd.activate = null;
    }
    provide(cd, provides, impl);
    properties(cd, info, name);
    reference(info, impl, cd, bindmethods);
    // compute namespace after references, an updated method means ds 1.2.
    getNamespace(info, cd, lifecycleMethods);
    cd.prepare(analyzer);
    return cd.getTag();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) MethodDef(aQute.bnd.osgi.Clazz.MethodDef) TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Clazz(aQute.bnd.osgi.Clazz) ClassDataCollector(aQute.bnd.osgi.ClassDataCollector)

Example 10 with MethodDef

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

the class AnnotationReader method doActivate.

/**
	 * 
	 */
protected void doActivate() {
    String methodDescriptor = member.getDescriptor().toString();
    DeclarativeServicesAnnotationError details = new DeclarativeServicesAnnotationError(className.getFQN(), member.getName(), methodDescriptor, ErrorType.ACTIVATE_SIGNATURE_ERROR);
    if (!(member instanceof MethodDef)) {
        analyzer.error("Activate annotation on a field %s.%s", clazz, member.getDescriptor()).details(details);
        return;
    }
    boolean hasMapReturnType = false;
    Matcher m = LIFECYCLEDESCRIPTORDS10.matcher(methodDescriptor);
    if ("activate".equals(member.getName()) && m.matches()) {
        component.activate = member.getName();
        hasMapReturnType = m.group(3) != null;
        if (!member.isProtected())
            component.updateVersion(V1_1);
    } else {
        m = LIFECYCLEDESCRIPTORDS11.matcher(methodDescriptor);
        if (m.matches()) {
            component.activate = member.getName();
            component.updateVersion(V1_1);
            hasMapReturnType = m.group(6) != null;
        } else {
            m = LIFECYCLEDESCRIPTORDS13.matcher(methodDescriptor);
            if (m.matches()) {
                component.activate = member.getName();
                component.updateVersion(V1_3);
                hasMapReturnType = m.group(4) != null;
                processAnnotationArguments(methodDescriptor, details);
            } else
                analyzer.error("Activate method for %s descriptor %s is not acceptable.", clazz, member.getDescriptor()).details(details);
        }
    }
    checkMapReturnType(hasMapReturnType, details);
}
Also used : MethodDef(aQute.bnd.osgi.Clazz.MethodDef) Matcher(java.util.regex.Matcher) DeclarativeServicesAnnotationError(aQute.bnd.component.error.DeclarativeServicesAnnotationError)

Aggregations

MethodDef (aQute.bnd.osgi.Clazz.MethodDef)10 DeclarativeServicesAnnotationError (aQute.bnd.component.error.DeclarativeServicesAnnotationError)4 Matcher (java.util.regex.Matcher)4 Clazz (aQute.bnd.osgi.Clazz)3 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)3 ClassDataCollector (aQute.bnd.osgi.ClassDataCollector)2 Tag (aQute.lib.tag.Tag)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Annotation (aQute.bnd.osgi.Annotation)1 FieldDef (aQute.bnd.osgi.Clazz.FieldDef)1 Instructions (aQute.bnd.osgi.Instructions)1 Delta (aQute.bnd.service.diff.Delta)1 Type (aQute.bnd.service.diff.Type)1 Version (aQute.bnd.version.Version)1 MultiMap (aQute.lib.collections.MultiMap)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1