DevFaqLookupGenerics

(Difference between revisions)
Line 1: Line 1:
-
__NOTOC__
 
__NOTOC__
__NOTOC__
===How do I use Java Generics with Lookup?===
===How do I use Java Generics with Lookup?===

Revision as of 15:52, 6 November 2009

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 = 
  lkp.lookupResult(X.class)
Collection c = r.allInstances()
{Collection<? extends X> c =
 r.allInstances()</pre>
Collection<? extends X> c = 
  lkp.lookupAll(X.class);

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