| // |
| // 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. |
| // |
| |
| = DevFaqLookupHowToOverride |
| :jbake-type: wiki |
| :jbake-tags: wiki, devfaq, needsreview |
| :jbake-status: published |
| :keywords: Apache NetBeans wiki DevFaqLookupHowToOverride |
| :description: Apache NetBeans wiki DevFaqLookupHowToOverride |
| :toc: left |
| :toc-title: |
| :syntax: true |
| |
| == How can I override an instance in the Default Lookup? |
| |
| As a result of NetBeans design for extensibility, you'll find a lot of code like this: |
| |
| [source,java] |
| ---- |
| |
| DialogDisplayer displayer = DialogDisplayer.getDefault(); |
| |
| ---- |
| |
| in which an API is defined `DialogDisplayer` as an abstract class or interface and an implementation is indirectly made available through a static method like `getDefault()`. This approach gives you a default implementation of `DialogDisplayer`, but also lets you "plug in" a different one of your own design. |
| |
| How do you do that? First, here's the implementation of the `getDefault()` method: |
| |
| [source,java] |
| ---- |
| |
| public static DialogDisplayer getDefault() { |
| DialogDisplayer dd = (DialogDisplayer) Lookup.getDefault().lookup(DialogDisplayer.class); |
| |
| if (dd == null) { |
| dd = new Trivial(); |
| } |
| |
| return dd; |
| } |
| |
| ---- |
| |
| As you see, it will attempt to find some instance of `DialogDisplayer` from the default `Lookup` (in other words, one that has been registered via `META-INF/services/`). If it cannot find one, it will return the default implementation (an instance of `Trivial`, which is an inner class of `DialogDisplayer`). |
| |
| Therefore, it seems that you could override the default simply by registering your own implementation of `DialogDisplayer`). If you tried it, you'd find it doesn't work (or at least may not work consistently) because there are already other instances registered and they'll likely take precedence over yours. |
| |
| So, how do you mask out any other implementations so that yours will be used? In the file where you register the new implementation (`META-INF/services/org.openide.DialogDisplayer` in this case), you will prefix the other implementation with a pound sign and a minus sign before listing your own on a different line. For example, here's what the file should look like: |
| |
| [source,yaml] |
| ---- |
| |
| #-org.netbeans.core.windows.services.DialogDisplayerImpl |
| com.tomwheeler.example.SpecialDialogDisplayerImpl |
| |
| ---- |
| |
| More information about this and other Lookup-related topics, including how to set the order of registered services, can be link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/doc-files/api.html[found in the Utilities API documentation]. |
| |
| == 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/DevFaqLookupHowToOverride[http://wiki.netbeans.org/DevFaqLookupHowToOverride] , |
| that was last modified by NetBeans user Jtulach |
| on 2010-07-24T20:15:37Z. |
| |
| |
| *NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed. |