This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 68079 - Lookups.fixed throws undeclared NPE
Summary: Lookups.fixed throws undeclared NPE
Status: RESOLVED WONTFIX
Alias: None
Product: platform
Classification: Unclassified
Component: Lookup (show other bugs)
Version: 5.x
Hardware: All All
: P3 blocker (vote)
Assignee: Jaroslav Tulach
URL:
Keywords: API_REVIEW_FAST
Depends on:
Blocks: 68076
  Show dependency tree
 
Reported: 2005-11-04 09:11 UTC by _ pkuzel
Modified: 2008-12-22 21:52 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description _ pkuzel 2005-11-04 09:11:53 UTC
According to Javadoc:

     * @throws NullPointerException if the supplied argument is null
     */
    public static Lookup fixed(Object[] objectsToLookup) {

should be my case:

   Lookups.fixed(new Object[] {null});

OK. In reality it raises NPE.

It's also more convenient for clients it this utility class ignores nulls.
Comment 1 _ pkuzel 2005-11-04 09:24:12 UTC
Checking in src/org/openide/util/lookup/SimpleLookup.java;
/shared/data/ccvs/repository/openide/util/src/org/openide/util/lookup/SimpleLookup.java,v
 <--  SimpleLookup.java
new revision: 1.2; previous revision: 1.1
done
Checking in test/unit/src/org/openide/util/lookup/SimpleLookupTest.java;
/shared/data/ccvs/repository/openide/util/test/unit/src/org/openide/util/lookup/SimpleLookupTest.java,v
 <--  SimpleLookupTest.java
new revision: 1.2; previous revision: 1.1
done
Comment 2 Jaroslav Tulach 2005-11-04 10:52:23 UTC
Good commit, thanks.  
Comment 3 Jesse Glick 2005-11-04 16:43:03 UTC
I think this should be reverted. The previous Javadoc did *not* permit clients
to pass a null array element. It explicitly forbade a null array, which is
redundant since the general contract for *all* NB APIs is that nulls are always
forbidden unless explicitly permitted; it implicitly forbade a null array
element by the same rule.

Passing a null in the array makes it seem as if the Lookup is acting like a
Collection which permits null values, which is not true - the null will not be
returned from e.g. l.lookup(new Lookup.Template(Object.class)).allInstances().

I don't have any strong feeling about it, just FYI.
Comment 4 _ pkuzel 2005-11-04 16:57:58 UTC
> the general contract for *all* NB APIs is that nulls are always
> forbidden unless explicitly permitted

I have always problem with it. It's API implementor friendly. API client
friendly approach is opposite:
  - all methods accept null arguments until stated
  - no method return null until stated

Lookups.fixed() contructs utility Lookup so I suppose main intent is to remove
load from clients. The change supports this intent.
Comment 5 Jesse Glick 2005-11-04 17:02:03 UTC
It's not about implementor vs. client, it's about complexity of the API
definition. Permitting nulls (if it were properly documented, which this change
was not) adds another clause to the spec of the method: "oh, and also, if you
put some nulls into the array, they will be accepted - but will not affect the
lookup content in any way". Which is gratuitous, IMHO.
Comment 6 Jan Lahoda 2005-11-04 17:40:03 UTC
Just a note: until now, if a client passed null to Lookups.fixed() by accident
(typically getSomething() without noticing the result may be null), client was
warned quickly. Now, the client may spend hours trying to find out why a given
object was not found in the lookup, without any clue that it was simply
discarded by the system.

But, regardless if the current or the original state will persist, I think that
the behaviour should be explicitely described in the javadoc (in particular,
what will happen if new Object[] {null} is put into Lookups.fixed.).
Comment 7 Jesse Glick 2005-11-04 18:18:38 UTC
Agreed. Reopening to at least have Javadoc updated to mention the new semantics
(technically apichanges.xml should mention it too).
Comment 8 _ rkubacki 2005-11-04 18:52:03 UTC
IMO the meaning of Javadoc is that Lookups.fixed(null); will throw NPE.

I agree w/ Jesse that adding nulls to any lookup is not expected and noone
declared that lookup implementations should filter them. Now you introduced
inconsistency between Lookup fixed(Object[] objectsToLookup) and Lookup
fixed(Object[] objectsToLookup, , InstanceContent.Convertor convertor) or
Lookups.singleton(Object objectToLookup).

Re API friendly to clients or implementors: current policy is set and works
well. Hybrid approach would be just a source of confusion. BTW: now we can check
our rules using assertions and have faster production code. If we allow nulls
generaly it will no longer be possible. 
Comment 9 Jaroslav Tulach 2005-11-07 06:18:54 UTC
Petr, please try to address comments of the reviewers. If you have not manage 
to satisfy them by Thursday, I'll rollback your changes. Also, next time 
please do not commit API changes without fast track review. 
Comment 10 Jaroslav Tulach 2005-11-08 16:37:29 UTC
Petr, do not you want to fix the open issues? 
Comment 11 _ pkuzel 2005-11-09 13:47:01 UTC
I have expressed my opinion. Leaving on maintainer to accept/reject.
Comment 12 Jaroslav Tulach 2005-11-09 13:54:05 UTC
Well, you have been asked to "do something" not express your opinion. If that 
is all you are going to do, it is simpler to revert then "do something" 
myself. Tomorrow is the deadline. 
Comment 13 _ pkuzel 2005-11-09 14:12:38 UTC
I have updated my clients. Maintainer can either:
  - accept (+ fixing Javadoc)
  - or reject (+ adding additional null checks and fixing Javadoc)

without breaking these clients.
Comment 14 Jaroslav Tulach 2005-11-10 10:12:52 UTC
With a sadness in heart I've just rollback Petr's commit which I otherwise 
liked.  
 
The reason is that there were objections from the reviewers that Petr refused 
to address and it is not my interest to address them either. 
 
I'd like to remind Petr that next time he should use fast track review to get 
the review *before* integration of a change, so I do not have spend time on 
reverting his commits that were found unsound by majority of reviewers. 
 
 
 
 
"Reverting back Petr's integration of 68079 as it has been found 
unappropriate/unfinished by majority of the reviewers" 
Checking in src/org/openide/util/lookup/SimpleLookup.java; 
/cvs/openide/util/src/org/openide/util/lookup/SimpleLookup.java,v  <--  
SimpleLookup.java 
new revision: 1.3; previous revision: 1.2 
done 
Checking in test/unit/src/org/openide/util/lookup/SimpleLookupTest.java; 
/cvs/openide/util/test/unit/src/org/openide/util/lookup/SimpleLookupTest.java,v  
<--  SimpleLookupTest.java 
new revision: 1.3; previous revision: 1.2 
 
Comment 15 Jan Lahoda 2005-11-10 10:31:08 UTC
I think we should:
-document that null elements in the "fixed" array are prohibited.
-do a precondition testing (something like
Arrays.asList(objectsToLookup).contains(null)=>throw new NPE()).
-test this behavior.

If there are no objections and unless someone else wants to do it, I will do
these changes.
Comment 16 Jaroslav Tulach 2005-11-10 13:16:10 UTC
I was not quite clear. I not only rollbacked the changes, but also added a 
test. Additions to javadoc are always welcomed. 
Comment 17 mslama 2005-11-10 13:26:01 UTC
AWT uses IllegalArgumentException when invalid argument is passed to method
(including null). I think this is better than NPE as NPE might be thrown also
due to other bug in code. On the other hand NPE is more specific.