You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2007/01/25 01:10:32 UTC

svn commit: r499624 - in /incubator/tuscany/java/sca/kernel: core/src/main/java/org/apache/tuscany/core/implementation/processor/ core/src/test/java/org/apache/tuscany/core/implementation/processor/ spi/src/main/java/org/apache/tuscany/spi/implementati...

Author: rfeng
Date: Wed Jan 24 16:10:30 2007
New Revision: 499624

URL: http://svn.apache.org/viewvc?view=rev&rev=499624
Log:
Add the introspection support for multiplicity of references and many-value of properties

Modified:
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceImpl.java
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java
    incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java
    incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceTestCase.java
    incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/PropertyProcessorTestCase.java
    incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessorTestCase.java
    incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/AbstractPropertyProcessor.java
    incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/ImplementationProcessorExtension.java
    incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/ImplementationProcessorService.java

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java Wed Jan 24 16:10:30 2007
@@ -89,7 +89,13 @@
             Class<?> param = params[i];
             Annotation[] paramAnnotations = annotations[i];
             try {
-                if (!service.processParam(param, paramAnnotations, names, i, type, injectionNames)) {
+                if (!service.processParam(param,
+                                          constructor.getGenericParameterTypes()[i],
+                                          paramAnnotations,
+                                          names,
+                                          i,
+                                          type,
+                                          injectionNames)) {
                     String name = (i < names.length) ? names[i] : "";
                     service.addName(injectionNames, i, name);
                 }

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java Wed Jan 24 16:10:30 2007
@@ -249,7 +249,13 @@
         if (explict) {
             for (int i = 0; i < params.length; i++) {
                 Class param = params[i];
-                implService.processParam(param, annotations[i], new String[0], i, type, paramNames);
+                implService.processParam(param,
+                                         constructor.getGenericParameterTypes()[i],
+                                         annotations[i],
+                                         new String[0],
+                                         i,
+                                         type,
+                                         paramNames);
             }
         } else {
             if (!implService.areUnique(params)) {

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceImpl.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceImpl.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceImpl.java Wed Jan 24 16:10:30 2007
@@ -20,6 +20,9 @@
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Member;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.Collection;
 import java.util.List;
 import javax.xml.namespace.QName;
 
@@ -41,6 +44,7 @@
 import org.apache.tuscany.spi.implementation.java.JavaMappedService;
 import org.apache.tuscany.spi.implementation.java.PojoComponentType;
 import org.apache.tuscany.spi.implementation.java.ProcessingException;
+import org.apache.tuscany.spi.model.Multiplicity;
 import org.apache.tuscany.spi.model.OverrideOptions;
 import org.apache.tuscany.spi.model.ServiceContract;
 
@@ -103,22 +107,22 @@
 
     public boolean processParam(
         Class<?> param,
+        Type genericParam,
         Annotation[] paramAnnotations,
         String[] constructorNames,
         int pos,
-        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
-        List<String> injectionNames) throws ProcessingException {
+        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, List<String> injectionNames) throws ProcessingException {
         boolean processed = false;
         for (Annotation annot : paramAnnotations) {
             if (Autowire.class.equals(annot.annotationType())) {
                 processed = true;
-                processAutowire(annot, constructorNames, pos, param, type, injectionNames);
+                processAutowire(annot, constructorNames, pos, param, genericParam, type, injectionNames);
             } else if (Property.class.equals(annot.annotationType())) {
                 processed = true;
-                processProperty(annot, constructorNames, pos, type, param, injectionNames);
+                processProperty(annot, constructorNames, pos, type, param, genericParam, injectionNames);
             } else if (Reference.class.equals(annot.annotationType())) {
                 processed = true;
-                processReference(annot, constructorNames, pos, type, param, injectionNames);
+                processReference(annot, constructorNames, pos, type, param, genericParam, injectionNames);
             } else if (Resource.class.equals(annot.annotationType())) {
                 processed = true;
                 processResource((Resource) annot, constructorNames, pos, type, param, injectionNames);
@@ -201,6 +205,7 @@
         String[] constructorNames,
         int pos,
         Class<?> param,
+        Type genericParam,
         PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
         List<String> injectionNames) throws ProcessingException {
         // the param is marked as an autowire
@@ -233,17 +238,29 @@
                 paramNum);
         }
         reference.setName(name);
-        reference.setRequired(autowireAnnot.required());
-        ServiceContract<?> contract = null;
+        boolean required = autowireAnnot.required();
+        reference.setRequired(required);
         try {
-            contract = registry.introspect(param);
+            Class<?> rawType = param;
+            if (rawType.isArray() || Collection.class.isAssignableFrom(rawType)) {
+                if (required) {
+                    reference.setMultiplicity(Multiplicity.ONE_N);
+                } else {
+                    reference.setMultiplicity(Multiplicity.ZERO_N);
+                }
+            } else {
+                if (required) {
+                    reference.setMultiplicity(Multiplicity.ONE_ONE);
+                } else {
+                    reference.setMultiplicity(Multiplicity.ZERO_ONE);
+                }
+            }
+            Class<?> baseType = getBaseType(rawType, genericParam);            
+            ServiceContract<?> contract = registry.introspect(baseType);
+            reference.setServiceContract(contract);
         } catch (InvalidServiceContractException e) {
             throw new ProcessingException(e);
         }
-
-//        ServiceContract<?> contract = new JavaServiceContract();
-        contract.setInterfaceClass(param);
-        reference.setServiceContract(contract);
         type.getReferences().put(name, reference);
         addName(injectionNames, pos, name);
     }
@@ -260,18 +277,23 @@
      * @param explicitNames    the collection of injection names to update
      * @throws ProcessingException
      */
-    private <T> void processProperty(
+    @SuppressWarnings("unchecked")
+    private void processProperty(
         Annotation annot,
         String[] constructorNames,
         int pos,
         PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
-        Class<T> param,
+        Class<?> param,
+        Type genericParam,
         List<String> explicitNames) throws ProcessingException {
-        // TODO multiplicity
         // the param is marked as a property
         Property propAnnot = (Property) annot;
-        JavaMappedProperty<T> property = new JavaMappedProperty<T>();
-        property.setJavaType(param);
+        JavaMappedProperty property = new JavaMappedProperty();
+        Class<?> baseType = getBaseType(param, genericParam);
+        if (param.isArray() || Collection.class.isAssignableFrom(param)) {
+            property.setMany(true);
+        }        
+        property.setJavaType(baseType);
         String name = propAnnot.name();
         if (name == null || name.length() == 0) {
             if (constructorNames.length < pos + 1 || constructorNames[pos] == null
@@ -300,7 +322,6 @@
                 property.setXmlType(typeInfo.getQName());
             }
         }
-        property.setJavaType(param);
         type.getProperties().put(name, property);
         addName(explicitNames, pos, name);
     }
@@ -323,6 +344,7 @@
         int pos,
         PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
         Class<?> param,
+        Type genericParam,
         List<String> explicitNames) throws ProcessingException {
 
         // TODO multiplicity
@@ -346,9 +368,25 @@
             throw new DuplicateReferenceException(name);
         }
         reference.setName(name);
-        reference.setRequired(refAnnotation.required());
+        boolean required = refAnnotation.required();
+        reference.setRequired(required);
         try {
-            ServiceContract<?> contract = registry.introspect(param);
+            Class<?> rawType = param;
+            if (rawType.isArray() || Collection.class.isAssignableFrom(rawType)) {
+                if (required) {
+                    reference.setMultiplicity(Multiplicity.ONE_N);
+                } else {
+                    reference.setMultiplicity(Multiplicity.ZERO_N);
+                }
+            } else {
+                if (required) {
+                    reference.setMultiplicity(Multiplicity.ONE_ONE);
+                } else {
+                    reference.setMultiplicity(Multiplicity.ZERO_ONE);
+                }
+            }
+            Class<?> baseType = getBaseType(rawType, genericParam);            
+            ServiceContract<?> contract = registry.introspect(baseType);
             reference.setServiceContract(contract);
         } catch (InvalidServiceContractException e) {
             throw new ProcessingException(e);
@@ -407,4 +445,25 @@
         addName(explicitNames, pos, name);
     }
 
+    protected static Class<?> getBaseType(Class<?> cls, Type genericType) {
+        if (cls.isArray()) {
+            return cls.getComponentType();
+        } else if (Collection.class.isAssignableFrom(cls)) {
+            if (genericType == cls) {
+                return Object.class;
+            } else {
+                ParameterizedType parameterizedType = (ParameterizedType)genericType;
+                Type baseType = parameterizedType.getActualTypeArguments()[0];
+                if (baseType instanceof Class) {
+                    return (Class<?>)baseType;
+                } else if (baseType instanceof ParameterizedType) {
+                    return (Class<?>)((ParameterizedType)baseType).getRawType();
+                } else {
+                    return null;
+                }
+            }
+        } else {
+            return cls;
+        }
+    }        
 }

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java Wed Jan 24 16:10:30 2007
@@ -18,11 +18,12 @@
  */
 package org.apache.tuscany.core.implementation.processor;
 
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
+
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
-
-import org.osoa.sca.annotations.Reference;
+import java.util.Collection;
 
 import org.apache.tuscany.spi.annotation.Autowire;
 import org.apache.tuscany.spi.component.CompositeComponent;
@@ -35,38 +36,41 @@
 import org.apache.tuscany.spi.implementation.java.JavaMappedService;
 import org.apache.tuscany.spi.implementation.java.PojoComponentType;
 import org.apache.tuscany.spi.implementation.java.ProcessingException;
+import org.apache.tuscany.spi.model.Multiplicity;
 import org.apache.tuscany.spi.model.ServiceContract;
-
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
+import org.osoa.sca.annotations.Reference;
 
 /**
- * Processes an {@link @Reference} annotation, updating the component type with corresponding {@link
+ * Processes an {@link @Reference} annotation, updating the component type with
+ * corresponding {@link
  * org.apache.tuscany.spi.implementation.java.JavaMappedReference}
- *
+ * 
  * @version $Rev$ $Date$
  */
 public class ReferenceProcessor extends ImplementationProcessorExtension {
 
     private JavaInterfaceProcessorRegistry regsitry;
 
-    public ReferenceProcessor(@Autowire JavaInterfaceProcessorRegistry registry) {
+    public ReferenceProcessor(@Autowire
+    JavaInterfaceProcessorRegistry registry) {
         this.regsitry = registry;
     }
 
-    public void visitMethod(CompositeComponent parent, Method method,
+    public void visitMethod(CompositeComponent parent,
+                            Method method,
                             PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
-                            DeploymentContext context)
-        throws ProcessingException {
+                            DeploymentContext context) throws ProcessingException {
         Reference annotation = method.getAnnotation(Reference.class);
         Autowire autowire = method.getAnnotation(Autowire.class);
         boolean isAutowire = autowire != null;
         if (annotation == null && !isAutowire) {
-            return; //Not a reference or autowire annotation.
+            return; // Not a reference or autowire annotation.
         }
         if (method.getParameterTypes().length != 1) {
             throw new IllegalReferenceException("Setter must have one parameter", method.toString());
         }
-        //process autowire required first let reference override. or if conflicting should this fault?
+        // process autowire required first let reference override. or if
+        // conflicting should this fault?
         boolean required = false;
         if (isAutowire) {
             required = autowire.required();
@@ -94,7 +98,22 @@
         reference.setName(name);
         ServiceContract contract;
         try {
-            contract = regsitry.introspect(method.getParameterTypes()[0]);
+            Class<?> rawType = method.getParameterTypes()[0];
+            if (rawType.isArray() || Collection.class.isAssignableFrom(rawType)) {
+                if (required) {
+                    reference.setMultiplicity(Multiplicity.ONE_N);
+                } else {
+                    reference.setMultiplicity(Multiplicity.ZERO_N);
+                }
+            } else {
+                if (required) {
+                    reference.setMultiplicity(Multiplicity.ONE_ONE);
+                } else {
+                    reference.setMultiplicity(Multiplicity.ZERO_ONE);
+                }
+            }
+            Class<?> baseType = getBaseType(rawType, method.getGenericParameterTypes()[0]);
+            contract = regsitry.introspect(baseType);
         } catch (InvalidServiceContractException e) {
             throw new ProcessingException(e);
         }
@@ -102,7 +121,8 @@
         type.getReferences().put(name, reference);
     }
 
-    public void visitField(CompositeComponent parent, Field field,
+    public void visitField(CompositeComponent parent,
+                           Field field,
                            PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
                            DeploymentContext context) throws ProcessingException {
         Reference annotation = field.getAnnotation(Reference.class);
@@ -131,7 +151,22 @@
         reference.setName(name);
         ServiceContract contract;
         try {
-            contract = regsitry.introspect(field.getType());
+            Class<?> rawType = field.getType();
+            if (rawType.isArray() || Collection.class.isAssignableFrom(rawType)) {
+                if (required) {
+                    reference.setMultiplicity(Multiplicity.ONE_N);
+                } else {
+                    reference.setMultiplicity(Multiplicity.ZERO_N);
+                }
+            } else {
+                if (required) {
+                    reference.setMultiplicity(Multiplicity.ONE_ONE);
+                } else {
+                    reference.setMultiplicity(Multiplicity.ZERO_ONE);
+                }
+            }
+            Class<?> baseType = getBaseType(rawType, field.getGenericType());
+            contract = regsitry.introspect(baseType);
         } catch (InvalidServiceContractException e) {
             throw new ProcessingException(e);
         }
@@ -139,12 +174,10 @@
         type.getReferences().put(name, reference);
     }
 
-    public <T> void visitConstructor(CompositeComponent parent, Constructor<T> constructor,
-                                     PojoComponentType<JavaMappedService, JavaMappedReference,
-                                         JavaMappedProperty<?>> type,
+    public <T> void visitConstructor(CompositeComponent parent,
+                                     Constructor<T> constructor,
+                                     PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
                                      DeploymentContext context) throws ProcessingException {
 
     }
-
-
 }

Modified: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java Wed Jan 24 16:10:30 2007
@@ -19,18 +19,21 @@
 package org.apache.tuscany.core.implementation.processor;
 
 import java.lang.reflect.Constructor;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
 
-import org.osoa.sca.annotations.Property;
-import org.osoa.sca.annotations.Reference;
+import junit.framework.TestCase;
 
+import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl;
 import org.apache.tuscany.spi.annotation.Autowire;
 import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
 import org.apache.tuscany.spi.implementation.java.JavaMappedReference;
 import org.apache.tuscany.spi.implementation.java.JavaMappedService;
 import org.apache.tuscany.spi.implementation.java.PojoComponentType;
-
-import junit.framework.TestCase;
-import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl;
+import org.apache.tuscany.spi.model.Multiplicity;
+import org.osoa.sca.annotations.Property;
+import org.osoa.sca.annotations.Reference;
 
 /**
  * @version $Rev$ $Date$
@@ -144,5 +147,38 @@
         public AllAutowireNoName(@Autowire String param1, @Autowire String param2, @Autowire String param3) {
         }
     }
+    
+    public static final class Multiple {
+        @org.osoa.sca.annotations.Constructor
+        public Multiple(@Autowire Collection<String> param1,
+                     @Property(name = "foo") String[] param2,
+                     @Reference(name = "bar", required=true) List<String> param3, 
+                     @Property(name = "abc") Set<String> param4,
+                     @Reference(name = "xyz") String[] param5) {
+        }
+    } 
+    
+    public void testMultiplicity() throws Exception {
+        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
+            new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
+        Constructor<Multiple> ctor1 =
+            Multiple.class.getConstructor(Collection.class, String[].class, List.class, Set.class, String[].class);
+        processor.visitConstructor(null, ctor1, type, null);
+        JavaMappedReference ref0 = type.getReferences().get("java.util.Collection0");
+        assertNotNull(ref0);
+        assertEquals(Multiplicity.ONE_N, ref0.getMultiplicity());
+        JavaMappedReference ref1 = type.getReferences().get("bar");
+        assertNotNull(ref1);
+        assertEquals(Multiplicity.ONE_N, ref1.getMultiplicity());
+        JavaMappedReference ref2 = type.getReferences().get("xyz");
+        assertNotNull(ref2);
+        assertEquals(Multiplicity.ZERO_N, ref2.getMultiplicity());
+        JavaMappedProperty prop1 = type.getProperties().get("foo");
+        assertNotNull(prop1);
+        assertTrue(prop1.isMany());
+        JavaMappedProperty prop2 = type.getProperties().get("abc");
+        assertNotNull(prop2);
+        assertTrue(prop2.isMany());
+    }    
 
 }

Modified: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceTestCase.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceTestCase.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceTestCase.java Wed Jan 24 16:10:30 2007
@@ -72,7 +72,13 @@
         Annotation[] paramAnnotations = ctor.getParameterAnnotations()[0];
         List<String> injectionNames = new ArrayList<String>();
         String[] names = new String[]{"foo"};
-        implService.processParam(int.class, paramAnnotations, names, 0, type, injectionNames);
+        implService.processParam(int.class,
+                                 ctor.getGenericParameterTypes()[0],
+                                 paramAnnotations,
+                                 names,
+                                 0,
+                                 type,
+                                 injectionNames);
         org.apache.tuscany.spi.model.Property<?> property = type.getProperties().get("foo");
         assertEquals(int.class, property.getJavaType());
         assertEquals(SimpleTypeMapperExtension.XSD_INT, property.getXmlType());

Modified: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/PropertyProcessorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/PropertyProcessorTestCase.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/PropertyProcessorTestCase.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/PropertyProcessorTestCase.java Wed Jan 24 16:10:30 2007
@@ -19,6 +19,10 @@
 package org.apache.tuscany.core.implementation.processor;
 
 import static org.apache.tuscany.spi.model.OverrideOptions.MUST;
+
+import java.util.Collection;
+import java.util.List;
+
 import junit.framework.TestCase;
 
 import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl;
@@ -153,4 +157,54 @@
         }
 
     }
+    
+    private class Multiple {
+        @Property
+        protected List<String> refs1;
+
+        @Property
+        protected String[] refs2;
+
+        @Property
+        public void setRefs3(String[] refs) {
+        }
+
+        @Property
+        public void setRefs4(Collection<String> refs) {
+        }
+
+    }
+
+    public void testMultiplicity_Collection() throws Exception {
+        processor.visitField(null, Multiple.class.getDeclaredField("refs1"), type, null);
+        JavaMappedProperty prop = type.getProperties().get("refs1");
+        assertNotNull(prop);
+        assertSame(String.class, prop.getJavaType());
+        assertTrue(prop.isMany());
+    }
+
+    public void testMultiplicity_Array() throws Exception {
+        processor.visitField(null, Multiple.class.getDeclaredField("refs2"), type, null);
+        JavaMappedProperty prop = type.getProperties().get("refs2");
+        assertNotNull(prop);
+        assertSame(String.class, prop.getJavaType());
+        assertTrue(prop.isMany());
+    }
+
+    public void testMultiplicity_Array_Method() throws Exception {
+        processor.visitMethod(null, Multiple.class.getMethod("setRefs3", new Class[] {String[].class}), type, null);
+        JavaMappedProperty prop = type.getProperties().get("refs3");
+        assertNotNull(prop);
+        assertSame(String.class, prop.getJavaType());
+        assertTrue(prop.isMany());
+    }
+
+    public void testMultiplicity_Collection_Method() throws Exception {
+        processor.visitMethod(null, Multiple.class.getMethod("setRefs4", new Class[] {Collection.class}), type, null);
+        JavaMappedProperty prop = type.getProperties().get("refs4");
+        assertNotNull(prop);
+        assertSame(String.class, prop.getJavaType());
+        assertTrue(prop.isMany());
+    }
+    
 }

Modified: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessorTestCase.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessorTestCase.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessorTestCase.java Wed Jan 24 16:10:30 2007
@@ -18,12 +18,16 @@
  */
 package org.apache.tuscany.core.implementation.processor;
 
+import java.util.Collection;
+import java.util.List;
+
 import org.osoa.sca.annotations.Reference;
 
 import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
 import org.apache.tuscany.spi.implementation.java.JavaMappedReference;
 import org.apache.tuscany.spi.implementation.java.JavaMappedService;
 import org.apache.tuscany.spi.implementation.java.PojoComponentType;
+import org.apache.tuscany.spi.model.Multiplicity;
 import org.apache.tuscany.spi.model.ServiceContract;
 
 import junit.framework.TestCase;
@@ -36,8 +40,7 @@
 
     PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
         new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
-    ReferenceProcessor processor =
-        new ReferenceProcessor(new JavaInterfaceProcessorRegistryImpl());
+    ReferenceProcessor processor = new ReferenceProcessor(new JavaInterfaceProcessorRegistryImpl());
 
     public void testMethodAnnotation() throws Exception {
         processor.visitMethod(null, ReferenceProcessorTestCase.Foo.class.getMethod("setFoo", Ref.class), type, null);
@@ -49,16 +52,20 @@
     }
 
     public void testMethodRequired() throws Exception {
-        processor
-            .visitMethod(null, ReferenceProcessorTestCase.Foo.class.getMethod("setFooRequired", Ref.class), type, null);
+        processor.visitMethod(null,
+                              ReferenceProcessorTestCase.Foo.class.getMethod("setFooRequired", Ref.class),
+                              type,
+                              null);
         JavaMappedReference prop = type.getReferences().get("fooRequired");
         assertNotNull(prop);
         assertTrue(prop.isRequired());
     }
 
     public void testMethodName() throws Exception {
-        processor
-            .visitMethod(null, ReferenceProcessorTestCase.Foo.class.getMethod("setBarMethod", Ref.class), type, null);
+        processor.visitMethod(null,
+                              ReferenceProcessorTestCase.Foo.class.getMethod("setBarMethod", Ref.class),
+                              type,
+                              null);
         assertNotNull(type.getReferences().get("bar"));
     }
 
@@ -89,19 +96,20 @@
             processor.visitField(null, ReferenceProcessorTestCase.Bar.class.getDeclaredField("baz"), type, null);
             fail();
         } catch (DuplicateReferenceException e) {
-            //expected
+            // expected
         }
     }
 
     public void testDuplicateMethods() throws Exception {
         processor.visitMethod(null, ReferenceProcessorTestCase.Bar.class.getMethod("dupMethod", Ref.class), type, null);
         try {
-            processor
-                .visitMethod(null, ReferenceProcessorTestCase.Bar.class.getMethod("dupSomeMethod", Ref.class), type,
-                    null);
+            processor.visitMethod(null,
+                                  ReferenceProcessorTestCase.Bar.class.getMethod("dupSomeMethod", Ref.class),
+                                  type,
+                                  null);
             fail();
         } catch (DuplicateReferenceException e) {
-            //expected
+            // expected
         }
     }
 
@@ -110,7 +118,7 @@
             processor.visitMethod(null, ReferenceProcessorTestCase.Bar.class.getMethod("badMethod"), type, null);
             fail();
         } catch (IllegalReferenceException e) {
-            //expected
+            // expected
         }
     }
 
@@ -132,7 +140,6 @@
         @Reference(name = "theBaz")
         protected Ref bazField;
 
-
         @Reference
         public void setFoo(Ref ref) {
         }
@@ -147,7 +154,6 @@
 
     }
 
-
     private class Bar {
 
         @Reference
@@ -168,6 +174,59 @@
         public void badMethod() {
         }
 
+    }
+
+    private class Multiple {
+        @Reference(required = true)
+        protected List<Ref> refs1;
+
+        @Reference(required = false)
+        protected Ref[] refs2;
+
+        @Reference(required = true)
+        public void setRefs3(Ref[] refs) {
+        }
+
+        @Reference(required = false)
+        public void setRefs4(Collection<Ref> refs) {
+        }
 
     }
+
+    public void testMultiplicity_1_N() throws Exception {
+        processor.visitField(null, Multiple.class.getDeclaredField("refs1"), type, null);
+        JavaMappedReference prop = type.getReferences().get("refs1");
+        assertNotNull(prop);
+        assertSame(Ref.class, prop.getServiceContract().getInterfaceClass());
+        assertEquals(Multiplicity.ONE_N, prop.getMultiplicity());
+        assertTrue(prop.isRequired());
+    }
+
+    public void testMultiplicity_0_N() throws Exception {
+        processor.visitField(null, Multiple.class.getDeclaredField("refs2"), type, null);
+        JavaMappedReference prop = type.getReferences().get("refs2");
+        assertNotNull(prop);
+        assertSame(Ref.class, prop.getServiceContract().getInterfaceClass());
+        assertEquals(Multiplicity.ZERO_N, prop.getMultiplicity());
+        assertFalse(prop.isRequired());
+    }
+
+    public void testMultiplicity_1_N_Method() throws Exception {
+        processor.visitMethod(null, Multiple.class.getMethod("setRefs3", new Class[] {Ref[].class}), type, null);
+        JavaMappedReference prop = type.getReferences().get("refs3");
+        assertNotNull(prop);
+        assertSame(Ref.class, prop.getServiceContract().getInterfaceClass());
+        assertEquals(Multiplicity.ONE_N, prop.getMultiplicity());
+        assertTrue(prop.isRequired());
+    }
+
+    public void testMultiplicity_0_N_Method() throws Exception {
+        processor.visitMethod(null, Multiple.class.getMethod("setRefs4", new Class[] {Collection.class}), type, null);
+        JavaMappedReference prop = type.getReferences().get("refs4");
+        assertNotNull(prop);
+        assertSame(Ref.class, prop.getServiceContract().getInterfaceClass());
+        assertEquals(Multiplicity.ZERO_N, prop.getMultiplicity());
+        assertFalse(prop.isRequired());
+    }
+
 }

Modified: incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/AbstractPropertyProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/AbstractPropertyProcessor.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/AbstractPropertyProcessor.java (original)
+++ incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/AbstractPropertyProcessor.java Wed Jan 24 16:10:30 2007
@@ -23,6 +23,7 @@
 import java.lang.reflect.Field;
 import java.lang.reflect.Member;
 import java.lang.reflect.Method;
+import java.util.Collection;
 import java.util.Map;
 
 import org.apache.tuscany.spi.component.CompositeComponent;
@@ -73,7 +74,12 @@
             throw new DuplicatePropertyException(name);
         }
 
-        JavaMappedProperty<?> property = createProperty(name, javaType, method);
+        Class<?> baseType = getBaseType(javaType, method.getGenericParameterTypes()[0]);
+        JavaMappedProperty<?> property = createProperty(name, baseType, method);
+        if (javaType.isArray() || Collection.class.isAssignableFrom(javaType)) {
+            property.setMany(true);
+        }
+        
         initProperty(property, annotation, parent, context);
         properties.put(name, property);
     }
@@ -99,7 +105,11 @@
             throw new DuplicatePropertyException(name);
         }
 
-        JavaMappedProperty<?> property = createProperty(name, javaType, field);
+        Class<?> baseType = getBaseType(javaType, field.getGenericType());
+        JavaMappedProperty<?> property = createProperty(name, baseType, field);
+        if (javaType.isArray() || Collection.class.isAssignableFrom(javaType)) {
+            property.setMany(true);
+        }        
         initProperty(property, annotation, parent, context);
         properties.put(name, property);
     }
@@ -127,7 +137,12 @@
                     if (name == null || name.length() == 0) {
                         name = param.getName();
                     }
-                    JavaMappedProperty<?> property = createProperty(name, param, constructor);
+                    
+                    Class<?> baseType = getBaseType(param, constructor.getGenericParameterTypes()[i]);
+                    JavaMappedProperty<?> property = createProperty(name, baseType, constructor);
+                    if (param.isArray() || Collection.class.isAssignableFrom(param)) {
+                        property.setMany(true);
+                    }
                     initProperty(property, monitorAnnot, parent, context);
                     properties.put(name, property);
                     service.addName(definition.getInjectionNames(), i, name);
@@ -156,4 +171,5 @@
         }
         return Character.toLowerCase(name.charAt(3)) + name.substring(4);
     }
+    
 }

Modified: incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/ImplementationProcessorExtension.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/ImplementationProcessorExtension.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/ImplementationProcessorExtension.java (original)
+++ incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/ImplementationProcessorExtension.java Wed Jan 24 16:10:30 2007
@@ -21,6 +21,9 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.Collection;
 
 import org.osoa.sca.annotations.Destroy;
 import org.osoa.sca.annotations.EagerInit;
@@ -90,4 +93,26 @@
                              DeploymentContext context) throws ProcessingException {
 
     }
+    
+    protected static Class<?> getBaseType(Class<?> cls, Type genericType) {
+        if (cls.isArray()) {
+            return cls.getComponentType();
+        } else if (Collection.class.isAssignableFrom(cls)) {
+            if (genericType == cls) {
+                return Object.class;
+            } else {
+                ParameterizedType parameterizedType = (ParameterizedType)genericType;
+                Type baseType = parameterizedType.getActualTypeArguments()[0];
+                if (baseType instanceof Class) {
+                    return (Class<?>)baseType;
+                } else if (baseType instanceof ParameterizedType) {
+                    return (Class<?>)((ParameterizedType)baseType).getRawType();
+                } else {
+                    return null;
+                }
+            }
+        } else {
+            return cls;
+        }
+    }    
 }

Modified: incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/ImplementationProcessorService.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/ImplementationProcessorService.java?view=diff&rev=499624&r1=499623&r2=499624
==============================================================================
--- incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/ImplementationProcessorService.java (original)
+++ incubator/tuscany/java/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/implementation/java/ImplementationProcessorService.java Wed Jan 24 16:10:30 2007
@@ -20,6 +20,7 @@
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Member;
+import java.lang.reflect.Type;
 import java.util.List;
 
 import org.apache.tuscany.spi.idl.InvalidServiceContractException;
@@ -66,6 +67,7 @@
      * Processes a constructor parameter by introspecting its annotations
      *
      * @param param            the parameter to process
+     * @param genericParam     the generic type of the parameter
      * @param paramAnnotations the parameter annotations
      * @param constructorNames the array of constructorNames specified by
      * @param pos              the declaration position of the constructor parameter
@@ -73,13 +75,13 @@
      * @param injectionNames   the list of parameter constructorNames specified on parameter annotations
      * @throws ProcessingException
      */
-    boolean processParam(
-        Class<?> param,
-        Annotation[] paramAnnotations,
-        String[] constructorNames,
-        int pos,
-        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
-        List<String> injectionNames) throws ProcessingException;
+    boolean processParam(Class<?> param,
+                         Type genericParam,
+                         Annotation[] paramAnnotations,
+                         String[] constructorNames,
+                         int pos,
+                         PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+                         List<String> injectionNames) throws ProcessingException;
 
     /**
      * Returns true if {@link @Autowire}, {@link @Property}, or {@link @Reference} are present in the given array



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org