Author: jmarino
Date: Wed Jul 19 19:05:11 2006
New Revision: 423712
URL: http://svn.apache.org/viewvc?rev=423712&view=rev
Log:
constructor injection tweaks to enable support of annotation extensions in CDI
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorAutowireTestCase.java
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java?rev=423712&r1=423711&r2=423712&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
(original)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java
Wed Jul 19 19:05:11 2006
@@ -13,8 +13,6 @@
*/
package org.apache.tuscany.core.implementation.processor;
-import static
org.apache.tuscany.core.implementation.processor.ProcessorUtils.processParam;
-
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
@@ -30,6 +28,7 @@
import org.apache.tuscany.core.implementation.JavaMappedService;
import org.apache.tuscany.core.implementation.PojoComponentType;
import org.apache.tuscany.core.implementation.ProcessingException;
+import static
org.apache.tuscany.core.implementation.processor.ProcessorUtils.processParam;
/**
* Handles processing of a constructor decorated with [EMAIL PROTECTED]
org.osoa.sca.annotations.Constructor}
@@ -39,6 +38,24 @@
@SuppressWarnings("unchecked")
public class ConstructorProcessor extends ImplementationProcessorSupport {
+ public void visitClass(CompositeComponent<?> parent, Class<?> clazz,
+ PojoComponentType<JavaMappedService,
JavaMappedReference, JavaMappedProperty<?>> type,
+ DeploymentContext context) throws
ProcessingException {
+ Constructor[] ctors = clazz.getConstructors();
+ boolean found = false;
+ for (Constructor constructor : ctors) {
+ if
(constructor.getAnnotation(org.osoa.sca.annotations.Constructor.class) != null)
{
+ if (found) {
+ DuplicateConstructorException e =
+ new DuplicateConstructorException("More than one
constructor marked with @Constructor");
+ e.setIdentifier(constructor.getDeclaringClass().getName());
+ throw e;
+ }
+ found = true;
+ }
+ }
+ }
+
public void visitConstructor(CompositeComponent<?> parent, Constructor<?>
constructor,
PojoComponentType<JavaMappedService,
JavaMappedReference, JavaMappedProperty<?>> type,
DeploymentContext context) throws
ProcessingException {
@@ -86,5 +103,4 @@
}
type.setConstructorDefinition(definition);
}
-
}
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java?rev=423712&r1=423711&r2=423712&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
(original)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
Wed Jul 19 19:05:11 2006
@@ -164,6 +164,7 @@
e.setIdentifier(clazz.getName());
throw e;
} else if (constructors.length == 1) {
+ boolean explictAnnotations;
Constructor constructor = constructors[0];
Class[] params = constructor.getParameterTypes();
if (params.length == 0) {
@@ -173,21 +174,26 @@
} else {
Annotation[][] annotations =
constructor.getParameterAnnotations();
List<String> paramNames = new ArrayList<String>();
- if (annotationsDefined(annotations)) {
+ explictAnnotations = annotationsDefined(annotations);
+ if (explictAnnotations) {
for (int i = 0; i < params.length; i++) {
Class param = params[i];
processParam(param, annotations[i], new String[0], i,
type, paramNames);
}
+ ConstructorDefinition<?> definition = new
ConstructorDefinition(constructor);
+ definition.setInjectionNames(paramNames);
+ type.setConstructorDefinition(definition);
+ return;
}
}
Map<String, JavaMappedProperty<?>> props = type.getProperties();
Map<String, JavaMappedReference> refs = type.getReferences();
- if (!areUnique(params)) {
+ if (!explictAnnotations && !areUnique(params)) {
throw new AmbiguousConstructorException(
"Unable to resolve parameter types as they are not unique,
use @Constructor");
}
- if (!calcPropRefUniqueness(props.values(), refs.values())) {
+ if (!explictAnnotations && !calcPropRefUniqueness(props.values(),
refs.values())) {
throw new AmbiguousConstructorException(
"Unable to resolve parameter types as reference and
property types are not unique, "
+ "use @Constructor");
@@ -498,4 +504,4 @@
as a reference, otherwise it will be defined as a property.
-*/
+*/
\ No newline at end of file
Modified:
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorAutowireTestCase.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorAutowireTestCase.java?rev=423712&r1=423711&r2=423712&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorAutowireTestCase.java
(original)
+++
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorAutowireTestCase.java
Wed Jul 19 19:05:11 2006
@@ -16,6 +16,8 @@
import java.lang.reflect.Constructor;
import java.util.List;
+import org.osoa.sca.annotations.Property;
+
import org.apache.tuscany.spi.annotation.Autowire;
import junit.framework.TestCase;
@@ -80,6 +82,18 @@
}
}
+ /**
+ * Verifies processing executes with additional extension annotations
+ */
+ public void testRandomAnnotation() throws Exception {
+ PojoComponentType<JavaMappedService, JavaMappedReference,
JavaMappedProperty<?>> type =
+ new PojoComponentType<JavaMappedService, JavaMappedReference,
JavaMappedProperty<?>>();
+ Constructor ctor = Foo3.class.getConstructor(String.class,
String.class);
+ processor.visitConstructor(null, ctor, type, null);
+ assertEquals(1, type.getProperties().size());
+ assertNotNull(type.getProperties().get("prop1"));
+ }
+
private static interface Bar {
}
@@ -119,4 +133,14 @@
}
+ public static class Foo3 {
+
+ @org.osoa.sca.annotations.Constructor({"prop1"})
+ public Foo3(@Property String prop, @Baz String baz) {
+ }
+ }
+
+ public @interface Baz {
+
+ }
}
Modified:
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java?rev=423712&r1=423711&r2=423712&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java
(original)
+++
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessorTestCase.java
Wed Jul 19 19:05:11 2006
@@ -17,11 +17,8 @@
public void testDuplicateConstructor() throws Exception {
PojoComponentType<JavaMappedService, JavaMappedReference,
JavaMappedProperty<?>> type =
new PojoComponentType<JavaMappedService, JavaMappedReference,
JavaMappedProperty<?>>();
- Constructor ctor1 = BadFoo.class.getConstructor(String.class);
- Constructor ctor2 = BadFoo.class.getConstructor(String.class,
String.class);
- processor.visitConstructor(null, ctor1, type, null);
try {
- processor.visitConstructor(null, ctor2, type, null);
+ processor.visitClass(null, BadFoo.class, type, null);
fail();
} catch (DuplicateConstructorException e) {
// expected
Modified:
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java?rev=423712&r1=423711&r2=423712&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
(original)
+++
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
Wed Jul 19 19:05:11 2006
@@ -152,8 +152,36 @@
assertNotNull(type.getConstructorDefinition().getConstructor());
}
+ public void testSameTypesButAnnotated() throws Exception {
+ PojoComponentType<JavaMappedService, JavaMappedReference,
JavaMappedProperty<?>> type =
+ new PojoComponentType<JavaMappedService, JavaMappedReference,
JavaMappedProperty<?>>();
+ processor.visitEnd(null, Foo12.class, type, null);
+ assertEquals(2, type.getProperties().size());
+ assertNotNull(type.getProperties().get("prop1"));
+ assertNotNull(type.getProperties().get("prop2"));
+ }
+
+ /**
+ * Verifies processing executes with additional extension annotations
+ */
+ public void testRandomAnnotation() throws Exception {
+ PojoComponentType<JavaMappedService, JavaMappedReference,
JavaMappedProperty<?>> type =
+ new PojoComponentType<JavaMappedService, JavaMappedReference,
JavaMappedProperty<?>>();
+ processor.visitEnd(null, Foo11.class, type, null);
+ assertEquals(1, type.getProperties().size());
+ assertNotNull(type.getProperties().get("prop1"));
+ }
+
+ public void testAutowire() throws Exception {
+ PojoComponentType<JavaMappedService, JavaMappedReference,
JavaMappedProperty<?>> type =
+ new PojoComponentType<JavaMappedService, JavaMappedReference,
JavaMappedProperty<?>>();
+ processor.visitEnd(null, Foo13.class, type, null);
+ assertEquals(1, type.getReferences().size());
+ assertNotNull(type.getReferences().get(String.class.getName()));
+ }
+
public void testMultipleConstructors() throws Exception {
- // throw new UnsupportedOperationException("Finish heuristic multiple
constructors - Foo10");
+ // throw new UnsupportedOperationException("Finish heuristic
multiple constructors - Foo10");
}
public static class Foo1 {
@@ -222,5 +250,26 @@
}
}
+ public static class Foo11 {
+
+ public Foo11(@Property(name = "prop1") String prop, @Baz String baz) {
+ }
+ }
+
+ public static class Foo12 {
+
+ public Foo12(@Property(name = "prop1") String prop, @Property(name =
"prop2") String baz) {
+ }
+ }
+
+
+ public @interface Baz {
+
+ }
+
+ public static class Foo13 {
+ public Foo13(@Autowire String foo) {
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]