diff options
Diffstat (limited to 'java/sca')
3 files changed, 76 insertions, 23 deletions
diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java index 49c589c5a1..2c6232d66a 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.implementation.java.introspect.impl; import static org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper.getBaseType; +import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -60,27 +61,39 @@ public class ReferenceProcessor extends BaseJavaClassVisitor { @Override public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { Reference annotation = method.getAnnotation(Reference.class); - if (annotation == null) { - return; // Not a reference annotation. - } - if (!JavaIntrospectionHelper.isSetter(method)) { - throw new IllegalReferenceException("Annotated method is not a setter: " + method, method); - } - String name = annotation.name(); - if ("".equals(name)) { - name = JavaIntrospectionHelper.toPropertyName(method.getName()); - } - JavaElementImpl ref = type.getReferenceMembers().get(name); - // Setter override field - if (ref != null && ref.getElementType() != ElementType.FIELD) { - throw new DuplicateReferenceException(name); - } - removeReference(ref, type); - - JavaElementImpl element = new JavaElementImpl(method, 0); - org.apache.tuscany.sca.assembly.Reference reference = createReference(element, name); - type.getReferences().add(reference); - type.getReferenceMembers().put(name, element); + if (annotation != null) { + + if (!JavaIntrospectionHelper.isSetter(method)) { + throw new IllegalReferenceException("Annotated method is not a setter: " + method, method); + } + String name = annotation.name(); + if ("".equals(name)) { + name = JavaIntrospectionHelper.toPropertyName(method.getName()); + } + JavaElementImpl ref = type.getReferenceMembers().get(name); + // Setter override field + if (ref != null && ref.getElementType() != ElementType.FIELD) { + throw new DuplicateReferenceException(name); + } + removeReference(ref, type); + + JavaElementImpl element = new JavaElementImpl(method, 0); + org.apache.tuscany.sca.assembly.Reference reference = createReference(element, name); + type.getReferences().add(reference); + type.getReferenceMembers().put(name, element); + } + + // enforce the constraint that an ordinary method's argument can not be a reference + Annotation paramsAnnotations[][] = method.getParameterAnnotations(); + for (int i = 0; i < paramsAnnotations.length; i++) { + Annotation argAnnotations[] = paramsAnnotations[i]; + for (int j = 0; j < argAnnotations.length; j++) { + Annotation ann = argAnnotations[j]; + if(argAnnotations[j].annotationType() == Reference.class) { + throw new IllegalReferenceException("Argument " + (i+1) + " of method " + method.getName() + " in class " + method.getDeclaringClass() + " can not be a Reference"); + } + } + } } diff --git a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractProcessorTest.java b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractProcessorTest.java index 842897606c..478c5d4be0 100644 --- a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractProcessorTest.java +++ b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractProcessorTest.java @@ -19,6 +19,7 @@ package org.apache.tuscany.sca.implementation.java.introspect.impl; import java.lang.reflect.Constructor; +import java.lang.reflect.Method; import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; @@ -39,8 +40,8 @@ public abstract class AbstractProcessorTest { protected AssemblyFactory factory; protected JavaInterfaceFactory javaFactory; protected ConstructorProcessor constructorProcessor; - private ReferenceProcessor referenceProcessor; - private PropertyProcessor propertyProcessor; + protected ReferenceProcessor referenceProcessor; + protected PropertyProcessor propertyProcessor; private ResourceProcessor resourceProcessor; @@ -63,6 +64,7 @@ public abstract class AbstractProcessorTest { definition = new JavaConstructorImpl<T>(constructor); type.getConstructors().put(constructor, definition); } + JavaParameterImpl[] parameters = definition.getParameters(); for (int i = 0; i < parameters.length; i++) { referenceProcessor.visitConstructorParameter(parameters[i], type); @@ -71,4 +73,6 @@ public abstract class AbstractProcessorTest { // monitorProcessor.visitConstructorParameter(parameters[i], type); } } + + } diff --git a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java index decdc34386..2aac962730 100644 --- a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java +++ b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java @@ -24,15 +24,22 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import java.lang.reflect.Constructor; +import java.lang.reflect.Method; import java.util.List; import org.apache.tuscany.sca.assembly.Multiplicity; import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; import org.apache.tuscany.sca.implementation.java.JavaImplementation; import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.junit.Ignore; import org.junit.Test; +import org.oasisopen.sca.annotation.Property; import org.oasisopen.sca.annotation.Reference; +import com.sun.xml.internal.ws.model.JavaMethodImpl; + /** * @version $Rev$ $Date$ */ @@ -110,6 +117,21 @@ public class ConstructorReferenceTestCase extends AbstractProcessorTest { // expected } } + + @Test + public void testClassWithBadMethodArgReference() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method meth = BadFoo2.class.getMethod("BadFoo2Method", String.class); + + try { + referenceProcessor.visitMethod(meth, type); + + fail(); + } catch (IllegalReferenceException e) { + e.printStackTrace(); + System.out.println("Exception successfully received"); + } + } // public void testMultiplicityRequired() throws Exception { // TODO multiplicity @@ -169,5 +191,19 @@ public class ConstructorReferenceTestCase extends AbstractProcessorTest { } } + + private static class BadFoo2 { + + @org.oasisopen.sca.annotation.Constructor() + public BadFoo2(@Property(name = "myProp", required = true)String prop) { + + } + + /** Java can't tell that the @reference argument is disallowed by SCA, but the run time must reject it*/ + public void BadFoo2Method(@Reference(name = "badMethodArgRef")String methArg) + {} + + + } } |