Author: jmarino
Date: Fri Oct  6 15:35:33 2006
New Revision: 453797

URL: http://svn.apache.org/viewvc?view=rev&rev=453797
Log:
support CDI where @Constructor without explict names in conjunction with 
@Autowire on parameters without explicit names

Modified:
    
incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceImpl.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/HeuristicConstructorTestCase.java

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=453797&r1=453796&r2=453797
==============================================================================
--- 
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
 Fri Oct  6 15:35:33 2006
@@ -18,15 +18,16 @@
  */
 package org.apache.tuscany.core.implementation.processor;
 
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getBaseName;
-
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Member;
 import java.util.List;
-
 import javax.xml.namespace.QName;
 
-import org.apache.tuscany.core.idl.java.IllegalCallbackException;
+import org.osoa.sca.annotations.Callback;
+import org.osoa.sca.annotations.Property;
+import org.osoa.sca.annotations.Reference;
+import org.osoa.sca.annotations.Remotable;
+
 import org.apache.tuscany.spi.annotation.Autowire;
 import org.apache.tuscany.spi.idl.InvalidServiceContractException;
 import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry;
@@ -40,14 +41,13 @@
 import org.apache.tuscany.spi.implementation.java.ProcessingException;
 import org.apache.tuscany.spi.model.OverrideOptions;
 import org.apache.tuscany.spi.model.ServiceContract;
-import org.osoa.sca.annotations.Callback;
-import org.osoa.sca.annotations.Property;
-import org.osoa.sca.annotations.Reference;
-import org.osoa.sca.annotations.Remotable;
+
+import org.apache.tuscany.core.idl.java.IllegalCallbackException;
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getBaseName;
 
 /**
  * The default implementation of an <code>ImplementationProcessorService</code>
- * 
+ *
  * @version $Rev$ $Date$
  */
 public class ImplementationProcessorServiceImpl implements 
ImplementationProcessorService {
@@ -74,7 +74,7 @@
             contract.setCallbackName(getBaseName(callbackClass));
         } else if (callback != null && Void.class.equals(callback.value())) {
             IllegalCallbackException e = new IllegalCallbackException(
-                    "Callback annotation must specify an interface on service 
type");
+                "Callback annotation must specify an interface on service 
type");
             e.setIdentifier(interfaze.getName());
             throw e;
         }
@@ -102,12 +102,12 @@
     }
 
     public boolean processParam(
-            Class<?> param,
-            Annotation[] paramAnnotations,
-            String[] constructorNames,
-            int pos,
-            PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type,
-            List<String> injectionNames) throws ProcessingException {
+        Class<?> param,
+        Annotation[] paramAnnotations,
+        String[] constructorNames,
+        int pos,
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type,
+        List<String> injectionNames) throws ProcessingException {
         boolean processed = false;
         for (Annotation annot : paramAnnotations) {
             if (Autowire.class.equals(annot.annotationType())) {
@@ -129,7 +129,7 @@
             for (Annotation annotation : annotations) {
                 Class<? extends Annotation> annotType = 
annotation.annotationType();
                 if (annotType.equals(Autowire.class) || 
annotType.equals(Property.class)
-                        || annotType.equals(Reference.class)) {
+                    || annotType.equals(Reference.class)) {
                     return true;
                 }
             }
@@ -139,9 +139,9 @@
 
     /**
      * Determines if all the members of a collection have unique types
-     * 
+     *
      * @param collection the collection to analyze
-     * @param start the position in the collection to start
+     * @param start      the position in the collection to start
      * @return true if the types are unique
      */
     private boolean areUnique(Class[] collection, int start) {
@@ -160,47 +160,56 @@
 
     /**
      * Processes autowire metadata for a constructor parameter
-     * 
-     * @param annot the autowire annotation
+     *
+     * @param annot            the autowire annotation
      * @param constructorNames the parameter names as specified in an [EMAIL 
PROTECTED] org.osoa.sca.annotations.Constructor}
-     *            annotation
-     * @param pos the position of the parameter in the constructor's parameter 
list
-     * @param param the parameter type
-     * @param type the component type associated with the implementation being 
processed
-     * @param injectionNames the collection of injection names to update
+     *                         annotation
+     * @param pos              the position of the parameter in the 
constructor's parameter list
+     * @param param            the parameter type
+     * @param type             the component type associated with the 
implementation being processed
+     * @param injectionNames   the collection of injection names to update
      * @throws InvalidAutowireException
      * @throws InvalidConstructorException
      */
     private void processAutowire(
-            Annotation annot,
-            String[] constructorNames,
-            int pos,
-            Class<?> param,
-            PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type,
-            List<String> injectionNames) throws InvalidAutowireException, 
InvalidConstructorException {
+        Annotation annot,
+        String[] constructorNames,
+        int pos,
+        Class<?> param,
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type,
+        List<String> injectionNames) throws InvalidAutowireException, 
InvalidConstructorException {
         // the param is marked as an autowire
         Autowire autowireAnnot = (Autowire) annot;
         JavaMappedReference reference = new JavaMappedReference();
         reference.setAutowire(true);
         String name = autowireAnnot.name();
         if (name == null || name.length() == 0) {
-            if (constructorNames.length > 0 && (constructorNames.length < pos 
+ 1 || constructorNames[pos] == null)) {
+            if (constructorNames.length > 1 && (constructorNames.length < pos 
+ 1 || constructorNames[pos] == null)) {
                 throw new InvalidAutowireException("Names in @Constructor and 
autowire parameter do not match at "
-                        + (pos + 1));
+                    + (pos + 1));
+            } else if (constructorNames.length == 1 && 
constructorNames[0].length() == 0) {
+                // special case when @Constructor present with all autowire 
params not specifying any name 
+                name = param.getName() + String.valueOf(pos);
+            } else if (constructorNames.length == 1
+                && (constructorNames.length < pos + 1 || constructorNames[pos] 
== null)) {
+                throw new InvalidAutowireException("Names in @Constructor and 
autowire parameter do not match at "
+                    + (pos + 1));
+            } else if (constructorNames.length == 1 && 
constructorNames[0].length() > 0) {
+                name = constructorNames[pos];
             } else if (constructorNames.length == 0 || 
constructorNames[pos].length() == 0) {
                 name = param.getName() + String.valueOf(pos);
             } else {
                 name = constructorNames[pos];
             }
         } else if (pos < constructorNames.length && constructorNames[pos] != 
null
-                && constructorNames[pos].length() != 0 && 
!name.equals(constructorNames[pos])) {
+            && constructorNames[pos].length() != 0 && 
!name.equals(constructorNames[pos])) {
             throw new InvalidConstructorException("Name specified by 
@Constructor does not match autowire name at "
-                    + (pos + 1));
+                + (pos + 1));
         }
         reference.setName(name);
-        
+
         reference.setRequired(autowireAnnot.required());
-        
+
         ServiceContract<?> contract = new JavaServiceContract();
         contract.setInterfaceClass(param);
         reference.setServiceContract(contract);
@@ -210,23 +219,23 @@
 
     /**
      * Processes parameter metadata for a constructor parameter
-     * 
-     * @param annot the parameter annotation
+     *
+     * @param annot            the parameter annotation
      * @param constructorNames the parameter names as specified in an [EMAIL 
PROTECTED] org.osoa.sca.annotations.Constructor}
-     *            annotation
-     * @param pos the position of the parameter in the constructor's parameter 
list
-     * @param type the component type associated with the implementation being 
processed
-     * @param param the parameter type
-     * @param explicitNames the collection of injection names to update
+     *                         annotation
+     * @param pos              the position of the parameter in the 
constructor's parameter list
+     * @param type             the component type associated with the 
implementation being processed
+     * @param param            the parameter type
+     * @param explicitNames    the collection of injection names to update
      * @throws ProcessingException
      */
     private <T> void processProperty(
-            Annotation annot,
-            String[] constructorNames,
-            int pos,
-            PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type,
-            Class<T> param,
-            List<String> explicitNames) throws ProcessingException {
+        Annotation annot,
+        String[] constructorNames,
+        int pos,
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type,
+        Class<T> param,
+        List<String> explicitNames) throws ProcessingException {
         // TODO multiplicity
         // the param is marked as a property
         Property propAnnot = (Property) annot;
@@ -234,21 +243,21 @@
         String name = propAnnot.name();
         if (name == null || name.length() == 0) {
             if (constructorNames.length < pos + 1 || constructorNames[pos] == 
null
-                    || constructorNames[pos].length() == 0) {
+                || constructorNames[pos].length() == 0) {
                 throw new InvalidPropertyException("No name specified for 
property parameter " + (pos + 1));
             }
             name = constructorNames[pos];
         } else if (pos < constructorNames.length && constructorNames[pos] != 
null
-                && constructorNames[pos].length() != 0 && 
!name.equals(constructorNames[pos])) {
+            && constructorNames[pos].length() != 0 && 
!name.equals(constructorNames[pos])) {
             throw new InvalidConstructorException("Name specified by 
@Constructor does not match property name at "
-                    + (pos + 1));
+                + (pos + 1));
         }
         if (type.getProperties().get(name) != null) {
             throw new DuplicatePropertyException(name);
         }
         property.setName(name);
         
property.setOverride(OverrideOptions.valueOf(propAnnot.override().toUpperCase()));
-        
+
         property.setXmlType(QName.valueOf(propAnnot.xmlType()));
         property.setJavaType(param);
         type.getProperties().put(name, property);
@@ -257,23 +266,23 @@
 
     /**
      * Processes reference metadata for a constructor parameter
-     * 
-     * @param annot the parameter annotation
+     *
+     * @param annot            the parameter annotation
      * @param constructorNames the parameter names as specified in an [EMAIL 
PROTECTED] org.osoa.sca.annotations.Constructor}
-     *            annotation
-     * @param pos the position of the parameter in the constructor's parameter 
list
-     * @param type the component type associated with the implementation being 
processed
-     * @param param the parameter type
-     * @param explicitNames the collection of injection names to update
+     *                         annotation
+     * @param pos              the position of the parameter in the 
constructor's parameter list
+     * @param type             the component type associated with the 
implementation being processed
+     * @param param            the parameter type
+     * @param explicitNames    the collection of injection names to update
      * @throws ProcessingException
      */
     private void processReference(
-            Annotation annot,
-            String[] constructorNames,
-            int pos,
-            PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type,
-            Class<?> param,
-            List<String> explicitNames) throws ProcessingException {
+        Annotation annot,
+        String[] constructorNames,
+        int pos,
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type,
+        Class<?> param,
+        List<String> explicitNames) throws ProcessingException {
 
         // TODO multiplicity
         // the param is marked as a reference
@@ -282,14 +291,14 @@
         String name = refAnnotation.name();
         if (name == null || name.length() == 0) {
             if (constructorNames.length < pos + 1 || constructorNames[pos] == 
null
-                    || constructorNames[pos].length() == 0) {
+                || constructorNames[pos].length() == 0) {
                 throw new InvalidReferenceException("No name specified for 
reference parameter " + (pos + 1));
             }
             name = constructorNames[pos];
         } else if (pos < constructorNames.length && constructorNames[pos] != 
null
-                && constructorNames[pos].length() != 0 && 
!name.equals(constructorNames[pos])) {
+            && constructorNames[pos].length() != 0 && 
!name.equals(constructorNames[pos])) {
             throw new InvalidConstructorException("Name specified by 
@Constructor does not match reference name at "
-                    + (pos + 1));
+                + (pos + 1));
         }
         if (type.getReferences().get(name) != null) {
             throw new DuplicateReferenceException(name);

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=453797&r1=453796&r2=453797
==============================================================================
--- 
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
 Fri Oct  6 15:35:33 2006
@@ -20,6 +20,10 @@
 
 import java.lang.reflect.Constructor;
 
+import org.osoa.sca.annotations.Property;
+import org.osoa.sca.annotations.Reference;
+
+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;
@@ -74,6 +78,27 @@
         }
     }
 
+    public void testMixedParameters() throws Exception {
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type =
+            new PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>>();
+        Constructor<Mixed> ctor1 = Mixed.class.getConstructor(String.class, 
String.class, String.class);
+        processor.visitConstructor(null, ctor1, type, null);
+        assertEquals("java.lang.String0", 
type.getConstructorDefinition().getInjectionNames().get(0));
+        assertEquals("foo", 
type.getConstructorDefinition().getInjectionNames().get(1));
+        assertEquals("bar", 
type.getConstructorDefinition().getInjectionNames().get(2));
+    }
+
+    public void testAllAutowireWithNoNames() throws Exception {
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type =
+            new PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>>();
+        Constructor<AllAutowireNoName> ctor1 =
+            AllAutowireNoName.class.getConstructor(String.class, String.class, 
String.class);
+        processor.visitConstructor(null, ctor1, type, null);
+        assertEquals("java.lang.String0", 
type.getConstructorDefinition().getInjectionNames().get(0));
+        assertEquals("java.lang.String1", 
type.getConstructorDefinition().getInjectionNames().get(1));
+        assertEquals("java.lang.String2", 
type.getConstructorDefinition().getInjectionNames().get(2));
+    }
+
     private static class BadFoo {
 
         @org.osoa.sca.annotations.Constructor("foo")
@@ -102,6 +127,21 @@
     private static class BadAnnotation {
         @org.osoa.sca.annotations.Constructor("foo")
         public BadAnnotation(String foo, Foo ref) {
+        }
+    }
+
+
+    public static final class Mixed {
+        @org.osoa.sca.annotations.Constructor
+        public Mixed(@Autowire String param1,
+                     @Property(name = "foo") String param2,
+                     @Reference(name = "bar") String param3) {
+        }
+    }
+
+    public static final class AllAutowireNoName {
+        @org.osoa.sca.annotations.Constructor
+        public AllAutowireNoName(@Autowire String param1, @Autowire String 
param2, @Autowire String param3) {
         }
     }
 

Modified: 
incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java?view=diff&rev=453797&r1=453796&r2=453797
==============================================================================
--- 
incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
 Fri Oct  6 15:35:33 2006
@@ -196,6 +196,16 @@
         assertNotNull(type.getReferences().get(String.class.getName() + "1"));
     }
 
+    public void testNoAutowireNameInConstructor() throws Exception {
+        PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type =
+            new PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>>();
+        processor.visitEnd(null, Foo16.class, type, null);
+        assertEquals(2, type.getReferences().size());
+        assertNotNull(type.getReferences().get(String.class.getName() + "0"));
+        assertNotNull(type.getReferences().get("bar"));
+        assertNotNull(type.getProperties().get("foo"));
+    }
+
     public void testPrivateConstructor() throws Exception {
         PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>> type =
             new PojoComponentType<JavaMappedService, JavaMappedReference, 
JavaMappedProperty<?>>();
@@ -307,6 +317,13 @@
 
     public static final class Foo15 {
         public Foo15(@Autowire String param1, @Autowire String param2) {
+        }
+    }
+
+    public static final class Foo16 {
+        public Foo16(@Autowire String param1,
+                     @Property(name = "foo") String param2,
+                     @Reference(name = "bar") String param3) {
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to