felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clem...@apache.org
Subject svn commit: r1051155 - in /felix/sandbox/clement/ipojo-constructor-injection: core/src/main/java/org/apache/felix/ipojo/ core/src/main/java/org/apache/felix/ipojo/parser/ manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/
Date Mon, 20 Dec 2010 15:59:57 GMT
Author: clement
Date: Mon Dec 20 15:59:57 2010
New Revision: 1051155

URL: http://svn.apache.org/viewvc?rev=1051155&view=rev
Log:
Overall simplification of the constructor injection.
- Remove the need of 'constructor' element in the metadata.
- Identify parameters per index

Removed:
    felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/parser/ConstructorMetadata.java
Modified:
    felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
    felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/ConstructorInjector.java
    felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/HandlerManager.java
    felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/HandlerManagerFactory.java
    felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
    felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/PrimitiveHandler.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/MetadataCollector.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/ComponentFactory.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java?rev=1051155&r1=1051154&r2=1051155&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
Mon Dec 20 15:59:57 2010
@@ -30,12 +30,12 @@ import java.util.Map;
 import org.apache.felix.ipojo.architecture.ComponentTypeDescription;
 import org.apache.felix.ipojo.metadata.Attribute;
 import org.apache.felix.ipojo.metadata.Element;
-import org.apache.felix.ipojo.parser.ConstructorMetadata;
 import org.apache.felix.ipojo.parser.ParseUtils;
 import org.apache.felix.ipojo.parser.PojoMetadata;
 import org.apache.felix.ipojo.util.Logger;
 import org.apache.felix.ipojo.util.Tracker;
 import org.apache.felix.ipojo.util.TrackerCustomizer;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
@@ -92,9 +92,6 @@ public class ComponentFactory extends IP
      */
     private PojoMetadata m_manipulation;
 
-
-	private ConstructorMetadata m_constructor;
-
     /**
      * Creates a instance manager factory.
      * The class is given in parameter. The component type is not a composite.
@@ -139,7 +136,6 @@ public class ComponentFactory extends IP
         m_classname = element.getAttribute("classname");
         if (m_classname == null) { throw new ConfigurationException("A component needs a
class name : " + element); }
         m_manipulation = new PojoMetadata(m_componentMetadata);
-        m_constructor = new ConstructorMetadata(m_componentMetadata);
     }
 
     /**
@@ -296,8 +292,7 @@ public class ComponentFactory extends IP
         Element[] elems = m_componentMetadata.getElements();
         for (int i = 0; i < elems.length; i++) {
             Element current = elems[i];
-            if (!"manipulation".equals(current.getName()) // Remove the manipulation element
-            		&& !"constructor".equals(current.getName())) { // Remove the constructor
element
+            if (!"manipulation".equals(current.getName())) { // Remove the manipulation element
                 RequiredHandler req = new RequiredHandler(current.getName(), current.getNameSpace());
                 if (!list.contains(req)) {
                     list.add(req);
@@ -440,10 +435,6 @@ public class ComponentFactory extends IP
         return m_manipulation;
     }
 
-    public ConstructorMetadata getConstructorMetadata() {
-    	return m_constructor;
-    }
-
     /**
      * Gets the version of the component type.
      * @return the version of <code>null</code> if not set.

Modified: felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/ConstructorInjector.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/ConstructorInjector.java?rev=1051155&r1=1051154&r2=1051155&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/ConstructorInjector.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/ConstructorInjector.java
Mon Dec 20 15:59:57 2010
@@ -2,6 +2,6 @@ package org.apache.felix.ipojo;
 
 public interface ConstructorInjector {
 
-	Object getConstructorParameter(String id, int index);
+	Object getConstructorParameter(int index);
 
 }

Modified: felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/HandlerManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/HandlerManager.java?rev=1051155&r1=1051154&r2=1051155&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/HandlerManager.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/HandlerManager.java
Mon Dec 20 15:59:57 2010
@@ -241,11 +241,4 @@ public class HandlerManager extends Inst
         }
     }
 
-	public Object getConstructorParameter(String id, int index) {
-		if (m_handler instanceof ConstructorInjector) {
-			return ((ConstructorInjector) m_handler).getConstructorParameter(id, index);
-		}
-		return null;
-	}
-
 }

Modified: felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/HandlerManagerFactory.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/HandlerManagerFactory.java?rev=1051155&r1=1051154&r2=1051155&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/HandlerManagerFactory.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/HandlerManagerFactory.java
Mon Dec 20 15:59:57 2010
@@ -152,8 +152,7 @@ public class HandlerManagerFactory exten
         Element[] elems = m_componentMetadata.getElements();
         for (int i = 0; i < elems.length; i++) {
             Element current = elems[i];
-            if (!"manipulation".equals(current.getName()) // Remove the manipulation element
-            		&& !"constructor".equals(current.getName())) { // Remove the constructor
element
+            if (!"manipulation".equals(current.getName())) { // Remove the manipulation element
                 RequiredHandler req = new RequiredHandler(current.getName(),
                         current.getNameSpace());
                 if (!list.contains(req)) {

Modified: felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java?rev=1051155&r1=1051154&r2=1051155&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
Mon Dec 20 15:59:57 2010
@@ -105,12 +105,20 @@ public class InstanceManager implements 
     private Map m_fieldRegistration;
 
     /**
-     * the map [method identifier, {@link MethodInterceptor} list] storing handlers interested
by the method.
-     * Once configure this map can't change.
+     * the map [method identifier, {@link MethodInterceptor} list] interested
+     * by the method.
+     * Once configured, this map can't change.
      */
     private Map m_methodRegistration;
 
     /**
+     * the map (sorted by parameter index) or {@link ConstructorInjector} interested by
+     * injecting constructor parameter.
+     * Once configured, this list can't change.
+     */
+    private Map m_constructorRegistration;
+
+    /**
      * The manipulated class.
      * Once set, this field doesn't change.
      */
@@ -197,6 +205,15 @@ public class InstanceManager implements 
         for (int i = 0; i < m_handlers.length; i++) {
             m_handlers[i].init(this, metadata, configuration);
         }
+
+        // Check that the constructor parameter are continuous.
+        if (m_constructorRegistration != null) {
+        	for (int i = 0; i < m_constructorRegistration.size(); i++) {
+        		if (! m_constructorRegistration.containsKey(new Integer(i))) {
+        			throw new ConfigurationException("The constructor parameter " + i + " is not managed");
+        		}
+        	}
+        }
     }
 
     /**
@@ -596,18 +613,18 @@ public class InstanceManager implements 
             // No factory-method, we use the constructor.
             try {
             	// Try to find the correct constructor.
-            	if (getFactory().getConstructorMetadata().hasConstructorMetadata()) {
-            		// Build the map of injected values.
-            		String[] ids = getFactory().getConstructorMetadata().getIds();
+            	if (m_constructorRegistration != null) {
             		// Initialize the injected values and types
             		// We have the IM first.
-            		Object[] values = new Object[ids.length + 1];
-            		Class[] types = new Class[ids.length + 1];
+            		Object[] values = new Object[m_constructorRegistration.size() + 1];
+            		Class[] types = new Class[m_constructorRegistration.size() + 1];
             		values[0] = this;
             		types[0] = InstanceManager.class;
-            		for (int i = 0; i < ids.length; i++) {
+            		for (int i = 0; i < m_constructorRegistration.size(); i++) {
             			for (int j = 0; values[i] == null  && j < m_handlers.length; j++)
{
-            				Object v = m_handlers[j].getConstructorParameter(ids[i], i);
+            				ConstructorInjector injector = (ConstructorInjector)
+            					m_constructorRegistration.get(new Integer(i));
+            				Object v = injector.getConstructorParameter(i);
             				if (v != null) {
             					values[i + 1] = v;
             					types[i + 1] = v.getClass();
@@ -967,6 +984,19 @@ public class InstanceManager implements 
         }
     }
 
+    public void register(int index, ConstructorInjector injector) throws ConfigurationException
{
+    	Integer key = new Integer(index);
+    	if (m_constructorRegistration == null) {
+    		m_constructorRegistration = new HashMap();
+    	}
+    	if (! m_constructorRegistration.containsKey(key)) {
+    		m_constructorRegistration.put(key, injector);
+    	} else {
+    		throw new ConfigurationException("Another constructor injector " +
+    				"manages the parameter " + index);
+    	}
+    }
+
     /**
      * This method is called by the manipulated class each time that a GETFIELD instruction
is executed.
      * The method asks to each attached handler monitoring this field which value need

Modified: felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/PrimitiveHandler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/PrimitiveHandler.java?rev=1051155&r1=1051154&r2=1051155&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/PrimitiveHandler.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/PrimitiveHandler.java
Mon Dec 20 15:59:57 2010
@@ -163,7 +163,7 @@ public abstract class PrimitiveHandler e
      * TODO
      * @see org.apache.felix.ipojo.ConstructorInjector#getConstructorParameter(java.lang.String,
int)
      */
-    public Object getConstructorParameter(String id, int index) {
+    public Object getConstructorParameter(int index) {
     	return null;
     }
 

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=1051155&r1=1051154&r2=1051155&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
Mon Dec 20 15:59:57 2010
@@ -31,8 +31,6 @@ import org.objectweb.asm.commons.EmptyVi
  */
 public class CustomAnnotationVisitor extends EmptyVisitor implements AnnotationVisitor {
 
-    //TODO manage enum annotations.
-
     /**
      * Parent element.
      */
@@ -236,7 +234,8 @@ public class CustomAnnotationVisitor ext
             m_collector.getElements().put(m_elem, m_parent);
 
             if (m_isParameterAnnotation) {
-            	m_collector.addConstructorParameter(m_index, m_id);
+            	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/MetadataCollector.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/MetadataCollector.java?rev=1051155&r1=1051154&r2=1051155&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/MetadataCollector.java
(original)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/annotations/MetadataCollector.java
Mon Dec 20 15:59:57 2010
@@ -659,64 +659,5 @@ public class MetadataCollector extends E
         }
     }
 
-    public void createConstructorElement(String descriptor) {
-    	Element constructor = null;
-		if (! m_elem.containsElement("constructor")) {
-			constructor = new Element("constructor", null);
-			Type[] types = Type.getArgumentTypes(descriptor);
-
-			StringBuffer args = new StringBuffer("{");
-			for (int i = 0; i < types.length; i++) {
-				Type type = types[i];
-				String t = null;
-				if (type.getSort() == Type.ARRAY) {
-		            if (type.getInternalName().startsWith("L")) {
-		                String internalType = type.getInternalName().substring(1);
-		                String nameType = internalType.replace('/', '.');
-		                t = nameType + "[]";
-		            } else {
-		                String nameType = type.getClassName().substring(0,
-		                        type.getClassName().length() - 2);
-		                t = nameType + "[]";
-		            }
-		        } else {
-		            t = type.getClassName();
-		        }
-
-				if (i == 0) {
-					args.append(t);
-				} else {
-					args.append("," + t);
-				}
-			}
-			args.append("}");
-			constructor.addAttribute(new Attribute("parameters", args.toString()));
-			m_elem.addElement(constructor);
-		}
-    }
-
-	/**
-	 * Adds a constructor parameter.
-	 * @param index the index
-	 * @param id the refid
-	 */
-	public void addConstructorParameter(int index, String id) {
-		if (id == null) {
-            System.err.println("Cannot compute contructor parameter : no refid");
-
-		}
-		Element constructor = null;
-		if (m_elem.containsElement("constructor")) {
-			constructor = m_elem.getElements("constructor")[0];
-			// Parameter element
-			Element param = new Element("param", null);
-			param.addAttribute(new Attribute("index", "" + index));
-			param.addAttribute(new Attribute("refid", id));
-			constructor.addElement(param);
-		} else {
-			System.err.println("No constructor found");
-		}
-	}
-
 }
 

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=1051155&r1=1051154&r2=1051155&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
Mon Dec 20 15:59:57 2010
@@ -63,25 +63,18 @@ public class MethodCollector extends Emp
     public AnnotationVisitor visitParameterAnnotation(int index, String annotation,
 			boolean visible) {
     	if (m_name.equals("<init>")) {
-    		AnnotationVisitor visitor = null;
     		if (annotation.equals("Lorg/apache/felix/ipojo/annotations/Property;")) {
-                visitor = processProperty(true, index);
+                return processProperty(true, index);
             }
             if (annotation.equals("Lorg/apache/felix/ipojo/annotations/Requires;")) {
-                visitor = new BindAnnotationParser(index);
+                return new BindAnnotationParser(index);
             }
 
-            if (visitor == null  && CustomAnnotationVisitor.isCustomAnnotation(annotation))
{
+            if (CustomAnnotationVisitor.isCustomAnnotation(annotation)) {
                 Element elem = CustomAnnotationVisitor.buildElement(annotation);
                 elem.addAttribute(new Attribute("index", "" + index));
-                visitor = new CustomAnnotationVisitor(elem, m_collector, true, false, index,
m_descriptor);
+                return new CustomAnnotationVisitor(elem, m_collector, true, false, index,
m_descriptor);
             }
-
-            if (visitor != null) {
-            	m_collector.createConstructorElement(m_descriptor);
-            	return visitor;
-            }
-
     	}
     	return super.visitParameterAnnotation(index, annotation, visible);
 	}
@@ -484,7 +477,7 @@ public class MethodCollector extends Emp
 	            method.addAttribute(new Attribute("type", m_type));
 	            req.addElement(method);
             } else {
-            	m_collector.addConstructorParameter(m_index, m_id);
+            	req.addAttribute(new Attribute("constructor-parameter", Integer.toString(m_index)));
             }
 
             m_collector.getIds().put(m_id, req);
@@ -616,7 +609,7 @@ public class MethodCollector extends Emp
             }
 
             if (m_isParameterAnnotation) {
-            	m_collector.addConstructorParameter(m_index, m_id);
+                prop.addAttribute(new Attribute("constructor-parameter", Integer.toString(m_index)));
             } else {
                 prop.addAttribute(new Attribute("method", m_method));
             }



Mime
View raw message