基于Eclipse的模型框架。它是Eclipse MDA(Model Driven Architecture)的一个重要组成部分,是Eclipse中许多项目的基础( e.g, GEF), EMF可以将模型转换成高效的,正确的,和易于定制的Java代码。 官网地址:http://www.eclipse.org/modeling/emf/ The EMF project is a modeling framework and code generation facility for building tools and other applications based on a structured data model. From a model specification described in XMI, EMF provides tools and runtime support to produce a set of Java classes for the model, along with a set of adapter classes that enable viewing and command-based editing of the model, and a basic editor
首先,你选用的Eclipse需要支持EMF开发才可以。
- 使用快捷键Ctrl+N,找到菜单Eclipse Modeling Framework
- 选择Empty EMF Project,填入工程名称,然后finish
- 选择model目录,按Ctrl+N,选择菜单Ecore Model,修改文件名为bowling.ecore,finish
- 如下图,双击bowling这个几点就会打开Properties视图,修改Name为bowling,修改Ns Prefix为org.suren.example.bowling,修改Ns URI为http://org/suiren/example/bowling
- 在bowling节点上右键新建一个EClass的Child,然后修改Name为Player
- 在Player节点上新建EAttribute,Name为name,EType为EString;再新建dateOfBirth,EType为EDate
- 在bowling节点上新建League,新增name;再新增一个EReference,EType选择为Player,Containment设置为true,Upper Bound为-1
[caption id="attachment_2507" align="alignnone" width="364"]

bowling.ecore[/caption]
新建一个代码生成器:
- 在model上新建EMF Generator Model,输入文件名:bowling.genmodel
- 选择Ecore Model
- 选择bowling.ecore,然后finish
- 打开bowling.genmodel,在根节点上右键选择Generate All
上面的步骤会生成几个Eclipse插件工程和一个测试工程,这里包含了生成代码的工具类等。 这些插件工程包含如下扩展点: org.eclipse.emf.ecore.generated_package org.eclipse.emf.edit.itemProviderAdapterFactories org.eclipse.ui.newWizards org.eclipse.ui.editors
EMF可以生产页面原型、Java类等资源,但是这都不是产品级别的。EMF客户端(EMF Client Platform)ECP就是为了满足用户可以生产出产品级别的资源而存在的。 官网地址:http://www.eclipse.org/ecp/ 可以从 这里先下载一个现成的客户端来做测试用。找到updatesite地址后,作为Eclipse的一个插件来安装。
Model to Text (M2T)是从模型产生文本的一个模块。
JET是典型的代码生成器的实现。代码生成器是Model Driven Development(MDD)的重要组成部分。MDD的目标是使用一种抽象的模型(例如EMF/ECORE或者UML)来描述一个软件系统,并且可以由此生成出代码。尽管我们可以自定义实现上面的功能,但MDD的优势在于可以自动地实现上面的过程。 使用快捷键Ctrl+N,然后找到Java Emitter Templates->Convert Project to JET Projects,选择一个Java工程后会添加JET Builder;同时,在工程的根目录下会新增templates目录。 选择当前工程后使用快捷键Alt+Enter打开工程属性对话框,找到JET Settings,设置源码目录。 在templates目录下新建文件suren.textjet,输入如下内容 [codesyntax lang="java"]
<%@ jet package="org.suren.test.jet" class="HelloJet"%>
surenpi.com say hello to jet.
[/codesyntax] 到此时,你就可以看到已经生成了HelloJet源文件了。 有关JET Transformations的详细内容,请看这篇文章。
Acceleo是Model to Text Language(MTL)的编码方式的标准实现。
这是另外一种基于JavaDoc的代码生成技术。
我们都知道,通过Eclipse新建一个类时可以添加一个main方法, 这篇文章有对此作具体介绍。
Velocity经常在Web应用中使用,可以做为代码生成器的引擎。
Eclipse插件扩展点清单 Eclipse插件开发HelloWorld篇 http://eclipsesource.com/blogs/tutorials/emf-tutorial/ http://eclipsesource.com/blogs/tutorials/getting-started-with-the-emf-client-platform/ http://www.cnblogs.com/jpcflyer/archive/2012/05/19/2508479.html