Author: kelvingoodson
Date: Thu Jul  9 15:06:38 2009
New Revision: 792573

URL: http://svn.apache.org/viewvc?rev=792573&view=rev
Log:
TUSCANY-3136 trap use of @Property reference on methods

Modified:
    
tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java
    
tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java
    
tuscany/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java

Modified: 
tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java?rev=792573&r1=792572&r2=792573&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java
 (original)
+++ 
tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java
 Thu Jul  9 15:06:38 2009
@@ -67,41 +67,52 @@
     @Override
     public void visitMethod(Method method, JavaImplementation type) throws 
IntrospectionException {
         A annotation = method.getAnnotation(annotationClass);
-        if (annotation == null) {
-            return;
-        }
-
-        if (!JavaIntrospectionHelper.isSetter(method)) {
-            throw new IllegalPropertyException("Annotated method is not a 
setter: " + method, method);
-        }
-
-        String name = getName(annotation);
-        if (name == null || "".equals(name)) {
-            name = method.getName();
-            if (name.startsWith("set")) {
-                name = 
JavaIntrospectionHelper.toPropertyName(method.getName());
-            }
-        }
-
-        Map<String, JavaElementImpl> properties = type.getPropertyMembers();
-        JavaElementImpl prop = properties.get(name);
-        // Setter override field
-        if (prop != null && prop.getElementType() != ElementType.FIELD) {
-            throw new DuplicatePropertyException(name);
+        if (annotation != null) {
+               
+               if (!JavaIntrospectionHelper.isSetter(method)) {
+                   throw new IllegalPropertyException("Annotated method is not 
a setter: " + method, method);
+               }
+       
+               String name = getName(annotation);
+               if (name == null || "".equals(name)) {
+                   name = method.getName();
+                   if (name.startsWith("set")) {
+                       name = 
JavaIntrospectionHelper.toPropertyName(method.getName());
+                   }
+               }
+       
+               Map<String, JavaElementImpl> properties = 
type.getPropertyMembers();
+               JavaElementImpl prop = properties.get(name);
+               // Setter override field
+               if (prop != null && prop.getElementType() != ElementType.FIELD) 
{
+                   throw new DuplicatePropertyException(name);
+               }
+       
+               removeProperty(prop, type);
+               
+               JavaElementImpl element = new JavaElementImpl(method, 0);
+               Property property = createProperty(name, element);
+       
+               // add databinding available as annotations, as extensions
+       
+               initProperty(property, annotation);
+               type.getProperties().add(property);
+               properties.put(name, element);
         }
-
-        removeProperty(prop, type);
         
-        JavaElementImpl element = new JavaElementImpl(method, 0);
-        Property property = createProperty(name, element);
-
-        // add databinding available as annotations, as extensions
-
-        initProperty(property, annotation);
-        type.getProperties().add(property);
-        properties.put(name, element);
+        // enforce the constraint that an ordinary method's argument can not 
be a Property
+        Annotation paramsAnnotations[][] = method.getParameterAnnotations();
+        for (int i = 0; i < paramsAnnotations.length; i++) {
+               Annotation argAnnotations[] = paramsAnnotations[i];
+               for (int j = 0; j < argAnnotations.length; j++) {
+                       if(argAnnotations[j].annotationType() == 
org.oasisopen.sca.annotation.Property.class) {
+                               throw new IllegalPropertyException("Argument " 
+ (i+1) + " of method " + method.getName() + " in class " + 
method.getDeclaringClass() + " can not be a Property");
+                       }
+               }
+               }
     }
 
+
     @Override
     public void visitField(Field field, JavaImplementation type) throws 
IntrospectionException {
 

Modified: 
tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java?rev=792573&r1=792572&r2=792573&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java
 (original)
+++ 
tuscany/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java
 Thu Jul  9 15:06:38 2009
@@ -88,7 +88,6 @@
         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");
                        }

Modified: 
tuscany/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java?rev=792573&r1=792572&r2=792573&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java
 (original)
+++ 
tuscany/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java
 Thu Jul  9 15:06:38 2009
@@ -25,6 +25,7 @@
 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.implementation.java.DefaultJavaImplementationFactory;
@@ -119,6 +120,22 @@
     // TODO multiplicity
 //    }
 
+    @Test
+    public void testClassWithBadMethodArgProperty() throws Exception {
+        JavaImplementation type = 
javaImplementationFactory.createJavaImplementation();
+        Method meth = BadFoo2.class.getMethod("BadFoo2Method", String.class);
+
+        try {
+               propertyProcessor.visitMethod(meth, type);
+               
+            fail();
+        } catch (IllegalPropertyException e) {
+               e.printStackTrace();
+               System.out.println("Exception successfully received");
+        }
+
+    }
+    
     private static class Foo {
 
         @org.oasisopen.sca.annotation.Constructor()
@@ -165,5 +182,19 @@
         }
 
     }
+    
+    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(@Property(name = "badMethodArgProp")String 
methArg) 
+        {}
+
+ 
+    }
 
 }


Reply via email to