-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: ConstraintValidator resolution for arrays
PostPosted: Sun Jul 12, 2009 5:30 am 
Newbie

Joined: Fri Jun 05, 2009 5:29 am
Posts: 4
The spec. doesn't make it clear what the expected behaviour of the system is with regard to ConstraintValidator resolution for arrays of primitive types, e.g. int[].
  • Should arrays of primitive types be converted to arrays of the primitive type wrappers (this could get expensive)?
  • Should they only match ConstraintValidators that have been configured for the specific type (or Object). This would be most in-keeping with the language spec. but would mean that there would have to be many implementations of validators for constraints such as Size (or the validator for Object would need to use value.getClass().isArray() and then do the right thing).

Then there is also the question of what the behaviour should be for arrays of arrays: for example, you might want to have a constraint of the 2d-size of an array.

In the 4.0.0.Alpha3 release of the reference implementation, all arrays (whether primitive or Object) seem to be treated as Array.class for resolution purposes. Everything, then comes crashing down (i.e. a ClassCastException is raised) when trying to call the isValid method of a validator (such as SizeValidatorForArray) that expects an Object[] and an int[] is provided.
Code:
public class TestHibernateValidatorOnPrimitiveArray {
    private static final class BeanWithIntArray {
        @Size(min = 5, max = 10)
        private int[] intArray;

        public int[] getIntArray() {
            return intArray;
        }

        public void setIntArray(int[] intArray) {
            this.intArray = intArray;
        }
    }
   
    @Test
    public void testHibernateValidatorOnArray() {
        ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
        Validator v = vf.getValidator();
       
        BeanWithIntArray object = new BeanWithIntArray();
        object.setIntArray(new int[0]);
       
        Set<ConstraintViolation<BeanWithIntArray>> cv = v.validate(object);
        System.out.println(cv);
    }
}

Code:
java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;
   at org.hibernate.validation.constraints.SizeValidatorForArray.isValid(SizeValidatorForArray.java:31)
   at org.hibernate.validation.engine.ConstraintTree.validateConstraints(ConstraintTree.java:119)
   at org.hibernate.validation.engine.MetaConstraint.validateConstraint(MetaConstraint.java:126)
   at org.hibernate.validation.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:262)
   at org.hibernate.validation.engine.ValidatorImpl.validateConstraints(ValidatorImpl.java:237)
   at org.hibernate.validation.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:189)
   at org.hibernate.validation.engine.ValidatorImpl.validate(ValidatorImpl.java:110)


Top
 Profile  
 
 Post subject: Re: ConstraintValidator resolution for arrays
PostPosted: Mon Jul 20, 2009 9:13 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hum, let me think that through.
So far the rules are the same as the JLS rules wrt subtyping (incl primitive types). I am not sure I want to add ad hoc rules to make life a bit simpler with arrays of primitive types.

http://java.sun.com/docs/books/jls/thir ... .html#4.10

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: ConstraintValidator resolution for arrays
PostPosted: Mon Jul 20, 2009 9:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Regarding the current Hibernate Validator implementation. The latest release 4.0.0.Beta2 contains a validator for each primitive type. See also HV-127

The class SizeValidatorTest contains various tests for primitive arrays.


Top
 Profile  
 
 Post subject: Re: ConstraintValidator resolution for arrays
PostPosted: Wed Sep 23, 2009 11:02 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I did study http://java.sun.com/docs/books/jls/thir ... .html#4.10 more extensively
and I am happy with the Java rules.
Particularly 4.10.1 saying that
Quote:
The following rules define the direct supertype relation among the primitive types:
double >1 float
float >1 long
long >1 int
int >1 char
int >1 short
short >1 byte


and 4.10.3
Quote:
The following rules define the direct subtype relation among array types:
If S and T are both reference types, then S[] >1 T[] iff S >1 T.
Object >1 Object[]
Cloneable >1 Object[]
java.io.Serializable >1 Object[]
If p is a primitive type, then:
Object >1 p[]
Cloneable >1 p[]
java.io.Serializable >1 p[]


Now does the RI properly implement all these rules is another story.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: ConstraintValidator resolution for arrays
PostPosted: Wed Sep 23, 2009 11:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
emmanuel wrote:
I did study http://java.sun.com/docs/books/jls/thir ... .html#4.10 more extensively
and I am happy with the Java rules.

More specifically, the spec says:
- Primitive types are considered equivalent to their respective primitive wrapper class.
- A ConstraintValidator<A, U> is said to be compliant with T if T is a subtype of U (according to the Java Language Specification 3rd edition chapter 4.10 Subtyping). Note that T is a subtype of U if T = U.

So it delegates the subtyping rules to section 4.10 of the JLS and consider primitive types with their non primitive equivalents. The reason why primitives are considered as their wrappers is because you cannot write
Code:
class PrimitiveConstraintValidator implements ConstraintValidator<Size, int> {}

It does not compile.
I will make it clearer in the spec that arrays of primitives are converted into arrays of wrappers.

As for arrays of arrays, I think they are property covered by recursively applying rules in 4.10.3

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.