FELIX-4957 : [DS][RFC-212] Various issues with field references


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1691279 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/helper/FieldHandler.java b/scr/src/main/java/org/apache/felix/scr/impl/helper/FieldHandler.java
index 322c8b8..b3eeeae 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/helper/FieldHandler.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/helper/FieldHandler.java
@@ -31,6 +31,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.CopyOnWriteArraySet;
 
 import org.apache.felix.scr.impl.manager.ComponentContextImpl;
@@ -479,45 +480,52 @@
     {
         try
         {
-        	// only optional field need initialization
-        	if ( metadata.isOptional() )
-        	{
-	            if ( metadata.isMultiple() )
-	            {
-	                if ( metadata.isReplace()  )
-	                {
-	                    this.setFieldValue(componentInstance, new ArrayList<Object>());
-	                }
-	                else
-	                {
-	                    final Class<?> fieldType = this.field.getType();
-	
-	                    // update strategy: if DS implementation provides collection implementation
-	                    //                  only list and collection are allowed, field must not be final
-	                    final Object providedImpl = this.getFieldValue(componentInstance);
-	                    if ( providedImpl == null)
-	                    {
-	                        if ( Modifier.isFinal(this.field.getModifiers()) )
-	                        {
-	                            logger.log( LogService.LOG_ERROR, "Field {0} in component {1} must not be declared as final", new Object[]
-	                                    {metadata.getField(), this.componentClass}, null );
-	                            valueType = ParamType.ignore;
-	                            return true;
-	                        }
-	                        if ( fieldType != ClassUtils.LIST_CLASS && fieldType != ClassUtils.COLLECTION_CLASS )
-	                        {
-	                            logger.log( LogService.LOG_ERROR, "Field {0} in component {1} has unsupported type {2}."+
-	                                " It must be one of java.util.Collection or java.util.List.",
-	                                new Object[] {metadata.getField(), this.componentClass, fieldType.getName()}, null );
-	                            valueType = ParamType.ignore;
-	                            return true;
-	                        }
-	                        this.setFieldValue(componentInstance, new CopyOnWriteArraySet<Object>());
-	                    }
-	                }
-	            }
-	            else
-	            {
+            if ( metadata.isMultiple() )
+            {
+                if ( metadata.isReplace()  )
+                {
+                    this.setFieldValue(componentInstance, new CopyOnWriteArrayList<Object>());
+                }
+                else
+                {
+                    final Class<?> fieldType = this.field.getType();
+
+                    // update strategy: if DS implementation provides collection implementation
+                    //                  only list and collection are allowed, field must not be final
+                    final Object providedImpl = this.getFieldValue(componentInstance);
+                    if ( providedImpl == null)
+                    {
+                        if ( Modifier.isFinal(this.field.getModifiers()) )
+                        {
+                            logger.log( LogService.LOG_ERROR, "Field {0} in component {1} must not be declared as final", new Object[]
+                                    {metadata.getField(), this.componentClass}, null );
+                            valueType = ParamType.ignore;
+                            return true;
+                        }
+                        if ( fieldType != ClassUtils.LIST_CLASS && fieldType != ClassUtils.COLLECTION_CLASS )
+                        {
+                            logger.log( LogService.LOG_ERROR, "Field {0} in component {1} has unsupported type {2}."+
+                                " It must be one of java.util.Collection or java.util.List.",
+                                new Object[] {metadata.getField(), this.componentClass, fieldType.getName()}, null );
+                            valueType = ParamType.ignore;
+                            return true;
+                        }
+                        if ( fieldType == ClassUtils.LIST_CLASS ) 
+                        {
+                        	this.setFieldValue(componentInstance, new CopyOnWriteArrayList<Object>());
+                        } 
+                        else
+                        {
+                        	this.setFieldValue(componentInstance, new CopyOnWriteArraySet<Object>());
+                        }
+                    }
+                }
+            }
+            else
+            {
+            	// only optional field need initialization
+            	if ( metadata.isOptional() )
+            	{
 	            	// null the field if optional and unary
 	            	this.setFieldValue(componentInstance, null);
 	            }
@@ -525,6 +533,8 @@
         }
         catch ( final InvocationTargetException ite)
         {
+            valueType = ParamType.ignore;
+
             logger.log( LogService.LOG_ERROR, "Field {0} in component {1} can't be initialized.",
                     new Object[] {metadata.getField(), this.componentClass}, ite );
             return false;