summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2011-07-12 19:54:10 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2011-07-12 19:54:10 +0000
commitfbcd4c322ba39715d1ba5e88b88a7ebd7342656d (patch)
tree68f6e9a3953de0f73f7ed70435781362fdd6199c /sca-java-2.x/trunk/modules/implementation-java-runtime/src/main
parentb93d10b4a0b0def94578fe10d870b67351de1bb1 (diff)
Allow other collection types for multiplicity injection
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1145739 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/implementation-java-runtime/src/main')
-rw-r--r--sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ListMultiplicityObjectFactory.java34
-rw-r--r--sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java4
2 files changed, 27 insertions, 11 deletions
diff --git a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ListMultiplicityObjectFactory.java b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ListMultiplicityObjectFactory.java
index 2906897e96..a0af933c71 100644
--- a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ListMultiplicityObjectFactory.java
+++ b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ListMultiplicityObjectFactory.java
@@ -19,7 +19,12 @@
package org.apache.tuscany.sca.implementation.java.injection;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
import org.apache.tuscany.sca.core.factory.ObjectCreationException;
import org.apache.tuscany.sca.core.factory.ObjectFactory;
@@ -30,21 +35,32 @@ import org.apache.tuscany.sca.core.factory.ObjectFactory;
*
* @version $Rev$ $Date$
*/
-public class ListMultiplicityObjectFactory implements ObjectFactory<List<?>> {
+public class ListMultiplicityObjectFactory implements ObjectFactory<Collection<?>> {
- private ObjectFactory[] factories;
+ private Collection<ObjectFactory<?>> factories;
+ private Class<?> collectionType;
- public ListMultiplicityObjectFactory(List<ObjectFactory<?>> factories) {
+ public ListMultiplicityObjectFactory(List<ObjectFactory<?>> factories, Class<?> collectionType) {
assert factories != null : "Object factories were null";
- this.factories = factories.toArray(new ObjectFactory[factories.size()]);
+ this.factories = factories;
+ this.collectionType = collectionType;
}
- public List<?> getInstance() throws ObjectCreationException {
- List<Object> list = new ArrayList<Object>();
- for (ObjectFactory factory : factories) {
- list.add(factory.getInstance());
+ public Collection<?> getInstance() throws ObjectCreationException {
+ Collection<Object> collection = null;
+ if (SortedSet.class.isAssignableFrom(collectionType)) {
+ collection = new TreeSet<Object>();
+ } else if (Set.class.isAssignableFrom(collectionType)) {
+ collection = new HashSet<Object>();
+ } else if (List.class.isAssignableFrom(collectionType)) {
+ collection = new ArrayList<Object>();
+ } else {
+ collection = new ArrayList<Object>();
}
- return list;
+ for (ObjectFactory<?> factory : factories) {
+ collection.add(factory.getInstance());
+ }
+ return collection;
}
}
diff --git a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java
index 1f1f1eefdc..e89b618cfe 100644
--- a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java
+++ b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java
@@ -151,14 +151,14 @@ public class JavaInstanceFactoryProvider<T> implements InstanceFactoryProvider<T
if (field.getType().isArray()) {
return new FieldInjector<T>(field, new ArrayMultiplicityObjectFactory(interfaceType, factories));
} else {
- return new FieldInjector<T>(field, new ListMultiplicityObjectFactory(factories));
+ return new FieldInjector<T>(field, new ListMultiplicityObjectFactory(factories, field.getType()));
}
} else if (element.getAnchor() instanceof Method) {
Method method = (Method)element.getAnchor();
if (method.getParameterTypes()[0].isArray()) {
return new MethodInjector<T>(method, new ArrayMultiplicityObjectFactory(interfaceType, factories));
} else {
- return new MethodInjector<T>(method, new ListMultiplicityObjectFactory(factories));
+ return new MethodInjector<T>(method, new ListMultiplicityObjectFactory(factories, method.getParameterTypes()[0]));
}
} else {
throw new InvalidAccessorException("Member must be a field or method: " + element.getName());