[hibernate-issues] [Hibernate-JIRA] Commented: (HV-23) NotEmptyValidator throws ClassCastException on non-null wrapper types

Sanne Grinovero (JIRA) noreply at atlassian.com
Sun Mar 30 08:10:33 EDT 2008


    [ http://opensource.atlassian.com/projects/hibernate/browse/HV-23?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_29899 ] 

Sanne Grinovero commented on HV-23:
-----------------------------------

Hi, I've looked at your tests and I confirm the Exception is thrown.

Anyway I think this shouldn't be used on fields to test "non-sizable" values, I didn't understand why not to use @NotNull, as also @NotEmpty and @NotNull should generate the same DB constraints.

Currently the last "if" casts to a String, another solution would be to use .toString():
using .toString on a String should be more efficient than casting and would have the side-effect to not throw this Exception;
still I don't think it would be wise to accept any other type without some warning to the developer.

I would propose to change

return ( (String) value ).length() > 0;
return value.toString().length() > 0;

But a warning should really be issued at validator creation.

The @MaxValidator just returns "false" if it is attached to an unsupported type, surely the
distracted developer will have a hard time to discover why his entities don't validate.

I think the Validator interface should be added a new method to check if it's being well used,
so to give the implementation an opportunity to express some comment at deploy time.

Emmanuel? Others?

> NotEmptyValidator throws ClassCastException on non-null wrapper types
> ---------------------------------------------------------------------
>
>                 Key: HV-23
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HV-23
>             Project: Hibernate Validator
>          Issue Type: Bug
>          Components: validators
>    Affects Versions: Bundle 3.2.1, 3.0.0.ga
>            Reporter: scott leberknight
>         Attachments: NotEmptyValidatorClassCastExceptionBugTest.java
>
>
> The NotEmptyValidator.isValid() method throws a ClassCastException if a non-null primitive wrapper type (e.g. Long) is supplied as the argument. The method assumes that if the value is not null, is not an array, collection, or map, that it can be cast to a String, which it cannot for primitive wrappers. See attached JUnit test, which also contains a possible fix to the logic and has a test for the fix too. I put the file in the org.hibernate.validator package so it can be dropped in as-is to the Hibernate Validator codebase.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list