Iadaptable Analysis in Eclipse

Source: Internet
Author: User
Tags implement int size modify requires return tostring
Java is a strongly typed language, and each instance must have a specified type. In fact, Java types have two types of declarations and run-time types (which can be said to be static types and dynamic types, as appropriate). Weak-type languages such as Python are often called untyped, but this is not rigorous because each instance has its Run-time type. You just don't have to declare the type of an instance beforehand.

To invoke a method in an object, this method needs to exist in the declaring type. That is, you can only invoke a method that is defined in the parent class, even if the instance is a determined subtype:

List List = new ArrayList ();
List.add ("Data"); It's no problem here.
List.ensurecapacity (4); It's not here. Ensurecapacity () only in ArrayList.
If we were to invoke a method in the actual type, we would first convert it to the correct type. In this case, we can convert ArrayList to list because ArrayList implements the list interface. can also be checked at runtime dynamically, using the list instanceof ArrayList.

   Extensible Interface

Unfortunately, a class cannot always implement the interface you need to implement. This may be because it works for only a few situations, or it is a type in a library that is not associated, or the interface is later changed.

In this case, you can use iadaptable. You can change the type of iadaptable dynamically. Avoid direct type conversions using the following methods:

Object o = new ArrayList ();
List List = (list) o;
We can do this:

iadaptable adaptable = new ArrayList ();
List List = (list) adaptable.getadapter (Java.util.List.class);
You can think of it as a type of dynamic transformation; We converted the adaptable to the list instance.

Why not direct conversion instead of using extra Getadapter ()? This mechanism allows us to transform the target class into an interface that is not implemented. For example, we might want to use HashMap as a List, although they are not compatible.

iadaptable adaptable = new HashMap ();
List List = (list) adaptable.getadapter (Java.util.List.class);
   Implement Iadaptable
 
Most iadaptable implementations seem to want to construct the overlay of multiple if expressions for supporting types. If you want to implement Getadapter () for HashMap, you can do this:

public class HashMap implements Iadaptable {
Public Object Getadapter (Class clazz) {
if (clazz = = Java.util.List.class) {
List List = new ArrayList (This.size ());
List.addall (This.values ());
return list;
}
return null;
}
// ...
}


Returns a proxy to itself, not a direct conversion type. If you are requesting an unsupported type, you can return null directly to indicate failure, which is better than throwing an exception.

  Platformobject

When you want to add a new type to be extended, just make a simple change. In any case, if you've got the type, why not modify the interface? There is a reason not to modify the class (if you are using an interface, it is not easy to guarantee backwards compatibility) or to change its type (HashMap is not a List but can be transformed). To solve this problem, in Eclipse, an abstract class platformobject is used. It implements the Iadaptable interface for you, so you can stop worrying about it.

Platformobject proxies all of its requests to getadapter () to Iadaptermanager. Iadaptermanager is provided by the platform by default and accessed through Platform.getadaptermanager (). You can think of it as a huge Map that takes care of the associated classes and the appropriate adapters. Platformobject's Getadapter () method can access this map.

   fit an existing class

The advantage is that each Platformobject object can be dynamically associated with a new adapter without recompiling. This is the way in many parts of eclipse to support scaling.

Here you want to convert the list with string to an XML node. The XML node is displayed as:

<List>
<Entry> A-String </Entry>
<Entry> Second String </Entry>
<Entry> Third String </Entry>
</List>
Because the list's ToString method may have other uses, it cannot be used. You can add a factory to the list, and a node object will automatically return when there is a request to convert to an XML node.

Here are 3 steps to take:

1. Generate node from list

Use Iadapterfactory to encapsulate the conversion mechanism:

Import nu.xom.*;
public class Nodelistfactory implements Iadapterfactory {
/** the supported types we can adapt to * *
private static final class[] types = {
Node.class,
};
Public class[] Getadapterlist () {
return types;
}
/** the actual conversion to a Node * *
public object Getadapter (object list, Class clazz) {
if (clazz = = Node.class && list instanceof list) {
element root = new Element ("List");
Iterator it = List.iterator ();
while (It.hasnext ()) {
ELEMENT item = new Element ("Entry");
Item.appendchild (It.next (). toString ());
Root.appendchild (item);
}
return root;
} else {
return null;
}
}
}
2. Registered Factory to platform Adaptermanager

We need to register the factory to the adapter factory, and when we request node to the list instance, it will know that it is using our registered factory. Platform manages Iadaptermanager for us, and the registration process is fairly simple:

Platform.getadaptermanager (). Registeradapters (
New Nodelistfactory (), List.class
);
The above code requires the Platform manager to associate Nodelistfactory and list. But we require the adapter of the list instance, and it will call this factory. According to our definition of the factory, we get a node object. In Eclispe, this step must be explicitly executed at the time the plug-in is started, and an implicit execution can be done through the org.eclipse.core.runtime.adapters extension point.

3. Request node to list

This requires the adapter to return a Node object:

Node getnodefrom (iadaptable list) {
Object adaptable = List.getadapter (Node.class);
if (adaptable!= null) {
Node node = (node) adaptable;
return node;
}
return null;
}
   Summary

If you want to add functionality to an existing class at run time, just define a factory that can complete the conversion function, and then register the project to the platform Adaptermanager. This feature can be used to register a specified UI component for a non-UI component while maintaining a complete separation of the two parts. It's like using in Org.rcpapps.rcpnews.ui and Org.rcpapps.rcpnews plug-ins. In these examples, Ipropertysource is in the UI plug-in, and it needs to be associated with data from a non-UI plug-in. When the UI plug-in initializes, it registers Ipropertysource to platform, and the corresponding property is displayed in the Properties view when the data object is selected in the browser.

Obviously, Java.util.List can't extend the platformobject, so you can't expect the code in the example to compile, you can reconstruct the subclass of the list to achieve the goal. Inheriting Platformobject is also not necessary:

public class Adaptablelist implements Iadaptable, List {
Public Object Getadapter (Class adapter) {
Return Platform.getadaptermanager (). Getadapter (this, adapter);
}
Private List delegate = new ArrayList ();
public int size () {
return Delegate.size ();
}
// ...
}
In this example, XOM is used to generate XML.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.