基于Freemarker的eclipse plugin代码生成器插件开发
来源:互联网 发布:淘宝上买黄牛票犯法吗 编辑:程序博客网 时间:2024/06/06 08:37
固定类型的软件写多了,里面总是有一些复制粘贴改变类名改变量的基础文件,相似程度非常高。作为一名程序员,坚持不多写一行重复代码的精神,写了一个Eclipse的代码生成器插件。插件通过在xml文件中配置的变量信息及模版位置、目标文件位置信息,直接生成目标文件,减少了大量的重复工作。
1.建立一个plug-in with a popup menu工程,引入freemarker.jar,配置popup menu的对应文件扩展名为.coding.xml
2.先写核心的文档生成代码,保证使用main函数可调用。核心的内容是按照freemarker的要求写好 模版路径、模板文件名、目标文件路径、目标文件名、文件所使用的字符集、以及包含所有数据的map
1 public class CodegenUtil 2 { 3 public static void genFile(String templatePath, String templateFileName, String targetPath, String targetFileName, String charset, Map paramMap) 4 throws IOException, TemplateException 5 { 6 7 File localFile = new File(targetPath, targetFileName); 8 if (!localFile.exists()) { 9 if (!localFile.getParentFile().exists())10 localFile.getParentFile().mkdirs();11 localFile.createNewFile();12 }13 OutputStreamWriter localOutputStreamWriter = new OutputStreamWriter(new FileOutputStream(localFile), charset);14 15 Configuration freemarkerConfigration = new Configuration();16 freemarkerConfigration.setDirectoryForTemplateLoading(new File(templatePath));17 18 Template localTemplate = freemarkerConfigration.getTemplate(templateFileName, charset);19 localTemplate.process(paramMap, localOutputStreamWriter);20 localOutputStreamWriter.close();21 22 }23 }
3.写Action调用
public void run(IAction action) { //读取选定的配置文件 IStructuredSelection selection =(IStructuredSelection) this.selection; ConsoleFactory.printToConsole("--------Start Coding--------", true); for(Object element:selection.toList()){ File file = (File)element; String fullpath = file.getLocationURI().getPath(); Map<String, String> params; List<Map<String,String>> templateMapList=new ArrayList<Map<String,String>>(); try { String configfilepath=fullpath.substring(1); ConsoleFactory.printToConsole("...load coding config "+configfilepath, true); params = XmlUtil.getVars(configfilepath); templateMapList=XmlUtil.getTemplates(configfilepath); for(Map<String ,String > templateMap:templateMapList){ String templateFilePath=templateMap.get(XmlUtil.TEMPLATE_PATH); String templateFileName=templateMap.get(XmlUtil.TEMPLATE_NAME); String targetFilePath=templateMap.get(XmlUtil.TARGET_PATH); String targetFileName=templateMap.get(XmlUtil.TARGET_NAME); ConsoleFactory.printToConsole("... ... coding ... "+targetFilePath+"\\"+targetFileName, true); params.put(XmlUtil.TEMPLATE_PATH, templateFilePath); params.put(XmlUtil.TEMPLATE_NAME, templateFileName); params.put(XmlUtil.TARGET_PATH, targetFilePath); params.put(XmlUtil.TARGET_NAME, targetFileName); String charset=params.get(XmlUtil.CHARSET); CodegenUtil.genFile(templateFilePath,templateFileName,targetFilePath,targetFileName,charset,params); } } catch (Exception e) { e.printStackTrace(); } } ConsoleFactory.printToConsole("--------Finish Coding--------", true); }
4.使用System.out.print所打印的东西在插件的运行环境下是不显示的,ConsoleFactory是我写的一个控制台输出类,调用了Eclipse的控制台,主要用来在使用时给出相关的代码生成提示。
public class ConsoleFactory implements IConsoleFactory { private static MessageConsole console=new MessageConsole("",null); static boolean exists=false; @Override public void openConsole() { showConsole(); } private static void showConsole(){ if(console!=null){ IConsoleManager manager=ConsolePlugin.getDefault().getConsoleManager(); IConsole[] existing = manager.getConsoles(); exists=false; for(int i=0;i<existing.length;i++){ if(console==existing[i]){ exists=true; } } if(!exists){ manager.addConsoles(new IConsole[]{console}); } } } public static void closeConsole(){ IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager(); if(console!=null){ manager.removeConsoles(new IConsole[]{console}); } } public static MessageConsole getConsole(){ showConsole(); return console; } public static void printToConsole(String message , boolean activate){ MessageConsoleStream printer = ConsoleFactory.getConsole().newMessageStream(); printer.setActivateOnWrite(activate); printer.println(message); }}
5.主要内容完毕。使用时先配好xml文件,如下示例。在Eclipse中选中.coding.xml文件,右键菜单 [代码生成器]-->[生成代码]
<?xml version="1.0" encoding="utf-8" ?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <variables> <variable name="developer" value="PennPeng" /> <variable name="charset" value="utf-8" /> <variable name="class" value="CodeGen" /> <variable name="name" value="姓名" /> <variable name="age" value="年龄" /> </variables> <templates> <template> <variable name="templatepath" value="E:\CodeGenTest\template" /> <variable name="templatename" value="a.ftl" /> <variable name="targetpath" value="E:\CodeGenTest" /> <variable name="targetname" value="CodeGen.java" /> </template> <template> <variable name="templatepath" value="E:\CodeGenTest\template" /> <variable name="templatename" value="b.ftl" /> <variable name="targetpath" value="E:\CodeGenTest" /> <variable name="targetname" value="CodeGen2.java" /> </template> </templates></config>
还有很多需要扩展完善的地方,比如数据库的自动支持、各类型文档的生成。
项目地址 https://github.com/buaawp/codegen
0 0
- 基于Freemarker的eclipse plugin代码生成器插件开发
- 基于Freemarker的eclipse plugin代码生成器插件开发
- 基于Freemarker的eclipse plugin代码生成器插件开发
- FreeMarker Eclipse Plugin的安装
- FreeMarker Eclipse Plugin的安装!
- slave4j1.0:Eclipse插件的Java代码生成器发布
- 基于ProGuard-Maven-Plugin的自定义代码混淆插件
- 基于ProGuard-Maven-Plugin的自定义代码混淆插件
- Install FreeMarker Eclipse plugin
- Eclipse插件(Plugin)开发中的NoClassDefFoundError的解决。
- eclipse按装freemarker的插件
- JEECG - 基于代码生成器的J2EE智能开发框架 续二: 代码生成器使用规则
- JEECG v2.3 基于代码生成器的智能开发框架
- 基于代码生成器的J2EE智能开发框架 JEECG
- 对Freemarker eclipse插件代码提示配置
- 对Freemarker eclipse插件代码提示配置
- 基于Mybatis的代码生成器
- Eclipse插件开发之plugin.xml进阶
- App crash on using Fabric
- Android M 权限
- Selenium_IDE的基本使用及脚本解析
- Selenium_RC环境配置
- 《简明Python教程》学习笔记
- 基于Freemarker的eclipse plugin代码生成器插件开发
- C++第一次实验-1
- (七) Freemarker 发送邮件
- 使用C++实现OO观察者模式
- windows 2003 配置多个tomcat需注意的配置点
- 读书笔记-《Linux/Unix设计思想》
- Python网络爬虫(1)--url访问及参数设置
- 读书笔记-《Java核心技术卷I-基础知识》
- ORACLE中 %TYPE 和 %ROWTYPE 的使用