users@jersey.java.net

Re: [Jersey] get(null) on KeyComparatorHashMap does not return null - unlike standard Java Maps

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 25 Jun 2010 07:11:23 -0700

Hi Alex,

Can you file an issue and attach the test case?

I copied code from HashMap, modified, and probably introduced an error.

Paul.

On Jun 25, 2010, at 2:27 AM, Alex Treppass wrote:

> KeyComparatorHashMap is a Jersey class that appears to operate like a
> java.util.HashMap, except that you can specify a helper class to
> generate the hashes and equals (so is useful for maps and classes we
> have no control over).
>
> We're using one in our authentication to map our user
> java.security.Principal objects. The behaviour we're noticing is that
> when we do something like:
> -------------------
> KeyComparatorHashMap<Principal, OurPrincipalStateObject>
> principalStates; // assume populated
> Principal p; // null in this case
> OurPrincipalStateObject state = principalStates.get(p);
> if (state == null)
> {
> // Anonymous user or not logged in
> }
> else
> {
> // handle logged-in user
> }
> -------------------
>
> Looking at the Jersey code, it does an internal maskNull() call that
> replaces the null with a new Object(). new Object() doesn’t cast to
> Principal particularly well and we get a classcast exception, rather
> than getting null from the Map.
>
> This behaviour is unlike standard Java Maps, and to get around it we'd
> have to insert an extra null check before attempting to retrieve from
> the map. Is this intentional?
>
> We're using the vanilla release of Jersey 1.2.
>
> Test case attached.
>
> Alex
> <
> KeyComparatorHashMapTest
> .java
> >---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net