Search in sources :

Example 1 with Reporter

use of org.apache.felix.ipojo.manipulator.Reporter 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 Reporter

use of org.apache.felix.ipojo.manipulator.Reporter in project felix by apache.

the class RemanipulationTest method testDoubleManipulationWithAnnotations.

/**
 * Tests checking that the consecutive manipulation does still returns valid metadata (from annotations),
 * and valid manipulation metadata.
 *
 * @throws IOException
 * @throws ClassNotFoundException
 */
public void testDoubleManipulationWithAnnotations() throws IOException, ClassNotFoundException {
    Reporter reporter = mock(Reporter.class);
    // Step 1 - First collection and manipulation
    // 1.1 Metadata collection
    byte[] origin = ManipulatorTest.getBytesFromFile(new File("target/test-classes/test/PlentyOfAnnotations.class"));
    MiniStore store = new MiniStore().addClassToStore("test.PlentyOfAnnotations", origin);
    AnnotationMetadataProvider provider = new AnnotationMetadataProvider(store, reporter);
    List<Element> originalMetadata = provider.getMetadatas();
    // 1.2 Manipulation
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    manipulator.prepare(origin);
    byte[] clazz = manipulator.manipulate(origin);
    Element originalManipulationMetadata = manipulator.getManipulationMetadata();
    // 1.3 Check that the class is valid
    ManipulatedClassLoader classloader = new ManipulatedClassLoader("test.PlentyOfAnnotations", clazz);
    Class cl = classloader.findClass("test.PlentyOfAnnotations");
    Assert.assertNotNull(cl);
    // ---------------
    // Step 2 - Second collection and manipulation
    // We use the output class as entry.
    // 2.1 Metadata collection
    store = new MiniStore().addClassToStore("test.PlentyOfAnnotations", clazz);
    provider = new AnnotationMetadataProvider(store, reporter);
    List<Element> metadataAfterOneManipulation = provider.getMetadatas();
    // 2.2 Manipulation
    manipulator = new Manipulator(this.getClass().getClassLoader());
    manipulator.prepare(clazz);
    byte[] clazz2 = manipulator.manipulate(clazz);
    Element manipulationMetadataAfterSecondManipulation = manipulator.getManipulationMetadata();
    // 2.3 Check that the class is valid
    classloader = new ManipulatedClassLoader("test.PlentyOfAnnotations", clazz);
    cl = classloader.findClass("test.PlentyOfAnnotations");
    Assert.assertNotNull(cl);
    // ---------------
    // Step 3 - Third collection and manipulation
    // We use the output class of 2 as entry.
    // 3.1 Metadata collection
    store = new MiniStore().addClassToStore("test.PlentyOfAnnotations", clazz2);
    provider = new AnnotationMetadataProvider(store, reporter);
    List<Element> metadataAfterTwoManipulation = provider.getMetadatas();
    // 3.2 Manipulation
    manipulator = new Manipulator(this.getClass().getClassLoader());
    manipulator.prepare(clazz2);
    byte[] clazz3 = manipulator.manipulate(clazz2);
    Element manipulationMetadataAfterThirdManipulation = manipulator.getManipulationMetadata();
    // 3.3 Check that the class is valid
    classloader = new ManipulatedClassLoader("test.PlentyOfAnnotations", clazz);
    cl = classloader.findClass("test.PlentyOfAnnotations");
    Assert.assertNotNull(cl);
    // ---------------
    // Verification
    // Unchanged metadata
    Assert.assertEquals(originalMetadata.toString(), metadataAfterOneManipulation.toString());
    Assert.assertEquals(originalMetadata.toString(), metadataAfterTwoManipulation.toString());
    // Unchanged manipulation metadata
    Assert.assertEquals(originalManipulationMetadata.toString(), manipulationMetadataAfterSecondManipulation.toString());
    Assert.assertEquals(originalManipulationMetadata.toString(), manipulationMetadataAfterThirdManipulation.toString());
}
Also used : Reporter(org.apache.felix.ipojo.manipulator.Reporter) Element(org.apache.felix.ipojo.metadata.Element) AnnotationMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.AnnotationMetadataProvider) File(java.io.File)

Example 3 with Reporter

use of org.apache.felix.ipojo.manipulator.Reporter in project felix by apache.

the class AnnotationMetadataProviderTestCase method testGetMetadatas.

public void testGetMetadatas() throws Exception {
    MiniStore store = new MiniStore(AnnotatedComponent.class, FakeAnnotation.class);
    Reporter reporter = mock(Reporter.class);
    AnnotationMetadataProvider provider = new AnnotationMetadataProvider(store, reporter);
    List<Element> meta = provider.getMetadatas();
    assertEquals(1, meta.size());
}
Also used : Reporter(org.apache.felix.ipojo.manipulator.Reporter) Element(org.apache.felix.ipojo.metadata.Element)

Example 4 with Reporter

use of org.apache.felix.ipojo.manipulator.Reporter in project felix by apache.

the class FileMetadataProviderTestCase method testGetMetadatasFromFile.

public void testGetMetadatasFromFile() throws Exception {
    File metadata = new File(new File("target", "test-classes"), "metadata.xml");
    Reporter reporter = mock(Reporter.class);
    FileMetadataProvider provider = new FileMetadataProvider(metadata, reporter);
    provider.setValidateUsingLocalSchemas(true);
    List<Element> meta = provider.getMetadatas();
    assertEquals(3, meta.size());
}
Also used : Reporter(org.apache.felix.ipojo.manipulator.Reporter) Element(org.apache.felix.ipojo.metadata.Element) File(java.io.File)

Example 5 with Reporter

use of org.apache.felix.ipojo.manipulator.Reporter in project felix by apache.

the class FileMetadataProviderTestCase method testGetMetadatasFromDirectory.

public void testGetMetadatasFromDirectory() throws Exception {
    File metadata = new File("target", "test-classes");
    Reporter reporter = mock(Reporter.class);
    FileMetadataProvider provider = new FileMetadataProvider(metadata, reporter);
    provider.setValidateUsingLocalSchemas(true);
    List<Element> meta = provider.getMetadatas();
    assertEquals(3, meta.size());
}
Also used : Reporter(org.apache.felix.ipojo.manipulator.Reporter) Element(org.apache.felix.ipojo.metadata.Element) File(java.io.File)

Aggregations

Reporter (org.apache.felix.ipojo.manipulator.Reporter)17 Element (org.apache.felix.ipojo.metadata.Element)12 ComponentWorkbench (org.apache.felix.ipojo.manipulator.metadata.annotation.ComponentWorkbench)9 File (java.io.File)7 MethodNode (org.objectweb.asm.tree.MethodNode)4 FileInputStream (java.io.FileInputStream)2 Pojoization (org.apache.felix.ipojo.manipulator.Pojoization)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 LinkedHashSet (java.util.LinkedHashSet)1 JarFile (java.util.jar.JarFile)1 ResourceStore (org.apache.felix.ipojo.manipulator.ResourceStore)1 AnnotationMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.AnnotationMetadataProvider)1 CompositeMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider)1 FileMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.FileMetadataProvider)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