Interface IAdapterManager


  • public interface IAdapterManager
    An adapter manager maintains a registry of adapter factories. Clients directly invoke methods on an adapter manager to register and unregister adapters. All adaptable objects (that is, objects that implement the IAdaptable interface) funnel IAdaptable.getAdapter invocations to their adapter manager's IAdapterManger.getAdapter method. The adapter manager then forwards this request unmodified to the IAdapterFactory.getAdapter method on one of the registered adapter factories.

    Adapter factories can be registered programmatically using the registerAdapters method. Alternatively, they can be registered declaratively using the org.eclipse.core.runtime.adapters extension point. Factories registered with this extension point will not be able to provide adapters until their corresponding plugin has been activated.

    The following code snippet shows how one might register an adapter of type com.example.acme.Sticky on resources in the workspace.

      IAdapterFactory pr = new IAdapterFactory() {
            @Override
            public Class[] getAdapterList() {
                    return new Class[] { com.example.acme.Sticky.class };
            }
    
            @Override
            public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
                    IResource res = (IResource) adaptableObject;
                    QualifiedName key = new QualifiedName("com.example.acme", "sticky-note");
                    try {
                            com.example.acme.Sticky v = (com.example.acme.Sticky) res.getSessionProperty(key);
                            if (v == null) {
                                    v = new com.example.acme.Sticky();
                                    res.setSessionProperty(key, v);
                            }
                    } catch (CoreException e) {
                            // unable to access session property - ignore
                    }
                    return v;
            }
      }
      Platform.getAdapterManager().registerAdapters(pr, IResource.class);
       

    This interface can be used without OSGi running.

    This interface is not intended to be implemented by clients.

    See Also:
    IAdaptable, IAdapterFactory
    Restriction:
    This interface is not intended to be implemented by clients.
    Restriction:
    This interface is not intended to be extended by clients.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int LOADED
      This value can be returned to indicate that an adapter factory is loaded.
      static int NONE
      This value can be returned to indicate that no applicable adapter factory was found.
      static int NOT_LOADED
      This value can be returned to indicate that an adapter factory was found, but has not been loaded.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      String[] computeAdapterTypes​(Class<?> adaptableClass)
      Returns the types that can be obtained by converting adaptableClass via this manager.
      <T> Class<? super T>[] computeClassOrder​(Class<T> clazz)
      Returns the class search order for a given class.
      <T> T getAdapter​(Object adaptable, Class<T> adapterType)
      Returns an object which is an instance of the given class associated with the given object.
      Object getAdapter​(Object adaptable, String adapterTypeName)
      Returns an object which is an instance of the given class name associated with the given object.
      boolean hasAdapter​(Object adaptable, String adapterTypeName)
      Returns whether there is an adapter factory registered that may be able to convert adaptable to an object of type adapterTypeName.
      Object loadAdapter​(Object adaptable, String adapterTypeName)
      Returns an object that is an instance of the given class name associated with the given object.
      int queryAdapter​(Object adaptable, String adapterTypeName)
      Returns a status of an adapter factory registered that may be able to convert adaptable to an object of type adapterTypeName.
      void registerAdapters​(IAdapterFactory factory, Class<?> adaptable)
      Registers the given adapter factory as extending objects of the given type.
      void unregisterAdapters​(IAdapterFactory factory)
      Removes the given adapter factory completely from the list of registered factories.
      void unregisterAdapters​(IAdapterFactory factory, Class<?> adaptable)
      Removes the given adapter factory from the list of factories registered as extending the given class.
    • Field Detail

      • NONE

        static final int NONE
        This value can be returned to indicate that no applicable adapter factory was found.
        Since:
        org.eclipse.equinox.common 3.3
        See Also:
        Constant Field Values
      • NOT_LOADED

        static final int NOT_LOADED
        This value can be returned to indicate that an adapter factory was found, but has not been loaded.
        Since:
        org.eclipse.equinox.common 3.3
        See Also:
        Constant Field Values
      • LOADED

        static final int LOADED
        This value can be returned to indicate that an adapter factory is loaded.
        Since:
        org.eclipse.equinox.common 3.3
        See Also:
        Constant Field Values
    • Method Detail

      • computeAdapterTypes

        String[] computeAdapterTypes​(Class<?> adaptableClass)
        Returns the types that can be obtained by converting adaptableClass via this manager. Converting means that subsequent calls to getAdapter() or loadAdapter() could result in an adapted object.

        Note that the returned types do not guarantee that a subsequent call to getAdapter with the same type as an argument will return a non-null result. If the factory's plug-in has not yet been loaded, or if the factory itself returns null, then getAdapter will still return null.

        Parameters:
        adaptableClass - the adaptable class being queried
        Returns:
        an array of type names that can be obtained by converting adaptableClass via this manager. An empty array is returned if there are none.
        Since:
        3.1
      • computeClassOrder

        <T> Class<? super T>[] computeClassOrder​(Class<T> clazz)
        Returns the class search order for a given class. The search order from a class with the definition
        class X extends Y implements A, B
        is as follows:
        • the target's class: X
        • X's superclasses in order to Object
        • a breadth-first traversal of each class's interfaces in the order returned by getInterfaces (in the example, X's superinterfaces then Y's superinterfaces)
        Parameters:
        clazz - the class for which to return the class order.
        Returns:
        the class search order for the given class. The returned search order will minimally contain the target class.
        Since:
        3.1
      • getAdapter

        <T> T getAdapter​(Object adaptable,
                         Class<T> adapterType)
        Returns an object which is an instance of the given class associated with the given object. Returns null if no such object can be found.

        Note that this method will never cause plug-ins to be loaded. If the only suitable factory is not yet loaded, this method will return null.

        In most cases, it is preferable for client code to use Adapters.adapt(Object, Class, boolean) rather than calling this method directly since doing so will also detect interfaces supplied by the IAdaptable interface

        Parameters:
        adaptable - the adaptable object being queried (usually an instance of IAdaptable)
        adapterType - the type of adapter to look up
        Returns:
        an object of the given adapter type, or null if the given adaptable object does not have an available adapter of the given type
      • getAdapter

        Object getAdapter​(Object adaptable,
                          String adapterTypeName)
        Returns an object which is an instance of the given class name associated with the given object. Returns null if no such object can be found.

        Note that this method will never cause plug-ins to be loaded. If the only suitable factory is not yet loaded, this method will return null. If activation of the plug-in providing the factory is required, use the loadAdapter method instead.

        In most cases, it is preferable for client code to use Adapters.adapt(Object, Class, boolean) rather than calling this method directly since doing so will also detect interfaces supplied by the IAdaptable interface

        Parameters:
        adaptable - the adaptable object being queried (usually an instance of IAdaptable)
        adapterTypeName - the fully qualified name of the type of adapter to look up
        Returns:
        an object castable to the given adapter type, or null if the given adaptable object does not have an available adapter of the given type
        Since:
        3.0
      • hasAdapter

        boolean hasAdapter​(Object adaptable,
                           String adapterTypeName)
        Returns whether there is an adapter factory registered that may be able to convert adaptable to an object of type adapterTypeName.

        Note that a return value of true does not guarantee that a subsequent call to getAdapter with the same arguments will return a non-null result. If the factory's plug-in has not yet been loaded, or if the factory itself returns null, then getAdapter will still return null.

        Parameters:
        adaptable - the adaptable object being queried (usually an instance of IAdaptable)
        adapterTypeName - the fully qualified class name of an adapter to look up
        Returns:
        true if there is an adapter factory that claims it can convert adaptable to an object of type adapterType, and false otherwise.
        Since:
        3.0
      • queryAdapter

        int queryAdapter​(Object adaptable,
                         String adapterTypeName)
        Returns a status of an adapter factory registered that may be able to convert adaptable to an object of type adapterTypeName.

        One of the following values can be returned:

        • NONE if no applicable adapter factory was found;
        • NOT_LOADED if an adapter factory was found, but has not been loaded;
        • LOADED if an adapter factory was found, and it is loaded.
        Parameters:
        adaptable - the adaptable object being queried (usually an instance of IAdaptable)
        adapterTypeName - the fully qualified class name of an adapter to look up
        Returns:
        a status of the adapter
        Since:
        org.eclipse.equinox.common 3.3
      • loadAdapter

        Object loadAdapter​(Object adaptable,
                           String adapterTypeName)
        Returns an object that is an instance of the given class name associated with the given object. Returns null if no such object can be found.

        Note that unlike the getAdapter methods, this method will cause the plug-in that contributes the adapter factory to be loaded if necessary. As such, this method should be used judiciously, in order to avoid unnecessary plug-in activations. Most clients should avoid activation by using getAdapter instead.

        In most cases, it is preferable for client code to use Adapters.adapt(Object, Class, boolean) rather than calling this method directly since doing so will also detect interfaces supplied by the IAdaptable interface.

        Parameters:
        adaptable - the adaptable object being queried (usually an instance of IAdaptable)
        adapterTypeName - the fully qualified name of the type of adapter to look up
        Returns:
        an object castable to the given adapter type, or null if the given adaptable object does not have an available adapter of the given type
        Since:
        3.0
      • registerAdapters

        void registerAdapters​(IAdapterFactory factory,
                              Class<?> adaptable)
        Registers the given adapter factory as extending objects of the given type.

        If the type being extended is a class, the given factory's adapters are available on instances of that class and any of its subclasses. If it is an interface, the adapters are available to all classes that directly or indirectly implement that interface.

        Parameters:
        factory - the adapter factory
        adaptable - the type being extended
        See Also:
        unregisterAdapters(IAdapterFactory), unregisterAdapters(IAdapterFactory, Class)
      • unregisterAdapters

        void unregisterAdapters​(IAdapterFactory factory)
        Removes the given adapter factory completely from the list of registered factories. Equivalent to calling unregisterAdapters(IAdapterFactory,Class) on all classes against which it had been explicitly registered. Does nothing if the given factory is not currently registered.
        Parameters:
        factory - the adapter factory to remove
        See Also:
        registerAdapters(IAdapterFactory, Class)
      • unregisterAdapters

        void unregisterAdapters​(IAdapterFactory factory,
                                Class<?> adaptable)
        Removes the given adapter factory from the list of factories registered as extending the given class. Does nothing if the given factory and type combination is not registered.
        Parameters:
        factory - the adapter factory to remove
        adaptable - one of the types against which the given factory is registered
        See Also:
        registerAdapters(IAdapterFactory, Class)