felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clem...@apache.org
Subject svn commit: r1051806 - in /felix/sandbox/clement/ipojo-constructor-injection: core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ core/src/main/java/org/apache/felix/ipojo/util/ manipulator/src/main/java/org/apache/felix/ipojo/manipulatio...
Date Wed, 22 Dec 2010 09:58:50 GMT
Author: clement
Date: Wed Dec 22 09:58:50 2010
New Revision: 1051806

URL: http://svn.apache.org/viewvc?rev=1051806&view=rev
Log:
Implement the constructor injection support for @Property (still work in progress, tests are
missing).

Property implements the constructor injector interface.

Add the type attribute when discovering a parameter annotation.


Modified:
    felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
    felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/util/Property.java
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/CustomAnnotationVisitor.java
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/MethodCollector.java

Modified: felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java?rev=1051806&r1=1051805&r2=1051806&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
Wed Dec 22 09:58:50 2010
@@ -123,9 +123,11 @@ public class ConfigurationHandler extend
         for (int i = 0; configurables != null && i < configurables.length; i++)
{
             String fieldName = configurables[i].getAttribute("field");
             String methodName = configurables[i].getAttribute("method");
+            String paramIndex = configurables[i].getAttribute("contructor-parameter");
 
-            if (fieldName == null && methodName == null) {
-                throw new ConfigurationException("Malformed property : The property needs
to contain at least a field or a method");
+            if (fieldName == null && methodName == null  && paramIndex ==
null) {
+                throw new ConfigurationException("Malformed property : The property needs
to contain" +
+                		" at least a field, a method or a contructor-parameter");
             }
 
             String name = configurables[i].getAttribute("name");
@@ -143,7 +145,7 @@ public class ConfigurationHandler extend
             // Detect the type of the property
             PojoMetadata manipulation = getFactory().getPojoMetadata();
             String type = null;
-            if (fieldName == null) {
+            if (methodName != null) {
                 MethodMetadata[] method = manipulation.getMethods(methodName);
                 if (method.length == 0) {
                     type = configurables[i].getAttribute("type");
@@ -157,11 +159,24 @@ public class ConfigurationHandler extend
                     type = method[0].getMethodArguments()[0];
                     configurables[i].addAttribute(new Attribute("type", type)); // Add the
type to avoid configure checking
                 }
-            } else {
+            } else if (fieldName != null) {
                 FieldMetadata field = manipulation.getField(fieldName);
                 if (field == null) { throw new ConfigurationException("Malformed property
: The field " + fieldName + " does not exist in the implementation class"); }
                 type = field.getFieldType();
                 configurables[i].addAttribute(new Attribute("type", type)); // Add the type
to avoid configure checking
+            } else if (paramIndex != null) {
+            	int index = Integer.parseInt(paramIndex);
+            	type = configurables[i].getAttribute("type");
+        		MethodMetadata[] cts = manipulation.getConstructors();
+        		// If we don't have a type, try to get the first constructor and get the type of
the parameter
+        		// we the index 'index'.
+            	if (type == null && cts.length != 0  && cts[0].getMethodArguments().length
> index) {
+            		type = cts[0].getMethodArguments()[index];
+            		configurables[i].addAttribute(new Attribute("type", type));
+            	} else {
+            		throw new ConfigurationException("Cannot determine the type of the property
" + index +
+            				", please use the type attribute");
+            	}
             }
 
             // Is the property set to immutable
@@ -240,13 +255,22 @@ public class ConfigurationHandler extend
         for (int i = 0; configurables != null && i < configurables.length; i++)
{
             String fieldName = configurables[i].getAttribute("field");
             String methodName = configurables[i].getAttribute("method");
+            String paramIndex = configurables[i].getAttribute("contructor-parameter");
+            int index = -1;
 
             String name = configurables[i].getAttribute("name"); // The initialize method
has fixed the property name.
             String value = configurables[i].getAttribute("value");
 
             String type = configurables[i].getAttribute("type"); // The initialize method
has fixed the property name.
 
-            Property prop = new Property(name, fieldName, methodName, value, type, getInstanceManager(),
this);
+            Property prop = null;
+            if (paramIndex == null) {
+            	prop = new Property(name, fieldName, methodName, value, type, getInstanceManager(),
this);
+            } else {
+            	index = Integer.parseInt(paramIndex);
+            	prop = new Property(name, fieldName, methodName, index,
+            			value, type, getInstanceManager(), this);
+            }
             addProperty(prop);
 
             // Check if the instance configuration contains value for the current property
:
@@ -262,6 +286,10 @@ public class ConfigurationHandler extend
                 FieldMetadata field = new FieldMetadata(fieldName, type);
                 getInstanceManager().register(field, prop);
             }
+
+            if (index != -1) {
+            	getInstanceManager().register(index, prop);
+            }
         }
 
         m_description = new ConfigurationHandlerDescription(this, m_configurableProperties,
m_managedServicePID);

Modified: felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/util/Property.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/util/Property.java?rev=1051806&r1=1051805&r2=1051806&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/util/Property.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/util/Property.java
Wed Dec 22 09:58:50 2010
@@ -25,6 +25,7 @@ import java.lang.reflect.Method;
 
 import org.apache.felix.ipojo.ComponentInstance;
 import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.ConstructorInjector;
 import org.apache.felix.ipojo.FieldInterceptor;
 import org.apache.felix.ipojo.Handler;
 import org.apache.felix.ipojo.InstanceManager;
@@ -36,7 +37,7 @@ import org.osgi.framework.BundleContext;
  * This class managed the method invocation as well as field injection.
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
-public class Property implements FieldInterceptor {
+public class Property implements FieldInterceptor, ConstructorInjector {
 
     /**
      * Object used for an unvalued property.
@@ -62,6 +63,12 @@ public class Property implements FieldIn
     private final Callback m_method;
 
     /**
+     * The index of the parameter in case of
+     * constructor injection.
+     */
+    private int m_index = -1;
+
+    /**
      * The value of the property.
      */
     private Object m_value = NO_VALUE;
@@ -125,7 +132,12 @@ public class Property implements FieldIn
         } else {
             m_method = null;
         }
+    }
 
+    public Property(String name, String field, String method, int index,
+    		String value, String type, InstanceManager manager, Handler handler) throws ConfigurationException
{
+    	this(name, field, method, value, type, manager, handler);
+    	m_index = index;
     }
 
     /**
@@ -258,6 +270,16 @@ public class Property implements FieldIn
     }
 
     /**
+     * Gets the parameter index.
+     * @return the parameter index or <code>-1</code>
+     * if this property is not injected using constructor
+     * parameter.
+     */
+    public int getParameterIndex() {
+    	return m_index;
+    }
+
+    /**
      * Checks if the property has a field.
      * @return <code>true</code> if the property has a field.
      */
@@ -566,7 +588,18 @@ public class Property implements FieldIn
         }
     }
 
-    /**
+    public Object getConstructorParameter(int index) {
+    	if (m_index != index) {
+    		return null;
+    	}
+
+    	if (m_value  == NO_VALUE) {
+           return getNoValue(m_type);
+        }
+        return m_value;
+	}
+
+	/**
      * Gets the handler managing the property.
      * @return the configuration handler.
      */

Modified: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/CustomAnnotationVisitor.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/CustomAnnotationVisitor.java?rev=1051806&r1=1051805&r2=1051806&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/CustomAnnotationVisitor.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/CustomAnnotationVisitor.java
Wed Dec 22 09:58:50 2010
@@ -23,6 +23,7 @@ import java.lang.reflect.Array;
 import org.apache.felix.ipojo.metadata.Attribute;
 import org.apache.felix.ipojo.metadata.Element;
 import org.objectweb.asm.AnnotationVisitor;
+import org.objectweb.asm.Type;
 import org.objectweb.asm.commons.EmptyVisitor;
 
 /**
@@ -234,6 +235,8 @@ public class CustomAnnotationVisitor ext
             m_collector.getElements().put(m_elem, m_parent);
 
             if (m_isParameterAnnotation) {
+            	String t = Type.getArgumentTypes(m_desc)[m_index].getClassName();
+            	m_elem.addAttribute(new Attribute("type", t));
             	m_elem.addAttribute(
             			new Attribute("constructor-parameter", Integer.toString(m_index)));
             }

Modified: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/MethodCollector.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/MethodCollector.java?rev=1051806&r1=1051805&r2=1051806&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/MethodCollector.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/MethodCollector.java
Wed Dec 22 09:58:50 2010
@@ -609,6 +609,8 @@ public class MethodCollector extends Emp
             }
 
             if (m_isParameterAnnotation) {
+            	String t = Type.getArgumentTypes(m_descriptor)[m_index].getClassName();
+            	prop.addAttribute(new Attribute("type", t));
                 prop.addAttribute(new Attribute("constructor-parameter", Integer.toString(m_index)));
             } else {
                 prop.addAttribute(new Attribute("method", m_method));



Mime
View raw message