Search in sources :

Example 1 with AnnotationMetadataProvider

use of org.apache.felix.ipojo.manipulator.metadata.AnnotationMetadataProvider in project felix by apache.

the class PojoizationPlugin method buildMetadataProvider.

protected CompositeMetadataProvider buildMetadataProvider(Analyzer analyzer, BndReporter reporter, BndJarResourceStore store) {
    // Build MetadataProvider
    CompositeMetadataProvider provider = new CompositeMetadataProvider(reporter);
    File file = new File(m_metadata);
    if (file.exists()) {
        // Absolute file system resource
        FileMetadataProvider fmp = new FileMetadataProvider(file, reporter);
        fmp.setValidateUsingLocalSchemas(m_useLocalSchemas);
        provider.addMetadataProvider(fmp);
    } else {
        // In archive resource
        Resource resource = analyzer.getJar().getResource(m_metadata);
        if (resource != null) {
            ResourceMetadataProvider rmp = new ResourceMetadataProvider(resource, reporter);
            rmp.setValidateUsingLocalSchemas(m_useLocalSchemas);
            provider.addMetadataProvider(rmp);
        }
    }
    provider.addMetadataProvider(new AnnotationMetadataProvider(store, reporter));
    return provider;
}
Also used : FileMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.FileMetadataProvider) Resource(aQute.bnd.osgi.Resource) CompositeMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider) AnnotationMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.AnnotationMetadataProvider) File(java.io.File)

Example 2 with AnnotationMetadataProvider

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

Aggregations

File (java.io.File)2 AnnotationMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.AnnotationMetadataProvider)2 Resource (aQute.bnd.osgi.Resource)1 Reporter (org.apache.felix.ipojo.manipulator.Reporter)1 CompositeMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider)1 FileMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.FileMetadataProvider)1 Element (org.apache.felix.ipojo.metadata.Element)1