Revision as of 00:16, 19 February 2010 by Tboudreau (Talk | contribs)

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

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

for (SomeService s : Lookup.getDefault().lookupAll(SomeService.class)) {
    // ...
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