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 2010/12/22 17:57:30 UTC
svn commit: r1051984 - in
/felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo:
InstanceManager.java handlers/configuration/ConfigurationHandler.java
parser/MethodMetadata.java
Author: clement
Date: Wed Dec 22 16:57:30 2010
New Revision: 1051984
URL: http://svn.apache.org/viewvc?rev=1051984&view=rev
Log:
Compute the constructor id.
Fix a bug when iterating over the constructor injector set.
Add a default name if the @Property does not have a name
Modified:
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/handlers/configuration/ConfigurationHandler.java
felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/parser/MethodMetadata.java
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=1051984&r1=1051983&r2=1051984&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 Wed Dec 22 16:57:30 2010
@@ -620,25 +620,26 @@ public class InstanceManager implements
Class[] types = new Class[m_constructorRegistration.size() + 1];
values[0] = this;
types[0] = InstanceManager.class;
+
+ // Iterate over the constructor injector
for (int i = 0; i < m_constructorRegistration.size(); i++) {
- for (int j = 0; values[i] == null && j < m_handlers.length; j++) {
- 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();
- }
- }
+ 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();
+ }
}
// Find the constructor.
Constructor cst = m_clazz.getDeclaredConstructor(types);
if (! cst.isAccessible()) {
cst.setAccessible(true);
}
- onEntry(null, null /*TODO Compute id*/, values);
+ String methodId = MethodMetadata.computeMethodId(cst);
+ onEntry(null, methodId, values);
instance = cst.newInstance(values);
- onExit(instance, null /*TODO Compute id*/, instance);
+ onExit(instance, methodId, instance);
} else {
// Old semantic
// Try to find if there is a constructor with a bundle context as parameter :
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=1051984&r1=1051983&r2=1051984&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 16:57:30 2010
@@ -123,19 +123,21 @@ 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");
+ String paramIndex = configurables[i].getAttribute("constructor-parameter");
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");
+ " at least a field, a method or a constructor-parameter");
}
String name = configurables[i].getAttribute("name");
if (name == null) {
- if (fieldName == null) {
+ if (fieldName == null && methodName != null) {
name = methodName;
+ } else if (fieldName == null && paramIndex != null) {
+ name = paramIndex;
} else {
- name = fieldName;
+ name = fieldName;
}
configurables[i].addAttribute(new Attribute("name", name)); // Add the type to avoid configure checking
}
@@ -255,7 +257,7 @@ 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");
+ String paramIndex = configurables[i].getAttribute("constructor-parameter");
int index = -1;
String name = configurables[i].getAttribute("name"); // The initialize method has fixed the property name.
Modified: felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/parser/MethodMetadata.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/parser/MethodMetadata.java?rev=1051984&r1=1051983&r2=1051984&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/parser/MethodMetadata.java (original)
+++ felix/sandbox/clement/ipojo-constructor-injection/core/src/main/java/org/apache/felix/ipojo/parser/MethodMetadata.java Wed Dec 22 16:57:30 2010
@@ -1,4 +1,4 @@
-/*
+/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -18,6 +18,7 @@
*/
package org.apache.felix.ipojo.parser;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.apache.felix.ipojo.metadata.Element;
@@ -28,7 +29,7 @@ import org.apache.felix.ipojo.metadata.E
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
public class MethodMetadata {
-
+
/**
* Empty Constructor Method Id.
*/
@@ -38,7 +39,7 @@ public class MethodMetadata {
* Bundle Context Constructor Method Id.
*/
public static final String BC_CONSTRUCTOR_ID = "$init$org_osgi_framework_BundleContext";
-
+
/**
* Constructor Prefix.
*/
@@ -50,12 +51,12 @@ public class MethodMetadata {
private String m_name;
/**
- * The argument type array.
+ * The argument type array.
*/
private String[] m_arguments = new String[0];
/**
- * The returned type.
+ * The returned type.
*/
private String m_return = "void";
@@ -110,7 +111,7 @@ public class MethodMetadata {
}
/**
- * Computes the method id for the given Method object.
+ * Computes the method id for the given Method object.
* @param method the Method object.
* @return the method id.
*/
@@ -140,4 +141,36 @@ public class MethodMetadata {
}
return identifier.toString();
}
+
+ /**
+ * Computes the method id for the given Constructor object.
+ * @param method the Method object.
+ * @return the method id.
+ */
+ public static String computeMethodId(Constructor method) {
+ StringBuffer identifier = new StringBuffer("$init");
+ Class[] args = method.getParameterTypes();
+ for (int i = 0; i < args.length; i++) {
+ identifier.append('$'); // Argument separator.
+ if (args[i].isArray()) {
+ if (args[i].getComponentType().isPrimitive()) {
+ // Primitive array
+ identifier.append(FieldMetadata.getPrimitiveTypeByClass(args[i].getComponentType()));
+ } else {
+ // Object array
+ identifier.append(args[i].getComponentType().getName().replace('.', '_')); // Replace '.' by '_'
+ }
+ identifier.append("__"); // Add __ (array)
+ } else {
+ if (args[i].isPrimitive()) {
+ // Primitive type
+ identifier.append(FieldMetadata.getPrimitiveTypeByClass(args[i]));
+ } else {
+ // Object type
+ identifier.append(args[i].getName().replace('.', '_')); // Replace '.' by '_'
+ }
+ }
+ }
+ return identifier.toString();
+ }
}