Pass the root bean class to constraint violation from validateValue
[JSR 303 TCK Result] 124 of 257 (48.25%) Pass with 8 Failures and 7 Errors.

Review at http://gwt-code-reviews.appspot.com/1428806

Review by: rchandia@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10116 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/validation/client/impl/GwtValidationContext.java b/user/src/com/google/gwt/validation/client/impl/GwtValidationContext.java
index ee68304..9351155 100644
--- a/user/src/com/google/gwt/validation/client/impl/GwtValidationContext.java
+++ b/user/src/com/google/gwt/validation/client/impl/GwtValidationContext.java
@@ -36,6 +36,7 @@
 
   private final BeanDescriptor beanDescriptor;
   private PathImpl path = new PathImpl();
+  private final Class<T> rootBeanClass;
   private final T rootBean;
   private final MessageInterpolator messageInterpolator;
   private final AbstractGwtValidator validator;
@@ -49,15 +50,18 @@
    */
   private final Set<Object> validatedObjects;
 
-  public GwtValidationContext(T rootBean, BeanDescriptor beanDescriptor,
+  public GwtValidationContext(Class<T> rootBeanClass, T rootBean,
+      BeanDescriptor beanDescriptor,
       MessageInterpolator messageInterpolator, AbstractGwtValidator validator) {
-    this(rootBean, beanDescriptor, messageInterpolator, validator,
+    this(rootBeanClass, rootBean, beanDescriptor, messageInterpolator,
+        validator,
         new HashSet<Object>());
   }
 
-  private GwtValidationContext(T rootBean, BeanDescriptor beanDescriptor,
+  private GwtValidationContext(Class<T> rootBeanClass, T rootBean, BeanDescriptor beanDescriptor,
       MessageInterpolator messageInterpolator, AbstractGwtValidator validator,
       Set<Object> validatedObjects) {
+    this.rootBeanClass = rootBeanClass;
     this.rootBean = rootBean;
     this.beanDescriptor = beanDescriptor;
     this.messageInterpolator = messageInterpolator;
@@ -79,8 +83,9 @@
    * @return the new GwtValidationContext.
    */
   public GwtValidationContext<T> append(String name) {
-    GwtValidationContext<T> temp = new GwtValidationContext<T>(rootBean,
-        beanDescriptor, messageInterpolator, validator, validatedObjects);
+    GwtValidationContext<T> temp = new GwtValidationContext<T>(rootBeanClass,
+        rootBean, beanDescriptor, messageInterpolator, validator,
+        validatedObjects);
     temp.path = path.append(name);
     return temp;
   }
@@ -91,8 +96,9 @@
    * @return the new GwtValidationContext.
    */
   public GwtValidationContext<T> appendIndex(String name, int index) {
-    GwtValidationContext<T> temp = new GwtValidationContext<T>(rootBean,
-        beanDescriptor, messageInterpolator, validator, validatedObjects);
+    GwtValidationContext<T> temp = new GwtValidationContext<T>(rootBeanClass,
+        rootBean, beanDescriptor, messageInterpolator, validator,
+        validatedObjects);
     temp.path = path.appendIndex(name, index);
     return temp;
   }
@@ -103,8 +109,9 @@
    * @return the new GwtValidationContext.
    */
   public GwtValidationContext<T> appendIterable(String name) {
-    GwtValidationContext<T> temp = new GwtValidationContext<T>(rootBean,
-        beanDescriptor, messageInterpolator, validator, validatedObjects);
+    GwtValidationContext<T> temp = new GwtValidationContext<T>(rootBeanClass,
+        rootBean, beanDescriptor, messageInterpolator, validator,
+        validatedObjects);
     temp.path = path.appendIterable(name);
     return temp;
   }
@@ -115,8 +122,9 @@
    * @return the new GwtValidationContext.
    */
   public GwtValidationContext<T> appendKey(String name, Object key) {
-    GwtValidationContext<T> temp = new GwtValidationContext<T>(rootBean,
-        beanDescriptor, messageInterpolator, validator, validatedObjects);
+    GwtValidationContext<T> temp = new GwtValidationContext<T>(rootBeanClass,
+        rootBean, beanDescriptor, messageInterpolator, validator,
+        validatedObjects);
     temp.path = path.appendKey(name, key);
     return temp;
   }
@@ -134,9 +142,8 @@
     return rootBean;
   }
 
-  @SuppressWarnings("unchecked")
   public Class<T> getRootBeanClass() {
-    return (Class<T>) rootBean.getClass();
+    return rootBeanClass;
   }
 
   public AbstractGwtValidator getValidator() {
diff --git a/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java b/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java
index 703e953..437d897 100644
--- a/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java
+++ b/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java
@@ -1028,7 +1028,7 @@
    * {@link javax.validation.ReportAsSingleViolation ReportAsSingleViolation},
    * then is called recursively and the {@code violationsVar} is changed to
    * match the the {@code constraintDescriptorVar}.
-   * 
+   *
    * @param sw the Source Writer
    * @param p the property
    * @param elementClass The class of the Element
@@ -1043,7 +1043,7 @@
       String constraintDescriptorVar, String violationsVar)
       throws UnableToCompleteException {
     boolean isComposite = !constraint.getComposingConstraints().isEmpty();
-    boolean firstReportAsSingleViolation = 
+    boolean firstReportAsSingleViolation =
         constraint.isReportAsSingleViolation()
         && violationsVar.equals(DEFAULT_VIOLATION_VAR) && isComposite;
     boolean reportAsSingleViolation = firstReportAsSingleViolation
@@ -1393,6 +1393,10 @@
 
   private void writeValidatePropertyCall(SourceWriter sw,
       PropertyDescriptor property, boolean useValue) {
+    if (useValue) {
+      // boolean valueTypeMatches = false;
+      sw.println("boolean valueTypeMatches = false;");
+    }
     if (beanHelper.hasGetter(property)) {
       if (useValue) {
         // if ( value == null || value instanceof propertyType) {
@@ -1401,13 +1405,16 @@
             property, false)));
         sw.println(") {");
         sw.indent();
+
+        // valueTypeMatches = true;
+        sw.println("valueTypeMatches = true;");
       }
       // validate_getMyProperty
       writeValidateGetterCall(sw, property, useValue);
       if (useValue) {
-        // } else
+        // }
         sw.outdent();
-        sw.print("} else ");
+        sw.println("}");
       }
     }
 
@@ -1419,19 +1426,22 @@
             property, true)));
         sw.println(") {");
         sw.indent();
+
+        // valueTypeMatches = true;
+        sw.println("valueTypeMatches = true;");
       }
       // validate_myProperty
       writeValidateFieldCall(sw, property, useValue);
       if (useValue) {
         // } else
         sw.outdent();
-        sw.print("}  else ");
+        sw.println("}");
       }
     }
 
     if (useValue & (beanHelper.hasGetter(property) || beanHelper.hasField(property))) {
-      // {
-      sw.println(" {");
+      // if(!valueTypeMatches) {
+      sw.println("if(!valueTypeMatches)  {");
       sw.indent();
 
       // throw new ValidationException(value.getClass +
diff --git a/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java b/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java
index 4387ec7..60a2bf0 100644
--- a/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java
+++ b/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java
@@ -120,12 +120,16 @@
   private void writeContext(SourceWriter sw, BeanHelper bean, String objectName) {
     // GwtValidationContext<T> context = new GwtValidationContext<T>(
     sw.print(GwtValidationContext.class.getSimpleName());
-    sw.print("<T> context =");
-    sw.print("    new " + GwtValidationContext.class.getSimpleName());
-    sw.println("<T>" + "(");
+    sw.print("<T> context = new ");
+    sw.print(GwtValidationContext.class.getSimpleName());
+    sw.println("<T>(");
     sw.indent();
     sw.indent();
 
+    // (Class<T>) MyBean.class,
+    sw.print("(Class<T>) ");
+    sw.println(bean.getTypeCanonicalName() + ".class, ");
+
     // object,
     sw.println(objectName + ", ");
 
diff --git a/user/test/org/hibernate/jsr303/tck/tests/validation/ValidateValueGwtTest.java b/user/test/org/hibernate/jsr303/tck/tests/validation/ValidateValueGwtTest.java
index ff7cd1a..968074b 100644
--- a/user/test/org/hibernate/jsr303/tck/tests/validation/ValidateValueGwtTest.java
+++ b/user/test/org/hibernate/jsr303/tck/tests/validation/ValidateValueGwtTest.java
@@ -15,7 +15,6 @@
  */
 package org.hibernate.jsr303.tck.tests.validation;
 
-import org.hibernate.jsr303.tck.util.client.Failing;
 
 /**
  * Test wrapper for {@link ValidateValueTest}.
@@ -28,12 +27,10 @@
     delegate.testExistingPropertyWoConstraintsNorCascaded();
   }
 
-  @Failing(issue = 5804)
   public void testValidateValue() {
     delegate.testValidateValue();
   }
 
-  @Failing(issue = 5804)
   public void testValidateValueFailure() {
     delegate.testValidateValueFailure();
   }
@@ -62,7 +59,6 @@
     delegate.testValidateValueWithNullPropertyName();
   }
 
-  // This passes but the desired behavior is still not working
   public void testValidIsNotHonoredValidateValue() {
     delegate.testValidIsNotHonoredValidateValue();
   }