You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2009/04/05 16:02:15 UTC

svn commit: r762084 - in /felix/trunk/ipojo: handler/extender/src/main/java/org/apache/felix/ipojo/handler/extender/ manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/sce...

Author: clement
Date: Sun Apr  5 14:02:15 2009
New Revision: 762084

URL: http://svn.apache.org/viewvc?rev=762084&view=rev
Log:
Fix issue Felix-1024
iPOJO creates a suitable constructor if none found.

Fix issue Felix-1025
Extender callbacks are no more called with the lock, and so avoid deadlocks.

Added:
    felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ASimpleParentClass.java   (with props)
    felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructor.java   (with props)
    felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructorWithParentClass.java   (with props)
Modified:
    felix/trunk/ipojo/handler/extender/src/main/java/org/apache/felix/ipojo/handler/extender/BundleTracker.java
    felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java
    felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
    felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/manipulation/SeveralConstructorTest.java
    felix/trunk/ipojo/tests/manipulator/creation/src/main/resources/metadata.xml

Modified: felix/trunk/ipojo/handler/extender/src/main/java/org/apache/felix/ipojo/handler/extender/BundleTracker.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/handler/extender/src/main/java/org/apache/felix/ipojo/handler/extender/BundleTracker.java?rev=762084&r1=762083&r2=762084&view=diff
==============================================================================
--- felix/trunk/ipojo/handler/extender/src/main/java/org/apache/felix/ipojo/handler/extender/BundleTracker.java (original)
+++ felix/trunk/ipojo/handler/extender/src/main/java/org/apache/felix/ipojo/handler/extender/BundleTracker.java Sun Apr  5 14:02:15 2009
@@ -100,20 +100,23 @@
     /**
      * Call this method to start the tracking of active bundles.
      **/
-    public synchronized void open() {
-        if (!m_open) {
-            m_open = true;
+    public void open() {
+        synchronized (this) {
+            if (!m_open) {
+                m_open = true;
 
-            m_context.addBundleListener(m_listener);
+                m_context.addBundleListener(m_listener);
+            }
+           }
 
             Bundle[] bundles = m_context.getBundles();
             for (int i = 0; i < bundles.length; i++) {
                 if (bundles[i].getState() == Bundle.ACTIVE) {
-                    m_bundleSet.add(bundles[i]);
-                    addedBundle(bundles[i]);
+                    if (m_bundleSet.add(bundles[i])) {
+                        addedBundle(bundles[i]);
+                    }
                 }
             }
-        }
     }
 
     /**

Modified: felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java?rev=762084&r1=762083&r2=762084&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java (original)
+++ felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java Sun Apr  5 14:02:15 2009
@@ -195,7 +195,7 @@
                     return new AnnotationCollector(md); 
                 }
             } else {
-                // Avoid constructors.
+                // no constructors.
                 if (!(name.startsWith("_get") || // Avoid getter method
                         name.startsWith("_set") || // Avoid setter method
                         name.equals("_setComponentManager") || // Avoid the set method

Modified: felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java?rev=762084&r1=762083&r2=762084&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java (original)
+++ felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java Sun Apr  5 14:02:15 2009
@@ -120,6 +120,18 @@
      * method.
      */
     private List m_visitedMethods = new ArrayList();
+    
+    /**
+     * Set to <code>true</code> when a suitable constructor
+     * is found. If not set to <code>true</code> at the end
+     * of the visit, the manipulator injects a constructor.
+     */
+    private boolean m_foundSuitableConstructor = false;
+
+    /**
+     * Name of the super class.
+     */
+    private String m_superclass;
 
     /**
      * Constructor.
@@ -148,6 +160,7 @@
      */
     public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
         m_owner = name;
+        m_superclass = superName;
         addPOJOInterface(version, access, name, signature, superName, interfaces);
         addIMField();
     }
@@ -179,8 +192,10 @@
             Type[] args = Type.getArgumentTypes(desc);
             if (args.length == 0) {
                 generateEmptyConstructor(access, signature, exceptions, md.getAnnotations());
+                m_foundSuitableConstructor = true;
             } else if (args.length == 1 && args[0].getClassName().equals("org.osgi.framework.BundleContext")) {
                 generateBCConstructor(access, signature, exceptions, md.getAnnotations());
+                m_foundSuitableConstructor = true;
             } else {
                 // Do nothing, the constructor does not match.
                 return cv.visitMethod(access, name, desc, signature, exceptions);
@@ -535,6 +550,11 @@
 
         // Add the getComponentInstance
         createGetComponentInstanceMethod();
+        
+        // Need to inject a constructor?
+        if (! m_foundSuitableConstructor) { // No adequate constructor, create one.
+            createSimpleConstructor();
+        }
 
         m_methods.clear();
         m_methodFlags.clear();
@@ -543,6 +563,31 @@
     }
 
     /**
+     * Creates a simple constructor with an instance manager
+     * in argument if no suitable constructor is found during
+     * the visit.
+     */
+    private void createSimpleConstructor() {
+        MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>",
+                "(Lorg/apache/felix/ipojo/InstanceManager;)V", null, null);
+        mv.visitCode();
+
+        // Super call
+        mv.visitVarInsn(ALOAD, 0);
+        mv.visitMethodInsn(INVOKESPECIAL, m_superclass, "<init>", "()V");
+
+        // Call set instance manager
+        mv.visitVarInsn(ALOAD, 0);
+        mv.visitVarInsn(ALOAD, 1);
+        mv.visitMethodInsn(INVOKEVIRTUAL, m_owner, "_setInstanceManager",
+                "(Lorg/apache/felix/ipojo/InstanceManager;)V");
+
+        mv.visitInsn(RETURN);
+        mv.visitMaxs(0, 0);
+        mv.visitEnd();
+    }
+
+    /**
      * Create the setter method for the __cm field.
      */
     private void createSetInstanceManagerMethod() {

Added: felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ASimpleParentClass.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ASimpleParentClass.java?rev=762084&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ASimpleParentClass.java (added)
+++ felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ASimpleParentClass.java Sun Apr  5 14:02:15 2009
@@ -0,0 +1,11 @@
+package org.apache.felix.ipojo.test.scenarios.component;
+
+public class ASimpleParentClass {
+    
+    protected String name;
+    
+    public ASimpleParentClass() {
+        name = "hello";
+    }
+
+}

Propchange: felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ASimpleParentClass.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructor.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructor.java?rev=762084&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructor.java (added)
+++ felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructor.java Sun Apr  5 14:02:15 2009
@@ -0,0 +1,31 @@
+package org.apache.felix.ipojo.test.scenarios.component;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.test.scenarios.manipulation.service.CheckService;
+
+public class NoEmptyConstructor implements CheckService {
+    
+   
+        private String name;
+
+        public NoEmptyConstructor(final String n) {
+            name = n;
+        }
+
+        public boolean check() {
+            return name != null;
+        }
+
+        public Properties getProps() {
+            Properties props = new Properties();
+            if (name == null) {
+                props.put("name", "NULL");
+            } else {
+                props.put("name", name);
+            }
+            return props;
+        }
+
+
+}

Propchange: felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructorWithParentClass.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructorWithParentClass.java?rev=762084&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructorWithParentClass.java (added)
+++ felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructorWithParentClass.java Sun Apr  5 14:02:15 2009
@@ -0,0 +1,27 @@
+package org.apache.felix.ipojo.test.scenarios.component;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.test.scenarios.manipulation.service.CheckService;
+
+public class NoEmptyConstructorWithParentClass extends ASimpleParentClass implements CheckService  {
+    
+        public NoEmptyConstructorWithParentClass(final String n) {
+            name = n;
+        }
+
+        public boolean check() {
+            return name != null;
+        }
+
+        public Properties getProps() {
+            Properties props = new Properties();
+            if (name == null) {
+                props.put("name", "NULL");
+            } else {
+                props.put("name", name);
+            }
+            return props;
+        }
+
+}

Propchange: felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/component/NoEmptyConstructorWithParentClass.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/manipulation/SeveralConstructorTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/manipulation/SeveralConstructorTest.java?rev=762084&r1=762083&r2=762084&view=diff
==============================================================================
--- felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/manipulation/SeveralConstructorTest.java (original)
+++ felix/trunk/ipojo/tests/manipulator/creation/src/main/java/org/apache/felix/ipojo/test/scenarios/manipulation/SeveralConstructorTest.java Sun Apr  5 14:02:15 2009
@@ -11,11 +11,14 @@
     
     private IPOJOHelper helper;
     
-    private ComponentInstance ci;
+    private ComponentInstance ci, ci2, ci3;
     
     public void setUp() {
         helper = new IPOJOHelper(this);
         ci = helper.createComponentInstance("org.apache.felix.ipojo.test.scenarios.component.SeveralConstructors");
+        ci2 = helper.createComponentInstance("org.apache.felix.ipojo.test.scenarios.component.NoEmptyConstructor");
+        ci3 = helper.createComponentInstance("org.apache.felix.ipojo.test.scenarios.component.NoEmptyConstructorWithParentClass");
+
     }
     
     public void tearDown() {
@@ -29,6 +32,23 @@
         assertTrue("Check assignation", cs.check());
         String name = (String) cs.getProps().get("name");
         assertEquals("Check message", "hello world", name);
+        //assertNull("Check message", name);
+    }
+    
+    public void testNoEmptyConstructor() {
+        ServiceReference ref = helper.getServiceReferenceByName(CheckService.class.getName(), ci2.getInstanceName());
+        CheckService cs = (CheckService) getServiceObject(ref);
+        assertFalse("Check assignation", cs.check());
+        String name = (String) cs.getProps().get("name");
+        assertEquals("Check message", "NULL", name);
+    }
+    
+    public void testNoEmptyConstructorWithAParentClass() {
+        ServiceReference ref = helper.getServiceReferenceByName(CheckService.class.getName(), ci3.getInstanceName());
+        CheckService cs = (CheckService) getServiceObject(ref);
+        assertTrue("Check assignation", cs.check()); // super set name to "hello"
+        String name = (String) cs.getProps().get("name");
+        assertEquals("Check message", "hello", name);
     }
 
 }

Modified: felix/trunk/ipojo/tests/manipulator/creation/src/main/resources/metadata.xml
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/manipulator/creation/src/main/resources/metadata.xml?rev=762084&r1=762083&r2=762084&view=diff
==============================================================================
--- felix/trunk/ipojo/tests/manipulator/creation/src/main/resources/metadata.xml (original)
+++ felix/trunk/ipojo/tests/manipulator/creation/src/main/resources/metadata.xml Sun Apr  5 14:02:15 2009
@@ -63,4 +63,11 @@
 	 <component classname="org.apache.felix.ipojo.test.scenarios.component.SeveralConstructors">
 	 	<provides/>
 	 </component>
+	 <!--  No Empty constructor -->
+	 <component classname="org.apache.felix.ipojo.test.scenarios.component.NoEmptyConstructor">
+	 	<provides/>
+	 </component>
+	 <component classname="org.apache.felix.ipojo.test.scenarios.component.NoEmptyConstructorWithParentClass">
+	 	<provides/>
+	 </component>
 </ipojo>