incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwo...@apache.org
Subject svn commit: r945286 - in /incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303: Jsr303MetaBeanFactory.java extensions/MethodValidatorImpl.java
Date Mon, 17 May 2010 18:31:47 GMT
Author: dwoods
Date: Mon May 17 18:31:47 2010
New Revision: 945286

URL: http://svn.apache.org/viewvc?rev=945286&view=rev
Log:
BVAL-45 ValidationException must be thrown when validating a bean with an annotated method
which isn't a valid getter.  Contributed by Carlos Vara.

Modified:
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java?rev=945286&r1=945285&r2=945286&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
Mon May 17 18:31:47 2010
@@ -225,11 +225,53 @@ public class Jsr303MetaBeanFactory imple
                                   new AppendValidationToMeta(metaProperty));
                         }
                     }
-                } // TODO: If propName == null AND it's annotated, throw Ex
+                }
+                else if ( hasValidationConstraintsDefined(method) ) {
+                    throw new ValidationException("Property " + method.getName() + " does
not follow javabean conventions.");
+                }
             }
         }
         addXmlConstraints(beanClass, metabean);
     }
+    
+    private boolean hasValidationConstraintsDefined(Method method) {
+        boolean ret = false;
+        for ( Annotation annot : method.getDeclaredAnnotations() ) {
+            if ( true == (ret = hasValidationConstraintsDefined(annot)) ) {
+                break;
+            }
+        }
+        
+        return ret;
+    }
+    
+    private boolean hasValidationConstraintsDefined(Annotation annot) {
+        // If it is annotated with @Constraint
+        if ( annot.annotationType().getAnnotation(Constraint.class) != null ) {
+            return true;
+        }
+        boolean ret = false;
+        
+        // Check in case it is a multivalued constraint
+        Object value = null;
+        try {
+            value = SecureActions.getAnnotationValue(annot, ANNOTATION_VALUE);
+        } catch (IllegalAccessException e) {
+            // Swallow it
+        } catch (InvocationTargetException e) {
+            // Swallow it
+        }
+        
+        if ( value instanceof Annotation[] ) {
+            for (Annotation annot2 : (Annotation[])value ) {
+                if ( true == (ret = hasValidationConstraintsDefined(annot2)) ) {
+                    break;
+                }
+            }
+        }
+        
+        return ret;
+    }
 
     /** add cascade validation and constraints from xml mappings */
     private void addXmlConstraints(Class<?> beanClass, MetaBean metabean)

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java?rev=945286&r1=945285&r2=945286&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java
Mon May 17 18:31:47 2010
@@ -17,6 +17,8 @@
 package org.apache.bval.jsr303.extensions;
 
 import javax.validation.ConstraintViolation;
+import javax.validation.ValidationException;
+import javax.validation.metadata.BeanDescriptor;
 import javax.validation.metadata.ConstraintDescriptor;
 
 import org.apache.bval.jsr303.ApacheFactoryContext;
@@ -25,6 +27,7 @@ import org.apache.bval.jsr303.ClassValid
 import org.apache.bval.jsr303.ConstraintValidation;
 import org.apache.bval.jsr303.ConstraintValidationListener;
 import org.apache.bval.jsr303.GroupValidationContext;
+import org.apache.bval.jsr303.Jsr303Features;
 import org.apache.bval.jsr303.groups.Group;
 import org.apache.bval.jsr303.groups.Groups;
 import org.apache.bval.model.MetaBean;
@@ -44,6 +47,17 @@ class MethodValidatorImpl extends ClassV
         super(factoryContext);
     }
 
+    
+    @Override
+    public BeanDescriptor getConstraintsForClass(Class<?> clazz) {
+        // TODO: Cache this MetaBean
+        MetaBean uncachedStubmetaBean = new MetaBean();
+        uncachedStubmetaBean.setBeanClass(clazz);
+        BeanDescriptorImpl edesc = createBeanDescriptor(uncachedStubmetaBean);
+        return edesc;
+    }
+
+
     @Override
     protected BeanDescriptorImpl createBeanDescriptor(MetaBean metaBean) {
         MethodBeanDescriptorImpl descriptor = new MethodBeanDescriptorImpl(factoryContext,



Mime
View raw message