New issue 62 by mmikulicic: getAttribute and Boolean.class unboxing
http://code.google.com/p/sarasvati/issues/detail?id=62
I noticed a usability problem with the new getAttribute API.
I used to do:
if(env.getBooleanAttribute("someattr")) ...
and if the attribute wasn't there it was treated as false.
now, I cannot do simply:
if(env.getAttribute("someattr", Boolean.class)) ...
because if "someattr" doesn't exist, the getAttribute would return null
which then gets autounboxed to the primitive boolean type by the compiler,
and throws a NPE.
This is caused by the fact that the null check happens too early, in the
generic code:
public static <T> T stringToObject (final String string, final Class<T>
type)
{
if ( string == null )
{
return null;
}
....
without allowing the type specific converter (if any) to handle a per-type
defaulting.
I don't know if a per-type defaulting would be useful in general, but I
feel that at least BooleanAttributeConverter should.
Currently I rewritten all my code to:
if ("true".equals(token.getEnv().getAttribute("someattr")))
There's a version that takes a default value. How do you feel about:
if(env.getAttribute("someattr", Boolean.class, false))
Comment #3 on issue 62 by plor...@gmail.com: getAttribute and Boolean.class
unboxing
http://code.google.com/p/sarasvati/issues/detail?id=62
Closing "Won't fix", no response to last comment.