Revision as of 21:32, 4 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

How do I use Java Generics with Lookup?

As of NetBeans 6, a number of convenience methods have been added to lookup, and support for Java generics has been added to Lookup. The following are differences:

NB 5.x Code NB 6 Code
Lookup.Result r = lkp.lookup(

  new Lookup.Template(X.class))
Lookup.Result<? extends X> r = 
Collection c = r.allInstances()
{Collection<? extends X> c =
Collection<? extends X> c = 

The new style also works well with for-loops and avoids casts:

for (SomeService s : Lookup.getDefault().lookupAll(SomeService.class)) {
    // ...
// ...
SomeSingleton s = Lookup.getDefault().lookup(SomeSingleton.class);
if (s != null) {
    // ...
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo