Search in sources :

Example 1 with AnnotationVisitorFactory

use of org.apache.felix.ipojo.manipulator.spi.AnnotationVisitorFactory 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 2 with AnnotationVisitorFactory

use of org.apache.felix.ipojo.manipulator.spi.AnnotationVisitorFactory in project felix by apache.

the class LegacyGenericBindingRegistry method createBindings.

@Override
protected List<Binding> createBindings(final Type type) {
    if (CUSTOM_HANDLER_PATTERN.matcher(type.getClassName()).matches()) {
        Binding binding = new Binding();
        binding.setAnnotationType(type);
        binding.setPredicate(alwaysTrue());
        binding.setFactory(new AnnotationVisitorFactory() {

            // Need to build a new Element instance for each created visitor
            public AnnotationVisitor newAnnotationVisitor(BindingContext context) {
                if (context.getClassNode() != null) {
                    return new TypeGenericVisitor(context.getWorkbench(), Elements.buildElement(type));
                } else if (context.getFieldNode() != null) {
                    return new FieldGenericVisitor(context.getWorkbench(), Elements.buildElement(type), context.getFieldNode());
                } else if ((context.getMethodNode() != null) && (context.getParameterIndex() == BindingContext.NO_INDEX)) {
                    return new MethodGenericVisitor(context.getWorkbench(), Elements.buildElement(type), context.getMethodNode());
                } else {
                    // last case: method parameter annotation
                    return new ParameterGenericVisitor(context.getWorkbench(), Elements.buildElement(type), context.getMethodNode(), context.getParameterIndex());
                }
            }

            @Override
            public String toString() {
                return "LegacyGenericVisitorFactory";
            }
        });
        // Return the produced generic binding
        return singletonList(binding);
    }
    return emptyList();
}
Also used : TypeGenericVisitor(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.TypeGenericVisitor) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) FieldGenericVisitor(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.FieldGenericVisitor) MethodGenericVisitor(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.MethodGenericVisitor) ParameterGenericVisitor(org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.ParameterGenericVisitor) BindingContext(org.apache.felix.ipojo.manipulator.spi.BindingContext) AnnotationVisitorFactory(org.apache.felix.ipojo.manipulator.spi.AnnotationVisitorFactory)

Aggregations

AnnotationVisitorFactory (org.apache.felix.ipojo.manipulator.spi.AnnotationVisitorFactory)2 BindingContext (org.apache.felix.ipojo.manipulator.spi.BindingContext)2 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)2 Reporter (org.apache.felix.ipojo.manipulator.Reporter)1 ComponentWorkbench (org.apache.felix.ipojo.manipulator.metadata.annotation.ComponentWorkbench)1 MethodBindVisitor (org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.bind.MethodBindVisitor)1 ParameterBindVisitor (org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.bind.ParameterBindVisitor)1 FieldGenericVisitor (org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.FieldGenericVisitor)1 GenericVisitorFactory (org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.GenericVisitorFactory)1 MethodGenericVisitor (org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.MethodGenericVisitor)1 ParameterGenericVisitor (org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.ParameterGenericVisitor)1 TypeGenericVisitor (org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.generic.TypeGenericVisitor)1 Element (org.apache.felix.ipojo.metadata.Element)1 MethodNode (org.objectweb.asm.tree.MethodNode)1