Given a model, EMF can automatically generate the Java source code-the source code allows you to create, query, update, serialize, deserialize, and track changes to your model instance. EMF provides a set of effective reflection APIs and allows you to work with dynamic (non-generated) models.
I. Introduction
Eclipse modeling framework (EMF) is a Java open-source framework and code generation tool used to build tools and other applications based on structured models. While the Eclipse platform provides a strong integration framework on the user interface and file level, EMF enhances this capability to achieve good granularity of data sharing between tools and applications.
Similar to other Java binding frameworks, such as JAXB or XMLBeans, after a model is given, EMF can generate Java source code-it allows you to create, query, update, deserialize and serialize your model instances. Although most Java binding frameworks only support one model class, such as the XML mode, EMF supports the XML mode and UML class diagram (Rational Rose or UML2) and the code generated in the annotated Java interface. In addition to the model code, EMF can also generate a complete application-including a customizable editor.
The code generated by EMF has a built-in change notification mechanism and supports cross-document reference. EMF provides a reflection API to access your model instances and allow you to dynamically create models. EMF supports model Constraints Verification. EMF provides powerful code generation tools to support model re-creation and merging using user-written code.
In this article, we will explain what EMF is and analyze its basic framework.
EMF was initially the implementation of an Object Management Group (OMG) Meta Object device (MOF) specification-it provides a standardized Meta Model for Object-Oriented Analysis and Design. For a long time, EMF has been used to implement a large number of tools and thus evolved into a Java Implementation of a core subset of an effective mof api.
In EMF, the class MOF core meta-model (a model) is called Ecore. In the current implementation of MOF 2.0, there is a subset of a similar MOF model, called the Essential MOF (EMOF), which is now independent. There are major differences between Ecore and EMOF, especially in naming. Therefore, EMF can read and write serialized EMOF transparently, allowing standard data exchange between tools.
Today, EMF has been widely used. For example, EMF is used to implement open-source XML Infoset model (XSD), service data object (SDO), UML2, and Web tool platform (WTP) projects on Eclipse. In addition, EMF is also used in commercial products, such as Omondo EclipseUML and IBM Rational and WebSphere products.
Ii. Ecore and reflection API
One key interface in EMF is Eobject, which is equivalent to java. lang. Object in terms of concept. All modeling objects, whether generated or not, must implement this interface to provide the following important features:
· Similar to Java's Object. getClass (), you can use the eClass () method to retrieve instance metadata, that is, its Eclass.
· On any EMF Modeling object, you can use the reflection API (eGet () and eSet () to access its data. This is conceptually equivalent to Java's java. lang. reflect. Method. invoke () Method, although more efficient.
· From any instance object, you can use the eContainer () method to obtain its container (parent ).
· EObject also extends Notifier, which allows you to monitor all changes to the object data.
As mentioned above, EMF has its own simple metadata-called Ecore. Figure 1 shows the complete class hierarchy structure of Ecore metadata. In Figure 1, you can see that the EPackage contains information about the model class (EClass) and data type (EDataType. EClass describes a modeling class and specifies attributes and references to describe instance data. EAttribute describes simple data, which is specified by an EDataType. EReference describes the association between classes. Its type is Eclass. The EFactory contains the method for creating model elements.
Figure 1. Ecore class hierarchy: this image shows the complete class hierarchy of Ecore metadata.
To find