Apache NetBeans
Apache NetBeans
Latest release

Apache NetBeans 23

Download

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

DataObject dob = (DataObject) lkp.lookup(DataObject.class)

DataObject dob = lkp.lookup(DataObject.class)

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);

Lookup.Template t = new Lookup.Template(X.class);

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

}

for (X x : lkp.lookupAll(X.class)) {

  //do something with x

}

Generics work well with JDK-5-style for-loops:

for (SomeService s : Lookup.getDefault().lookupAll(SomeService.class)) {
    // ...
}