kmmbvnr / sarasvati

Automatically exported from code.google.com/p/sarasvati
0 stars 5 forks source link

getAttribute and Boolean.class unboxing #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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.

Original issue reported on code.google.com by mmikuli...@gmail.com on 16 Jun 2010 at 3:31

GoogleCodeExporter commented 9 years ago
Currently I rewritten all my code to:

   if ("true".equals(token.getEnv().getAttribute("someattr")))

Original comment by mmikuli...@gmail.com on 16 Jun 2010 at 3:36

GoogleCodeExporter commented 9 years ago
There's a version that takes a default value. How do you feel about:

if(env.getAttribute("someattr", Boolean.class, false))

Original comment by plor...@gmail.com on 16 Jun 2010 at 4:53

GoogleCodeExporter commented 9 years ago
Closing "Won't fix", no response to last comment.

Original comment by plor...@gmail.com on 24 Jul 2011 at 3:43