felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clem...@apache.org
Subject svn commit: r1044656 - in /felix/sandbox/clement/ipojo-constructor-injection/manipulator/src: main/java/org/apache/felix/ipojo/manipulation/ test/java/org/apache/felix/ipojo/manipulation/ test/java/test/
Date Sat, 11 Dec 2010 13:52:20 GMT
Author: clement
Date: Sat Dec 11 13:52:19 2010
New Revision: 1044656

URL: http://svn.apache.org/viewvc?rev=1044656&view=rev
Log:
Manipulate all constructors to support constructor injection.

Added:
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/NoValidConstructor.java
Modified:
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Child.java

Modified: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java?rev=1044656&r1=1044655&r2=1044656&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
Sat Dec 11 13:52:19 2010
@@ -191,15 +191,14 @@ public class MethodCreator extends Class
             newDesc = "(Lorg/apache/felix/ipojo/InstanceManager;" + newDesc;
 
             Type[] args = Type.getArgumentTypes(desc);
+
+            // TODO HERE ! => All constructor matches, no distinction between the different
constructors.
+            generateConstructor(access, desc, signature, exceptions, md.getAnnotations());
+
             if (args.length == 0) {
-                generateEmptyConstructor(access, signature, exceptions, md.getAnnotations());
// No parameters, so no annotations parameters
                 m_foundSuitableConstructor = true;
             } else if (args.length == 1 && args[0].getClassName().equals("org.osgi.framework.BundleContext"))
{
-                generateBCConstructor(access, signature, exceptions, md.getAnnotations());
// One parameter, so no annotations parameters
                 m_foundSuitableConstructor = true;
-            } else {
-                // Do nothing, the constructor does not match.
-                return cv.visitMethod(access, name, desc, signature, exceptions);
             }
 
             // Insert the new constructor
@@ -296,6 +295,32 @@ public class MethodCreator extends Class
         return cv.visitField(access, name, desc, signature, value);
     }
 
+    private void generateConstructor(int access, String descriptor, String signature, String[]
exceptions, List annotations) {
+         GeneratorAdapter mv = new GeneratorAdapter(cv.visitMethod(access, "<init>",
descriptor, signature, exceptions), access, "<init>", descriptor);
+    	 // Compute the new signature
+    	 String newDesc = descriptor.substring(1); // Remove the first (
+         newDesc = "(Lorg/apache/felix/ipojo/InstanceManager;" + newDesc;
+
+
+         mv.visitCode();
+         mv.visitVarInsn(ALOAD, 0);
+         mv.visitInsn(ACONST_NULL);
+         mv.loadArgs();
+         mv.visitMethodInsn(INVOKESPECIAL, m_owner, "<init>", newDesc);
+         mv.visitInsn(RETURN);
+
+         // Move annotations
+         if (annotations != null) {
+             for (int i = 0; i < annotations.size(); i++) {
+                 AnnotationDescriptor ad = (AnnotationDescriptor) annotations.get(i);
+                 ad.visitAnnotation(mv);
+             }
+         }
+
+         mv.visitMaxs(0, 0);
+         mv.visitEnd();
+    }
+
     /**
      * Create a constructor to call the manipulated constructor.
      * This constructor does not have any argument. It will call the manipulated

Modified: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java?rev=1044656&r1=1044655&r2=1044656&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java
Sat Dec 11 13:52:19 2010
@@ -168,6 +168,108 @@ public class ManipulatorTest extends Tes
 
 	}
 
+	public void testManipulatingWithConstructorModification() throws Exception {
+		Manipulator manipulator = new Manipulator();
+		byte[] clazz = manipulator.manipulate(getBytesFromFile(new File("target/test-classes/test/Child.class")));
+		TestClassLoader classloader = new TestClassLoader("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());
+
+
+
+	}
+
+
+	public void testManipulatingWithNoValidConstructor() throws Exception {
+		Manipulator manipulator = new Manipulator();
+		byte[] clazz = manipulator.manipulate(getBytesFromFile(new File("target/test-classes/test/NoValidConstructor.class")));
+		TestClassLoader classloader = new TestClassLoader("test.NoValidConstructor", clazz);
+		Class cl = classloader.findClass("test.NoValidConstructor");
+		Assert.assertNotNull(cl);
+		Assert.assertNotNull(manipulator.getManipulationMetadata());
+
+		System.out.println(manipulator.getManipulationMetadata());
+
+		// The manipulation add stuff to the class.
+		Assert.assertTrue(clazz.length > getBytesFromFile(new File("target/test-classes/test/NoValidConstructor.class")).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);
+
+		// 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);
+
+	}
+
 	public static byte[] getBytesFromFile(File file) throws IOException {
 	    InputStream is = new FileInputStream(file);
 	    long length = file.length();

Modified: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Child.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Child.java?rev=1044656&r1=1044655&r2=1044656&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Child.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Child.java
Sat Dec 11 13:52:19 2010
@@ -3,7 +3,7 @@ package test;
 public class Child extends Parent {
 
 	Child(int i, String f) {
-		super(0, f);
+		super(i, f);
 	}
 
 	Child() {

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/NoValidConstructor.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/NoValidConstructor.java?rev=1044656&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/NoValidConstructor.java
(added)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/NoValidConstructor.java
Sat Dec 11 13:52:19 2010
@@ -0,0 +1,15 @@
+package test;
+
+public class NoValidConstructor {
+
+	String m_s;
+
+	public NoValidConstructor(String s) {
+		m_s = s;
+	}
+
+	public String getS() {
+		return m_s;
+	}
+
+}



Mime
View raw message