程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 用例子講解FreeMarker應用—生成Java代碼

用例子講解FreeMarker應用—生成Java代碼

編輯:關於JAVA

對FreeMarker而言,無非是“Template + data-model = output”。

這裡,假設我們所要生成的Java代碼,即“output”如下:

package com.cs.qdog.swift.objects;

public class F32B {

private Double amount;

private String currency;

public Double getAmount() {

return amount;

}

public void setAmount(Double amount) {

this.amount = amount;

}

public String getCurrency() {

return currency;

}

public void setCurrency(String currency) {

this.currency = currency;

}

}

其中,類名、屬性都屬於“data-model”,其可用樹形結構表現如下:

(root)

|

+- class = “F32B”

|

|- propertIEs

| |

| +- currency

| | |

| | +- name = “currency”

| | |

| | +- type = “String”

| |

| +- amount

| | |

| | +- name = “amount”

| | |

| | +- type = “Double”

那麼,則可使用如下“Template”:

package com.cs.qdog.swift.objects;

public class ${class} {

<#list propertIEs as prop>

private ${prop.type} ${prop.name};

<#list propertIEs as prop>

public ${prop.type} get${prop.name?cap_first}(){

return ${prop.name};

}

public void set${prop.name?cap_first}(${prop.type} ${prop.name}){

this.${prop.name} = ${prop.name};

}

}

最後,將FreeMarker文檔中的例子改動一下,測試生成“output”的Java程序如下:

package com.cs.qdog.swift.objects;

import Java.io.File;

import Java.io.IOException;

import Java.io.OutputStreamWriter;

import Java.io.Writer;

import Java.util.Collection;

import Java.util.HashMap;

import Java.util.HashSet;

import Java.util.Map;

import freemarker.template.Configuration;

import freemarker.template.DefaultObjectWrapper;

import freemarker.template.Template;

import freemarker.template.TemplateException;

public class GenObjects {

public static void main(String[] args) throws IOException,

TemplateException {

/* ------------------------------------------------------------------- */

/* You usually do it only once in the whole application life-cycle: */

/* Create and adjust the configuration */

Configuration cfg = new Configuration();

cfg.setDirectoryForTemplateLoading(new File(

"D:/Temp/EclipseWorkSpace/GenSwiftFIElds/templates"));

cfg.setObjectWrapper(new DefaultObjectWrapper());

/* ------------------------------------------------------------------- */

/* You usually do these for many times in the application life-cycle: */

/* Get or create a template */

Template temp = cfg.getTemplate("SwiftFIEldClass.ftl");

/* Create a data-model */

Map root = new HashMap();

root.put("class", "F32B");

Collection> propertIEs = new HashSet>();

root.put("properties", propertIEs);

/* subfIEld 1: currency */

Map currency = new HashMap();

currency.put("name", "currency");

currency.put("type", "String");

propertIEs.add(currency);

/* subfIEld 2: amount */

Map amount = new HashMap();

amount.put("name", "amount");

amount.put("type", "Double");

propertIEs.add(amount);

/* Merge data-model with template */

Writer out = new OutputStreamWriter(System.out);

temp.process(root, out);

out.flush();

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved