-->
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.  [ 4 posts ] 
Author Message
 Post subject: Validate on Generic Object, but return <?>
PostPosted: Sat Aug 07, 2010 4:18 pm 
Regular
Regular

Joined: Sun Sep 26, 2004 9:27 pm
Posts: 75
Location: Atlanta, GA, USA
Apologies, as this may be more of a generic Java question, but I've not been able to figure this out, even from Java 5.

I'm validating a standard Object, as it might be lots of different things. However, I want the return Set to be parameterized as Set<ConstraintViolation<?>>, so that I can pass it to a ConstraintViolationException.

However, the compiler is upset that Object is being passed to validate(), as it assumes the Set should be Set<ConstraintViolation<Object>>. Is there a magical way I can have the Object pretend to be a generic, non-Object Object?

Thanks for any and all help.


Top
 Profile  
 
 Post subject: Re: Validate on Generic Object, but return <?>
PostPosted: Tue Aug 10, 2010 12:40 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Quoting from the Java Language Specification
Quote:
Note that in general, List<?> is a list of unknown type. It is not a subtype of List<T>, for any type T. Allowing such a subtype relation would be unsound
- see eg http://java.sun.com/docs/books/jls/third_edition/html/conversions.html. Or have a look at the generics primer - http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf.

I don't quite understand what you try to achieve, but could you not work with Set<ConstraintViolation<Object>>?

--Hardy


Top
 Profile  
 
 Post subject: Re: Validate on Generic Object, but return <?>
PostPosted: Wed Aug 18, 2010 5:13 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
I think the problem with creating ConstraintViolationException instances you describe is addressed with

http://opensource.atlassian.com/project ... e/BVAL-198

As outlined in said issue you can help yourself by wrapping your set of constraint violations into a new HashSet:

Code:
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(domainObject);

//compiler error: ("The constructor ConstraintViolationException(Set<ConstraintViolation<Object>>) is undefined")
throw new ConstraintViolationException(constraintViolations);

//this works
throw new ConstraintViolationException(new HashSet<ConstraintViolation<?>>(constraintViolations));


Regards,

Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Validate on Generic Object, but return <?>
PostPosted: Wed Aug 18, 2010 5:52 pm 
Regular
Regular

Joined: Sun Sep 26, 2004 9:27 pm
Posts: 75
Location: Atlanta, GA, USA
Thanks Gunnar. That's exactly what I ended up doing.

It's ugly, but it works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.