Decipher Eclipse Architecture: IAdaptable – Part 1 – Brief Introduction

eclipse-core-runtime

If you read Eclipse source code, you will find that IAdaptable is a very popular interface which is implemented by many others. Why do so many classes/interfaces implement IAdaptable? The answer is that Adapter is the core design pattern for eclipse Core Runtime.

What is IAdaptable?

public interface IAdaptable {
	/**
	 * Returns an object which is an instance of the given class
	 * associated with this object. Returns <code>null</code> if
	 * no such object can be found.
	 *
	 * @param adapter the adapter class to look up
	 * @return a object castable to the given class, 
	 *    or <code>null</code> if this object does not
	 *    have an adapter for the given class
	 */
	public Object getAdapter(Class adapter);
}

IAdaptable is used for Eclipse extension support. It adapts an existing class to another interface. The getAdapter method returns an object castable to the given interface.

It is worth to mention that adapting a class means getting a wrapper class which is a subtype of the target. The wrapper class wraps the adaptee.

Why do we need IAdaptable?

As new features are added to the system, and those new features require services provided by the existing class, we need to make the existing services applicable to new features(i.e. class).

A typical example is the Eclipse Properties view. It presents a set of properties of the selected object. When you select any item in Pacakge view or Hierarchy view, the property view will display properties of the selected item.

The Property view needs an interface to fetch the properties, and then display them. The interface is IPropertySource.

The org.eclipse.resources plugin provides interfaces like IResource, IFile, IFolder, etc. If we want Property view display properties of IFile, one way is to let IFile extend IPropertySource. This will work, but there are several problems of this solution.

public interface IFile extends IPropertySource

First of all,an IFile object has to be changed to implement methods of IPropertySource. Secondly, we may need IFile to implement many other interfaces, then the result would be bloated.

If an IFile object implements the IAdapter interface, then

	public Object getAdapter(Class adapter){
		if(adapter.equals(IPropertySource.class)){
			return new PropertySourceAdapter(this);
		}
		return null;
	}
	class PropertySourceAdapter implements IPropertySource{	
		private final Object item;
 
		public PropertySourceAdapter(Object item){
			this.item = item;
		}
		//required methods of IPropertySource ...
	}

In this way, objects that implement IAdaptable interface can easily be adapted to any given interface.

Actually, this is not the solution used in eclipse, because this requires changes of IFile class. When you read part 3 of IAdaptable, you will find that IFile should not be changed. The adapter is configured in plugin.xml file.

Category >> Architecture & Design  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>