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() |
| Lookup.Template t = new Lookup.Template(X.class); Lookup.Result r = lkp.lookup(t); Collection c = r.allInstances(); | 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) {
// ...
}