You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jm...@apache.org on 2006/07/16 04:18:49 UTC

svn commit: r422337 - in /incubator/tuscany/sandbox/chianti/sca/core/src: main/java/org/apache/tuscany/core/implementation/processor/ test/java/org/apache/tuscany/core/implementation/processor/

Author: jmarino
Date: Sat Jul 15 19:18:48 2006
New Revision: 422337

URL: http://svn.apache.org/viewvc?rev=422337&view=rev
Log:
added support for heuristic constructor selection based on @Property, @Reference, and @Autowire annotations

Modified:
    incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
    incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
    incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ProcessorUtils.java
    incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java

Modified: incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java?rev=422337&r1=422336&r2=422337&view=diff
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java (original)
+++ incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java Sat Jul 15 19:18:48 2006
@@ -18,10 +18,6 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import org.osoa.sca.annotations.Property;
-import org.osoa.sca.annotations.Reference;
-
-import org.apache.tuscany.spi.annotation.Autowire;
 import org.apache.tuscany.spi.component.CompositeComponent;
 import org.apache.tuscany.spi.deployer.DeploymentContext;
 
@@ -30,7 +26,6 @@
 import org.apache.tuscany.core.implementation.JavaMappedProperty;
 import org.apache.tuscany.core.implementation.JavaMappedReference;
 import org.apache.tuscany.core.implementation.JavaMappedService;
-import org.apache.tuscany.core.implementation.JavaServiceContract;
 import org.apache.tuscany.core.implementation.PojoComponentType;
 import org.apache.tuscany.core.implementation.ProcessingException;
 
@@ -67,7 +62,7 @@
             Class<?> param = params[i];
             Annotation[] paramAnnotations = annotations[i];
             try {
-                if (processParam(param, paramAnnotations, names, i, type, explicitNames)) {
+                if (ProcessorUtils.processParam(param, paramAnnotations, names, i, type, explicitNames)) {
                     annotationsDeclared = true;
                 }
             } catch (ProcessingException e) {
@@ -90,113 +85,4 @@
         type.setConstructorDefinition(definition);
     }
 
-    /**
-     * Processes a constructor parameter by introspecting its annotations
-     *
-     * @param param            the parameter to process
-     * @param paramAnnotations the parameter annotations
-     * @param constructorNames the array of constructorNames specified by @Constructor
-     * @param pos              the declaration position of the constructor parameter
-     * @param type             the component type associated with implementation being reflected
-     * @param explicitNames    the list of parameter constructorNames specified on parameter annotations
-     * @return true if the parameters have explicit annotation declarations
-     * @throws ProcessingException
-     */
-    private boolean processParam(Class<?> param,
-                                 Annotation[] paramAnnotations,
-                                 String[] constructorNames,
-                                 int pos,
-                                 PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
-                                 List<String> explicitNames)
-        throws ProcessingException {
-        boolean annotationsDeclared = false;
-        for (Annotation annot : paramAnnotations) {
-            if (Autowire.class.equals(annot.annotationType())) {
-                // 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 < pos + 1 || constructorNames[pos] == null
-                        || constructorNames[pos].length() == 0) {
-                        throw new InvalidAutowireException("No name specified for autowire parameter " + (pos + 1));
-                    }
-                    name = constructorNames[pos];
-                } else if (pos < constructorNames.length
-                    && constructorNames[pos] != null
-                    && constructorNames[pos].length() != 0
-                    && !name.equals(constructorNames[pos])) {
-                    throw new InvalidConstructorException(
-                        "Name specified by @Constructor does not match autowire name at " + (pos + 1));
-                }
-                reference.setName(name);
-                JavaServiceContract contract = new JavaServiceContract();
-                contract.setInterfaceClass(param);
-                reference.setServiceContract(contract);
-                type.getReferences().put(name, reference);
-                annotationsDeclared = true;
-                explicitNames.add(name);
-            } else if (Property.class.equals(annot.annotationType())) {
-                // TODO multiplicity
-                // the param is marked as a property
-                Property propAnnot = (Property) annot;
-                JavaMappedProperty property = new JavaMappedProperty();
-                String name = propAnnot.name();
-                if (name == null || name.length() == 0) {
-                    if (constructorNames.length < pos + 1 || constructorNames[pos] == null
-                        || 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])) {
-                    throw new InvalidConstructorException(
-                        "Name specified by @Constructor does not match property name at " + (pos + 1));
-                }
-                if (type.getProperties().get(name) != null) {
-                    throw new DuplicatePropertyException(name);
-                }
-                property.setName(name);
-                property.setRequired(propAnnot.required());
-                property.setJavaType(param);
-                type.getProperties().put(name, property);
-                annotationsDeclared = true;
-                explicitNames.add(name);
-            } else if (Reference.class.equals(annot.annotationType())) {
-                // TODO multiplicity
-                // the param is marked as a reference
-                Reference refAnnotation = (Reference) annot;
-                JavaMappedReference reference = new JavaMappedReference();
-                String name = refAnnotation.name();
-                if (name == null || name.length() == 0) {
-                    if (constructorNames.length < pos + 1 || constructorNames[pos] == null
-                        || 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])) {
-                    throw new InvalidConstructorException(
-                        "Name specified by @Constructor does not match reference name at " + (pos + 1));
-                }
-                if (type.getReferences().get(name) != null) {
-                    throw new DuplicateReferenceException(name);
-                }
-                reference.setName(name);
-                reference.setRequired(refAnnotation.required());
-                JavaServiceContract contract = new JavaServiceContract();
-                contract.setInterfaceClass(param);
-                reference.setServiceContract(contract);
-                type.getReferences().put(name, reference);
-                annotationsDeclared = true;
-                explicitNames.add(name);
-            }
-        }
-        return annotationsDeclared;
-    }
 }

Modified: incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java?rev=422337&r1=422336&r2=422337&view=diff
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java (original)
+++ incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java Sat Jul 15 19:18:48 2006
@@ -13,8 +13,7 @@
  */
 package org.apache.tuscany.core.implementation.processor;
 
-import static org.apache.tuscany.core.implementation.processor.ProcessorUtils.areUnique;
-
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Member;
@@ -28,9 +27,12 @@
 import java.util.Map;
 import java.util.Set;
 
+import org.osoa.sca.annotations.Property;
+import org.osoa.sca.annotations.Reference;
 import org.osoa.sca.annotations.Remotable;
 import org.osoa.sca.annotations.Service;
 
+import org.apache.tuscany.spi.annotation.Autowire;
 import org.apache.tuscany.spi.component.CompositeComponent;
 import org.apache.tuscany.spi.deployer.DeploymentContext;
 
@@ -42,7 +44,9 @@
 import org.apache.tuscany.core.implementation.JavaServiceContract;
 import org.apache.tuscany.core.implementation.PojoComponentType;
 import org.apache.tuscany.core.implementation.ProcessingException;
+import static org.apache.tuscany.core.implementation.processor.ProcessorUtils.areUnique;
 import static org.apache.tuscany.core.implementation.processor.ProcessorUtils.createService;
+import static org.apache.tuscany.core.implementation.processor.ProcessorUtils.processParam;
 import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllInterfaces;
 import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllPublicAndProtectedFields;
 import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllUniquePublicProtectedMethods;
@@ -149,8 +153,7 @@
      */
     @SuppressWarnings("unchecked")
     private void calcConstructor(PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
-                                 Class<?> clazz) throws NoConstructorException,
-                                                        AmbiguousConstructorException {
+                                 Class<?> clazz) throws ProcessingException {
         // determine constructor if one is not annotated
         if (type.getConstructorDefinition() != null) {
             return;
@@ -167,7 +170,17 @@
                 ConstructorDefinition<?> definition = new ConstructorDefinition(constructor);
                 type.setConstructorDefinition(definition);
                 return;
+            } else {
+                Annotation[][] annotations = constructor.getParameterAnnotations();
+                List<String> paramNames = new ArrayList<String>();
+                if (annotationsDefined(annotations)) {
+                    for (int i = 0; i < params.length; i++) {
+                        Class param = params[i];
+                        processParam(param, annotations[i], new String[0], i, type, paramNames);
+                    }
+                }
             }
+
             Map<String, JavaMappedProperty<?>> props = type.getProperties();
             Map<String, JavaMappedReference> refs = type.getReferences();
             if (!areUnique(params)) {
@@ -222,6 +235,22 @@
             ConstructorDefinition<?> definition = new ConstructorDefinition(selected);
             type.setConstructorDefinition(definition);
         }
+    }
+
+    private boolean annotationsDefined(Annotation[][] annots) {
+        // since all parameters must be annotated or not, just check the first one
+        if (annots.length > 0 && annots[0].length > 0) {
+            Annotation[] annotations = annots[0];
+            for (Annotation annotation : annotations) {
+                Class<? extends Annotation> annotType = annotation.annotationType();
+                if (annotType.equals(Autowire.class)
+                    || annotType.equals(Property.class)
+                    || annotType.equals(Reference.class)) {
+                    return true;
+                }
+            }
+        }
+        return false;
     }
 
     /**

Modified: incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ProcessorUtils.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ProcessorUtils.java?rev=422337&r1=422336&r2=422337&view=diff
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ProcessorUtils.java (original)
+++ incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ProcessorUtils.java Sat Jul 15 19:18:48 2006
@@ -13,14 +13,24 @@
  */
 package org.apache.tuscany.core.implementation.processor;
 
+import java.lang.annotation.Annotation;
+import java.util.List;
+
 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.osoa.sca.annotations.Scope;
 
+import org.apache.tuscany.spi.annotation.Autowire;
 import org.apache.tuscany.spi.model.InteractionScope;
 
+import org.apache.tuscany.core.implementation.JavaMappedProperty;
+import org.apache.tuscany.core.implementation.JavaMappedReference;
 import org.apache.tuscany.core.implementation.JavaMappedService;
 import org.apache.tuscany.core.implementation.JavaServiceContract;
+import org.apache.tuscany.core.implementation.PojoComponentType;
+import org.apache.tuscany.core.implementation.ProcessingException;
 import org.apache.tuscany.core.util.JavaIntrospectionHelper;
 
 /**
@@ -97,4 +107,115 @@
         }
     }
 
+    /**
+     * Processes a constructor parameter by introspecting its annotations
+     *
+     * @param param            the parameter to process
+     * @param paramAnnotations the parameter annotations
+     * @param constructorNames the array of constructorNames specified by @Constructor
+     * @param pos              the declaration position of the constructor parameter
+     * @param type             the component type associated with implementation being reflected
+     * @param explicitNames    the list of parameter constructorNames specified on parameter annotations
+     * @return true if the parameters have explicit annotation declarations
+     * @throws org.apache.tuscany.core.implementation.ProcessingException
+     *
+     */
+    public static boolean processParam(Class<?> param,
+                                       Annotation[] paramAnnotations,
+                                       String[] constructorNames,
+                                       int pos,
+                                       PojoComponentType<JavaMappedService, JavaMappedReference,
+                                           JavaMappedProperty<?>> type,
+                                       List<String> explicitNames)
+        throws ProcessingException {
+        boolean annotationsDeclared = false;
+        for (Annotation annot : paramAnnotations) {
+            if (Autowire.class.equals(annot.annotationType())) {
+                // 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 < pos + 1 || constructorNames[pos] == null
+                        || constructorNames[pos].length() == 0) {
+                        throw new InvalidAutowireException("No name specified for autowire parameter " + (pos + 1));
+                    }
+                    name = constructorNames[pos];
+                } else if (pos < constructorNames.length
+                    && constructorNames[pos] != null
+                    && constructorNames[pos].length() != 0
+                    && !name.equals(constructorNames[pos])) {
+                    throw new InvalidConstructorException(
+                        "Name specified by @Constructor does not match autowire name at " + (pos + 1));
+                }
+                reference.setName(name);
+                JavaServiceContract contract = new JavaServiceContract();
+                contract.setInterfaceClass(param);
+                reference.setServiceContract(contract);
+                type.getReferences().put(name, reference);
+                annotationsDeclared = true;
+                explicitNames.add(name);
+            } else if (Property.class.equals(annot.annotationType())) {
+                // TODO multiplicity
+                // the param is marked as a property
+                Property propAnnot = (Property) annot;
+                JavaMappedProperty property = new JavaMappedProperty();
+                String name = propAnnot.name();
+                if (name == null || name.length() == 0) {
+                    if (constructorNames.length < pos + 1 || constructorNames[pos] == null
+                        || 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])) {
+                    throw new InvalidConstructorException(
+                        "Name specified by @Constructor does not match property name at " + (pos + 1));
+                }
+                if (type.getProperties().get(name) != null) {
+                    throw new DuplicatePropertyException(name);
+                }
+                property.setName(name);
+                property.setRequired(propAnnot.required());
+                property.setJavaType(param);
+                type.getProperties().put(name, property);
+                annotationsDeclared = true;
+                explicitNames.add(name);
+            } else if (Reference.class.equals(annot.annotationType())) {
+                // TODO multiplicity
+                // the param is marked as a reference
+                Reference refAnnotation = (Reference) annot;
+                JavaMappedReference reference = new JavaMappedReference();
+                String name = refAnnotation.name();
+                if (name == null || name.length() == 0) {
+                    if (constructorNames.length < pos + 1 || constructorNames[pos] == null
+                        || 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])) {
+                    throw new InvalidConstructorException(
+                        "Name specified by @Constructor does not match reference name at " + (pos + 1));
+                }
+                if (type.getReferences().get(name) != null) {
+                    throw new DuplicateReferenceException(name);
+                }
+                reference.setName(name);
+                reference.setRequired(refAnnotation.required());
+                JavaServiceContract contract = new JavaServiceContract();
+                contract.setInterfaceClass(param);
+                reference.setServiceContract(contract);
+                type.getReferences().put(name, reference);
+                annotationsDeclared = true;
+                explicitNames.add(name);
+            }
+        }
+        return annotationsDeclared;
+    }
 }

Modified: incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java?rev=422337&r1=422336&r2=422337&view=diff
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java (original)
+++ incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java Sat Jul 15 19:18:48 2006
@@ -13,8 +13,11 @@
  */
 package org.apache.tuscany.core.implementation.processor;
 
+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.model.ServiceContract;
 
 import junit.framework.TestCase;
@@ -120,6 +123,27 @@
         }
     }
 
+    public void testConstructorPropertyAnnotatedParamsOnly() throws Exception {
+        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
+            new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
+        processor.visitEnd(null, Foo7.class, type, null);
+        assertNotNull(type.getProperties().get("myProp"));
+    }
+
+    public void testConstructorReferenceAnnotatedParamsOnly() throws Exception {
+        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
+            new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
+        processor.visitEnd(null, Foo8.class, type, null);
+        assertNotNull(type.getReferences().get("myRef"));
+    }
+
+    public void testConstructorAutowireAnnotatedParamsOnly() throws Exception {
+        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
+            new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
+        processor.visitEnd(null, Foo9.class, type, null);
+        assertNotNull(type.getReferences().get("myAutowire"));
+    }
+
     @SuppressWarnings("unchecked")
     public void testDefaultConstructor() throws Exception {
         PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
@@ -164,5 +188,35 @@
         public Foo6(Ref ref) {
         }
     }
+
+    public static class Foo7 {
+        public Foo7(@Property(name = "myProp") String prop) {
+        }
+    }
+
+
+    public static class Foo8 {
+        public Foo8(@Reference(name = "myRef") String ref) {
+        }
+    }
+
+    public static class Foo9 {
+        public Foo9(@Autowire(name = "myAutowire") String autowire) {
+        }
+    }
+
+    public static class Foo10 {
+
+        public Foo10() {
+        }
+
+        public Foo10(String prop) {
+        }
+
+        public Foo10(@Property(name = "prop1") String prop1, @Property(name = "prop2") String prop2) {
+
+        }
+    }
+
 
 }



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