Search in sources :

Example 11 with InstanceManager

use of org.apache.felix.ipojo.InstanceManager in project felix by apache.

the class InnerClassAdapterTest method testManipulatingTheInner.

@Test
public void testManipulatingTheInner() throws Exception {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    String className = "test.PojoWithInner";
    byte[] origin = ManipulatorTest.getBytesFromFile(new File(baseClassDirectory + className.replace(".", "/") + ".class"));
    ManipulatedClassLoader classloader = manipulate(className, manipulator);
    Class cl = classloader.findClass(className);
    Assert.assertNotNull(cl);
    Assert.assertNotNull(manipulator.getManipulationMetadata());
    Assert.assertFalse(manipulator.getInnerClasses().isEmpty());
    System.out.println(manipulator.getManipulationMetadata());
    // The manipulation add stuff to the class.
    Assert.assertTrue(classloader.get(className).length > origin.length);
    boolean found = false;
    Constructor cst = null;
    Constructor[] csts = cl.getDeclaredConstructors();
    for (Constructor cst2 : csts) {
        System.out.println(Arrays.asList(cst2.getParameterTypes()));
        if (cst2.getParameterTypes().length == 1 && cst2.getParameterTypes()[0].equals(InstanceManager.class)) {
            found = true;
            cst = cst2;
        }
    }
    Assert.assertTrue(found);
    // We still have the empty constructor
    found = false;
    csts = cl.getDeclaredConstructors();
    for (Constructor cst1 : csts) {
        System.out.println(Arrays.asList(cst1.getParameterTypes()));
        if (cst1.getParameterTypes().length == 0) {
            found = true;
        }
    }
    Assert.assertTrue(found);
    // Check the POJO interface
    Assert.assertTrue(Arrays.asList(cl.getInterfaces()).contains(Pojo.class));
    InstanceManager im = Mockito.mock(InstanceManager.class);
    cst.setAccessible(true);
    Object pojo = cst.newInstance(new Object[] { im });
    Assert.assertNotNull(pojo);
    Assert.assertTrue(pojo instanceof Pojo);
    Method method = cl.getMethod("doSomething", new Class[0]);
    Assert.assertTrue(((Boolean) method.invoke(pojo, new Object[0])).booleanValue());
}
Also used : Pojo(org.apache.felix.ipojo.Pojo) Constructor(java.lang.reflect.Constructor) InstanceManager(org.apache.felix.ipojo.InstanceManager) Method(java.lang.reflect.Method) File(java.io.File) Test(org.junit.Test)

Example 12 with InstanceManager

use of org.apache.felix.ipojo.InstanceManager in project felix by apache.

the class ManipulatorTest method testManipulatingDoubleArray.

/**
 * https://issues.apache.org/jira/browse/FELIX-3621
 */
public void testManipulatingDoubleArray() throws Exception {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    byte[] origin = getBytesFromFile(new File("target/test-classes/test/DoubleArray.class"));
    manipulator.prepare(origin);
    byte[] clazz = manipulator.manipulate(origin);
    ManipulatedClassLoader classloader = new ManipulatedClassLoader("test.DoubleArray", clazz);
    Class cl = classloader.findClass("test.DoubleArray");
    Assert.assertNotNull(cl);
    Assert.assertNotNull(manipulator.getManipulationMetadata());
    System.out.println(manipulator.getManipulationMetadata());
    Assert.assertTrue(manipulator.getManipulationMetadata().toString().contains("arguments=\"{int[][]}\""));
    // The manipulation add stuff to the class.
    Assert.assertTrue(clazz.length > origin.length);
    boolean found = false;
    Constructor cst = null;
    Constructor[] csts = cl.getDeclaredConstructors();
    for (int i = 0; i < csts.length; i++) {
        System.out.println(Arrays.asList(csts[i].getParameterTypes()));
        if (csts[i].getParameterTypes().length == 1 && csts[i].getParameterTypes()[0].equals(InstanceManager.class)) {
            found = true;
            cst = csts[i];
        }
    }
    Assert.assertTrue(found);
    // We still have the empty constructor
    found = false;
    csts = cl.getDeclaredConstructors();
    for (int i = 0; i < csts.length; i++) {
        System.out.println(Arrays.asList(csts[i].getParameterTypes()));
        if (csts[i].getParameterTypes().length == 0) {
            found = true;
        }
    }
    Assert.assertTrue(found);
    // Check the POJO interface
    Assert.assertTrue(Arrays.asList(cl.getInterfaces()).contains(Pojo.class));
    cst.setAccessible(true);
    Object pojo = cst.newInstance(new Object[] { new InstanceManager() });
    Assert.assertNotNull(pojo);
    Assert.assertTrue(pojo instanceof Pojo);
    Method method = cl.getMethod("start", new Class[0]);
    Assert.assertTrue(((Boolean) method.invoke(pojo, new Object[0])).booleanValue());
}
Also used : Pojo(org.apache.felix.ipojo.Pojo) Constructor(java.lang.reflect.Constructor) InstanceManager(org.apache.felix.ipojo.InstanceManager) Method(java.lang.reflect.Method)

Example 13 with InstanceManager

use of org.apache.felix.ipojo.InstanceManager in project felix by apache.

the class ManipulatorTest method testManipulatingWithConstructorModification.

public void testManipulatingWithConstructorModification() throws Exception {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    byte[] origin = getBytesFromFile(new File("target/test-classes/test/Child.class"));
    manipulator.prepare(origin);
    byte[] clazz = manipulator.manipulate(origin);
    ManipulatedClassLoader classloader = new ManipulatedClassLoader("test.Child", clazz);
    Class cl = classloader.findClass("test.Child");
    Assert.assertNotNull(cl);
    Assert.assertNotNull(manipulator.getManipulationMetadata());
    boolean found = false;
    Constructor cst = null;
    Constructor[] csts = cl.getDeclaredConstructors();
    for (int i = 0; i < csts.length; i++) {
        System.out.println(Arrays.asList(csts[i].getParameterTypes()));
        if (csts[i].getParameterTypes().length == 1 && csts[i].getParameterTypes()[0].equals(InstanceManager.class)) {
            found = true;
            cst = csts[i];
        }
    }
    Assert.assertTrue(found);
    // We still have the regular constructor
    found = false;
    csts = cl.getDeclaredConstructors();
    for (int i = 0; i < csts.length; i++) {
        System.out.println(Arrays.asList(csts[i].getParameterTypes()));
        if (csts[i].getParameterTypes().length == 2) {
            found = true;
        }
    }
    Assert.assertTrue(found);
    // Check that we have the IM, Integer, String constructor too
    Constructor cst2 = cl.getDeclaredConstructor(new Class[] { InstanceManager.class, Integer.TYPE, String.class });
    Assert.assertNotNull(cst2);
    // Check the POJO interface
    Assert.assertTrue(Arrays.asList(cl.getInterfaces()).contains(Pojo.class));
    // Creation using cst
    InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
    cst.setAccessible(true);
    Object pojo = cst.newInstance(new Object[] { im });
    Assert.assertNotNull(pojo);
    Assert.assertTrue(pojo instanceof Pojo);
    Method method = cl.getMethod("doSomething", new Class[0]);
    Assert.assertEquals(9, ((Integer) method.invoke(pojo, new Object[0])).intValue());
    // Try to create using cst2
    im = (InstanceManager) Mockito.mock(InstanceManager.class);
    cst2.setAccessible(true);
    pojo = cst2.newInstance(new Object[] { im, new Integer(2), "bariton" });
    Assert.assertNotNull(pojo);
    Assert.assertTrue(pojo instanceof Pojo);
    method = cl.getMethod("doSomething", new Class[0]);
    Assert.assertEquals(10, ((Integer) method.invoke(pojo, new Object[0])).intValue());
}
Also used : Pojo(org.apache.felix.ipojo.Pojo) Constructor(java.lang.reflect.Constructor) InstanceManager(org.apache.felix.ipojo.InstanceManager) Method(java.lang.reflect.Method)

Aggregations

InstanceManager (org.apache.felix.ipojo.InstanceManager)13 Constructor (java.lang.reflect.Constructor)9 Pojo (org.apache.felix.ipojo.Pojo)9 Method (java.lang.reflect.Method)8 MockBundle (org.apache.felix.ipojo.test.MockBundle)4 Bundle (org.osgi.framework.Bundle)4 BundleContext (org.osgi.framework.BundleContext)4 Test (org.junit.Test)3 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)2 Logger (org.apache.felix.ipojo.util.Logger)2 File (java.io.File)1 Proxy (java.lang.reflect.Proxy)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Nullable (org.apache.felix.ipojo.Nullable)1