-->
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: Constraints initialized with declared field/property type
PostPosted: Sun Mar 30, 2008 12:39 am 
Newbie

Joined: Sun Mar 30, 2008 12:19 am
Posts: 15
It might be nice if Constraint implementations received (in initialize) the base Class of the annotated class. This would allow errors about inappropriately annotated types to be exposed while processing annotations rather than during data validation.

The ability to validate such constraints without the actual type are restricted; for example a @Length designed to work on only String classes would only allow String and Object as String is a final class. Extend @Length to work on arrays and it also has to allow any Class.isArray() == true.

Extend @Length to work on CharSequence and it is no longer worthwhile to attempt any checks - unless a class is final, some subclass could implement that interface, and thus it would be possible for the constraint to work properly at runtime.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 01, 2008 11:12 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Because of generics and subclasses, you do not always know the actual targeted type at initialization time. That defeat the original purpose

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 03, 2008 7:10 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I think it would work, and it could be really useful.
Imagine:
Code:
@NotEmpty
String name;

@NotEmpty
List<Insurance> insurances;

@NotEmpty
byte a;


If the framework could look at the declared type to which it is attaching the specific Contraint instance, it could immediately tell the third annotations is wrong. @NotEmpty accepts Arrays, String, Collection and Map, any subclass will still be accepted. If there were code trying to put a byte in insurance, it wouldn't compile anyway.

It could have some nice consequences:
* tools could use it to produce coding-time warnings
* you won't get RunTimeExceptions for that one value you forgot testing
* separate concerns in Constraint implementation
* faster execution, as implementations can trust the type and forget type checking

Imagine in the case of @NotEmpty I could create an appropriate delegate at initialization time, and always use that one when validate(..) is called, instead of type-testing at each call.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 03, 2008 7:26 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Also if frameworks were supposed to test the validator-validity at initialization, you could get proper warnings for stupid errors like
Code:
@Range(min = 100, max = 50)
public int floor;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 29, 2008 6:51 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Emmanuel,
I had sent you some prototypical code implementing this;
Do you remember? the first was a nearly complete patch to Hibernate Validator, but depended on the XClass from annotations.
Then you was worried about the annotations dependency, so I sent you a very simplified single java class to showcase the same concept without any dependency.
In Hibernate Validator it looks like to have more sense to use the XClass, but validator implementation should only return a list of compatible Class files, so the standard wouldn't need to speak about XClass, that would be an implementation detail.

I know you're busy, I'm just writing this down as a reminder as I still think this feature
would be very nice to have included; also other people may be interested about what we discussed.

_________________
Sanne
http://in.relation.to/


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.