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 (assume that lkp
is a [[DevFaqLookup| Lookup
]]):
NB 5.x Code | NB 6 Code |
---|---|
|
|
|
|
|
|
Lookup.Result r = lkp.lookup(t); Collection c = r.allInstances(); |
|
Lookup.Result r = lkp.lookup(t); Collection c = r.allInstances(); ` `for (Iterator it=c.iterator(); it.hasNext();) { X x = (X) it.next(); //do something with x } |
//do something with x } |
Generics work well with JDK-5-style for-loops:
for (SomeService s : Lookup.getDefault().lookupAll(SomeService.class)) {
// ...
}