I'm using validation 5.1.2.Final.
Trying this code throws a ValidationException, which I was a little surprised about after reading the javadoc.
Code:
@SuppressWarnings("unchecked")
final ConstraintViolationImpl<Object> impl = violation.unwrap(ConstraintViolationImpl.class);
The 5.1.2.Final sources say that method is:
Code:
@Override
public <C> C unwrap(Class<C> type) {
if ( type.isAssignableFrom( ConstraintViolation.class ) ) {
return type.cast( this );
}
throw log.getTypeNotSupportedForUnwrappingException( type );
}
Which looks like a bug to me. That condition is essentially
Code:
type.equals( ConstraintViolation.class )
which doesn't make much sense to me, you already have it as a ConstraintViolation in order to get access to that method. It looks like it was intended to be the other way around:
Code:
ConstraintViolation.class.isAssignableFrom( type )
I can see its changed in master but it still looks backwards. Wouldn't it make more sense to be something like this?
Code:
@Override
public <C> C unwrap(Class<C> type) {
if ( type.isInstance( this ) ) {
return type.cast( this );
}
throw log.getTypeNotSupportedForUnwrappingException( type );
}
Is this a conscious decision not to expose the impl class or a bug? It doesn't really make much difference in my use case as `violation` is always the impl so I can just cast it but I would have though the above usage would have been the intention of that method.