Search in sources :

Example 36 with Element

use of org.apache.felix.ipojo.metadata.Element in project felix by apache.

the class ManifestMetadataParser method parse.

/**
 * Looks for the <code>iPOJO-Components</code> header
 * in the given dictionary. Then, initializes the
 * {@link ManifestMetadataParser#m_elements} list (adds the
 * <code>iPOJO</code> root element) and parses the contained
 * component type declarations and instance configurations.
 * @param dict the given headers of the manifest file
 * @throws ParseException if any error occurs
 */
public void parse(Dictionary dict) throws ParseException {
    String componentClassesStr = (String) dict.get("iPOJO-Components");
    // Add the ipojo element inside the element list
    addElement(new Element("iPOJO", ""));
    parseElements(componentClassesStr.trim());
}
Also used : Element(org.apache.felix.ipojo.metadata.Element)

Example 37 with Element

use of org.apache.felix.ipojo.metadata.Element in project felix by apache.

the class ManifestMetadataParser method removeLastElement.

/**
 * Removes an element from the {@link ManifestMetadataParser#m_elements} list.
 * @return an element to remove
 */
private Element removeLastElement() {
    int idx = -1;
    idx = m_elements.length - 1;
    Element last = m_elements[idx];
    if (idx >= 0) {
        if ((m_elements.length - 1) == 0) {
            // It is the last element of the list;
            m_elements = new Element[0];
        } else {
            // Remove the last element of the list :
            Element[] newElementsList = new Element[m_elements.length - 1];
            System.arraycopy(m_elements, 0, newElementsList, 0, idx);
            m_elements = newElementsList;
        }
    }
    return last;
}
Also used : Element(org.apache.felix.ipojo.metadata.Element)

Example 38 with Element

use of org.apache.felix.ipojo.metadata.Element in project felix by apache.

the class DefaultBindingModule method configure.

/**
 * Configure all the iPOJO's default annotation's bindings.
 */
public void configure() {
    // Class level annotations
    // --------------------------------
    bind(Component.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            return new ComponentVisitor(context.getWorkbench(), context.getReporter());
        }
    });
    bind(Handler.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            return new HandlerVisitor(context.getWorkbench(), context.getReporter());
        }
    });
    bind(Provides.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            return new ProvidesVisitor(context.getWorkbench());
        }
    });
    bind(HandlerDeclaration.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            Reporter reporter = context.getReporter();
            return new HandlerDeclarationVisitor(context.getWorkbench(), getFreshDocumentBuilder(reporter), reporter);
        }
    });
    bind(Instantiate.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            return new InstantiateVisitor(context.getWorkbench());
        }
    });
    // Field level annotations
    // --------------------------------
    bind(Requires.class).when(on(ElementType.FIELD)).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            return new RequiresVisitor(context.getWorkbench(), context.getFieldNode().name);
        }
    }).when(on(ElementType.PARAMETER)).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            return new ParameterBindVisitor(context.getWorkbench(), Action.BIND, context.getParameterIndex());
        }
    });
    bind(Controller.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            return new ControllerVisitor(context.getWorkbench(), context.getFieldNode().name);
        }
    });
    bind(ServiceProperty.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            String name = context.getFieldNode().name;
            ComponentWorkbench workbench = context.getWorkbench();
            if (!workbench.getIds().containsKey("provides")) {
                // The provides annotation is already computed.
                context.getReporter().warn("The component does not provide services, skip ServiceProperty for {}", name);
                return null;
            } else {
                // Get the provides element
                Element provides = workbench.getIds().get("provides");
                return new FieldPropertyVisitor(name, provides);
            }
        }
    });
    bind(ServiceController.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            String name = context.getFieldNode().name;
            ComponentWorkbench workbench = context.getWorkbench();
            if (!workbench.getIds().containsKey("provides")) {
                // The provides annotation is already computed.
                context.getReporter().warn("The component does not provide services, skip @ServiceController for {}", name);
                return null;
            } else {
                // Get the provides element
                Element provides = workbench.getIds().get("provides");
                return new ServiceControllerVisitor(name, provides);
            }
        }
    });
    bind(Property.class).when(on(ElementType.FIELD)).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            ComponentWorkbench workbench = context.getWorkbench();
            Element properties = Elements.getPropertiesElement(workbench);
            String name = context.getFieldNode().name;
            return new FieldPropertyVisitor(name, properties);
        }
    }).when(on(ElementType.METHOD)).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            ComponentWorkbench workbench = context.getWorkbench();
            // @Property on method parameter
            Element properties = Elements.getPropertiesElement(workbench);
            String name = context.getMethodNode().name;
            return new MethodPropertyVisitor(properties, name);
        }
    }).when(on(ElementType.PARAMETER)).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            ComponentWorkbench workbench = context.getWorkbench();
            // @Property on method parameter
            Element properties = Elements.getPropertiesElement(workbench);
            MethodNode method = context.getMethodNode();
            return new ParameterPropertyVisitor(properties, method, context.getParameterIndex());
        }
    });
    bind(Validate.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            MethodNode node = context.getMethodNode();
            return new LifecycleVisitor(context.getWorkbench(), Names.computeEffectiveMethodName(node.name), LifecycleVisitor.Transition.VALIDATE);
        }
    });
    bind(Invalidate.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            MethodNode node = context.getMethodNode();
            return new LifecycleVisitor(context.getWorkbench(), Names.computeEffectiveMethodName(node.name), LifecycleVisitor.Transition.INVALIDATE);
        }
    });
    bind(Updated.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            MethodNode node = context.getMethodNode();
            return new UpdatedVisitor(context.getWorkbench(), Names.computeEffectiveMethodName(node.name));
        }
    });
    bind(Bind.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            MethodNode node = context.getMethodNode();
            return new MethodBindVisitor(context.getWorkbench(), Action.BIND, node, context.getReporter());
        }
    });
    bind(Unbind.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            MethodNode node = context.getMethodNode();
            return new MethodBindVisitor(context.getWorkbench(), Action.UNBIND, node, context.getReporter());
        }
    });
    bind(Modified.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            MethodNode node = context.getMethodNode();
            return new MethodBindVisitor(context.getWorkbench(), Action.MODIFIED, node, context.getReporter());
        }
    });
    bind(PostRegistration.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            MethodNode node = context.getMethodNode();
            return new PostRegistrationVisitor(context.getWorkbench(), node.name);
        }
    });
    bind(PostUnregistration.class).to(new AnnotationVisitorFactory() {

        public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
            MethodNode node = context.getMethodNode();
            return new PostUnregistrationVisitor(context.getWorkbench(), node.name);
        }
    });
    bind(Context.class).to(new GenericVisitorFactory("context", ""));
}
Also used : GenericVisitorFactory(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.GenericVisitorFactory) ParameterBindVisitor(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.bind.ParameterBindVisitor) Element(org.apache.felix.ipojo.metadata.Element) AnnotationVisitorFactory(org.apache.felix.ipojo.manipulator.spi.AnnotationVisitorFactory) MethodNode(org.objectweb.asm.tree.MethodNode) BindingContext(org.apache.felix.ipojo.manipulator.spi.BindingContext) Reporter(org.apache.felix.ipojo.manipulator.Reporter) BindingContext(org.apache.felix.ipojo.manipulator.spi.BindingContext) MethodBindVisitor(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.bind.MethodBindVisitor) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ComponentWorkbench(org.apache.felix.ipojo.manipulator.metadata.annotation.ComponentWorkbench)

Example 39 with Element

use of org.apache.felix.ipojo.metadata.Element in project felix by apache.

the class MetaAnnotationBindingRegistry method createBindings.

@Override
protected List<Binding> createBindings(final Type type) {
    // Parse the annotation
    byte[] bytes;
    try {
        bytes = m_store.read(type.getInternalName().concat(".class"));
    } catch (IOException e) {
        // Annotation type cannot be read
        m_reporter.trace("Could not read bytecode for @%s.", type.getClassName());
        return emptyList();
    } catch (IllegalStateException e) {
        m_reporter.trace("Could not read bytecode for @%s because the bundle is not in a state allowing read " + "operations.", type.getClassName());
        return emptyList();
    }
    AnnotationParser parser = new AnnotationParser();
    AnnotationType annotationType = parser.read(bytes);
    // Search meta-annotations
    ChainedAnnotationDiscovery chain = new ChainedAnnotationDiscovery();
    StereotypeDiscovery stereotypeDiscovery = new StereotypeDiscovery();
    HandlerBindingDiscovery handlerBindingDiscovery = new HandlerBindingDiscovery();
    IgnoredDiscovery ignoredDiscovery = new IgnoredDiscovery();
    chain.getDiscoveries().add(stereotypeDiscovery);
    chain.getDiscoveries().add(handlerBindingDiscovery);
    chain.getDiscoveries().add(ignoredDiscovery);
    annotationType.traverse(chain);
    // Produced Bindings
    List<Binding> bindings = new ArrayList<Binding>();
    // @Stereotype support
    if (stereotypeDiscovery.isStereotype()) {
        m_reporter.trace("@Stereotype detected: @%s", type.getClassName());
        Binding binding = new Binding();
        binding.setAnnotationType(type);
        binding.setPredicate(alwaysTrue());
        binding.setFactory(new StereotypeVisitorFactory(annotationType));
        bindings.add(binding);
    }
    // @HandlerBinding support
    if (handlerBindingDiscovery.isHandlerBinding()) {
        m_reporter.trace("@HandlerBinding detected: @%s", type.getClassName());
        Binding binding = new Binding();
        binding.setAnnotationType(type);
        binding.setPredicate(alwaysTrue());
        final Element element = buildElement(handlerBindingDiscovery, type);
        binding.setFactory(new GenericVisitorFactory(element.getName(), element.getNameSpace()));
        bindings.add(binding);
    }
    // Its IMPORTANT that the @Ignore is processed last since it removes existing bindings
    if (ignoredDiscovery.isIgnore()) {
        m_reporter.trace("@Ignore detected: @%s", type.getClassName());
        Binding binding = new NullBinding(type);
        bindings.clear();
        bindings.add(binding);
        // just in case of ...
        bindings = unmodifiableList(bindings);
    }
    return bindings;
}
Also used : NullBinding(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.ignore.NullBinding) HandlerBinding(org.apache.felix.ipojo.annotations.HandlerBinding) GenericVisitorFactory(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.GenericVisitorFactory) AnnotationParser(org.apache.felix.ipojo.manipulator.metadata.annotation.model.parser.AnnotationParser) IgnoredDiscovery(org.apache.felix.ipojo.manipulator.metadata.annotation.model.discovery.IgnoredDiscovery) ChainedAnnotationDiscovery(org.apache.felix.ipojo.manipulator.metadata.annotation.model.discovery.ChainedAnnotationDiscovery) Element(org.apache.felix.ipojo.metadata.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AnnotationType(org.apache.felix.ipojo.manipulator.metadata.annotation.model.AnnotationType) HandlerBindingDiscovery(org.apache.felix.ipojo.manipulator.metadata.annotation.model.discovery.HandlerBindingDiscovery) StereotypeVisitorFactory(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.stereotype.StereotypeVisitorFactory) NullBinding(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.ignore.NullBinding) StereotypeDiscovery(org.apache.felix.ipojo.manipulator.metadata.annotation.model.discovery.StereotypeDiscovery)

Example 40 with Element

use of org.apache.felix.ipojo.metadata.Element in project felix by apache.

the class ControllerVisitor method visitEnd.

/**
 * Visit @Handler annotation attributes.
 * @see org.objectweb.asm.AnnotationVisitor#visit(String, Object)
 */
public void visitEnd() {
    Element controller = new Element("controller", "");
    controller.addAttribute(new Attribute("field", field));
    workbench.getElements().put(controller, null);
}
Also used : Attribute(org.apache.felix.ipojo.metadata.Attribute) Element(org.apache.felix.ipojo.metadata.Element)

Aggregations

Element (org.apache.felix.ipojo.metadata.Element)400 Test (org.junit.Test)126 Attribute (org.apache.felix.ipojo.metadata.Attribute)109 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)22 ParseException (org.apache.felix.ipojo.parser.ParseException)14 PojoMetadata (org.apache.felix.ipojo.parser.PojoMetadata)14 ArrayList (java.util.ArrayList)13 Reporter (org.apache.felix.ipojo.manipulator.Reporter)12 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)12 Dictionary (java.util.Dictionary)10 Properties (java.util.Properties)10 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)10 BaseTest (org.ow2.chameleon.testing.helpers.BaseTest)10 PropertyDescription (org.apache.felix.ipojo.architecture.PropertyDescription)9 IOException (java.io.IOException)8 List (java.util.List)8 Before (org.junit.Before)7 Enumeration (java.util.Enumeration)6 ComponentWorkbench (org.apache.felix.ipojo.manipulator.metadata.annotation.ComponentWorkbench)6 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)6