blob: 4b6100880baf640d61180c03295e4e4546c033bd [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqLookupGenerics
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqLookupGenerics
:description: Apache NetBeans wiki DevFaqLookupGenerics
:toc: left
:toc-title:
:syntax: true
=== 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();) {
&nbsp;&nbsp;X x = (X) it.next();
&nbsp;&nbsp;//do something with x
}`
|`for (X x : lkp.lookupAll(X.class)) {
&nbsp;&nbsp;//do something with x
}`
|===
Generics work well with JDK-5-style for-loops:
[source,java]
----
for (SomeService s : Lookup.getDefault().lookupAll(SomeService.class)) {
// ...
}
----
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqLookupGenerics[http://wiki.netbeans.org/DevFaqLookupGenerics] ,
that was last modified by NetBeans user Tboudreau
on 2010-02-19T00:29:05Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.