FELIX-4216 Allow @Property without name in constructors
Use the parameter's name to infer the property's name.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1520079 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
index d296a3c..0c6a7c6 100644
--- a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
+++ b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
@@ -127,7 +127,25 @@
if (fieldName == null && methodName != null) {
name = methodName;
} else if (fieldName == null && paramIndex != null) {
- name = paramIndex;
+ try {
+ // Extract the name from the arguments.
+ MethodMetadata[] constructors = getFactory().getPojoMetadata().getConstructors();
+ if (constructors.length != 1) {
+ throw new ConfigurationException("Cannot infer the property name injected in the constructor " +
+ "parameter #" + paramIndex + " - add the `name` attribute");
+ } else {
+ int idx = Integer.valueOf(paramIndex);
+ if (constructors[0].getMethodArgumentNames().length > idx) {
+ name = constructors[0].getMethodArgumentNames()[idx];
+ } else {
+ throw new ConfigurationException("Cannot infer the property name injected in the constructor " +
+ "parameter #" + paramIndex + " - not enough argument in the constructor :" +
+ constructors[0].getArguments());
+ }
+ }
+ } catch(Throwable e) {
+ e.printStackTrace();
+ }
} else {
name = fieldName;
}