关于java代码生成器---下篇之实例

原创
2010/05/19 20:15
阅读数 8.5K

项目准备:先去down个freemarker.jar包,  http://freemarker.org/freemarkerdownload.html

上篇讨论了代码生成器的原理,输出=模板+数据,那么现在就生成一个Student.java文件做个简单例子。

首先先写出模板,先解决一个问题,上篇有讲到属性名首字母大写的问题

由于freemarker中不支持将首字母大写(属性名中用到),那么自己先写一个自定义宏如下:

package com;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

public class UpperFirstCharacter implements TemplateDirectiveModel {

    public void execute(Environment env,
            Map params, TemplateModel[] loopVars,
            TemplateDirectiveBody body)
            throws TemplateException, IOException {
        // Check if no parameters were given:
        if (!params.isEmpty()) {
            throw new TemplateModelException(
                    "This directive doesn't allow parameters.");
        }
        if (loopVars.length != 0) {
                throw new TemplateModelException(
                    "This directive doesn't allow loop variables.");
        }
        
        // If there is non-empty nested content:
        if (body != null) {
            // Executes the nested body. Same as <#nested> in FTL, except
            // that we use our own writer instead of the current output writer.
            body.render(new UpperCaseFilterWriter(env.getOut()));
        } else {
            throw new RuntimeException("missing body");
        }
    }
    
    /**
     * A {@link Writer} that transforms the character stream to upper case
     * and forwards it to another {@link Writer}.
     */ 
    private static class UpperCaseFilterWriter extends Writer {
       
        private final Writer out;
           
        UpperCaseFilterWriter (Writer out) {
            this.out = out;
        }

        public void write(char[] cbuf, int off, int len)
                throws IOException {
//            char[] transformedCbuf = new char[len];
//            for (int i = 0; i < len; i++) {
//                transformedCbuf[i] = Character.toUpperCase(cbuf[i + off]);
//            }
//            out.write(transformedCbuf);
        	cbuf[0] = Character.toUpperCase(cbuf[0]);
        	out.write(String.valueOf(cbuf).trim());///把右边空格去掉
        }

        public void flush() throws IOException {
            out.flush();
        }

        public void close() throws IOException {
            out.close();
        }
    }

}

 

下面呢就可以编写模板了,代码如下:

package ${package};

//这个地方可以事先定义好需要的类
import java.util.Date;

import java.io.Serializable;

public class ${className} implements Serializable{
<#list properties as pro>
	private ${pro.proType} ${pro.proName};
</#list>
	
<#list properties as pro>
	public void set<@upperFC>${pro.proName}</@upperFC>(${pro.proType} ${pro.proName}){
		this.${pro.proName}=${pro.proName};
	}
	
	public ${pro.proType} get<@upperFC>${pro.proName}</@upperFC>(){
		return this.${pro.proName};
	}
	
</#list>
}

模板文件取名为javabean.html,在com包下

下面编写测试类:

package com;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//System.out.println(System.getProperty("user.dir")+"============");
		Configuration cfg = new Configuration();
		try {
        	cfg.setClassForTemplateLoading(Test.class, "/com");//指定模板所在的classpath目录
        	cfg.setSharedVariable("upperFC", new UpperFirstCharacter());//添加一个"宏"共享变量用来将属性名首字母大写
			Template t = cfg.getTemplate("javabean.html");//指定模板
			FileOutputStream fos = new FileOutputStream(new File("d:/Student.java"));//java文件的生成目录
			
			//模拟数据源
			Map data = new HashMap();
			data.put("package", "edu");//包名
			data.put("className", "Student");
			
			List pros = new ArrayList();
			Map pro_1 = new HashMap();
			pro_1.put("proType", String.class.getSimpleName());
			pro_1.put("proName", "name");
			pros.add(pro_1);
			
			Map pro_2 = new HashMap();
			pro_2.put("proType", String.class.getSimpleName());
			pro_2.put("proName", "sex");
			pros.add(pro_2);
			
			Map pro_3 = new HashMap();
			pro_3.put("proType", Integer.class.getSimpleName());
			pro_3.put("proName", "age");
			pros.add(pro_3);
			
			data.put("properties", pros);
			t.process(data, new OutputStreamWriter(fos,"utf-8"));//
			fos.flush();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}

}

 

运行一下测试类,在D盘可以找到一个Student.java的文件,打开看看对不对

展开阅读全文
打赏
0
10 收藏
分享
加载中
我运行了出错:
2012-10-10 9:15:26 freemarker.log.JDK14LoggerFactory$JDK14Logger error
严重:

on line 14, column 24 in javabean.html: upperFC is not a Macro or Transform.
The problematic instruction:
----------
==> user-directive upperFC [on line 14, column 24 in javabean.html]
----------

Java backtrace for programmers:
----------
freemarker.template.TemplateException: on line 14, column 24 in javabean.html: upperFC is not a Macro or Transform.
  at freemarker.core.UnifiedCall.accept(UnifiedCall.java:140)
  at freemarker.core.Environment.visit(Environment.java:196)
  at freemarker.core.MixedContent.accept(MixedContent.java:92)
  at freemarker.core.Environment.visit(Environment.java:196)
  at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:172)
  at freemarker.core.Environment.visit(Environment.java:351)
  at freemarker.core.IteratorBlock.accept(IteratorBlock.java:95)
  at freemarker.core.Environment.visit(Environment.java:196)
  at freemarker.core.MixedContent.accept(MixedContent.java:92)
  at freemarker.core.Environment.visit(Environment.java:196)
  at freemarker.core.Environment.process(Environment.java:176)
  at freemarker.template.Template.process(Template.java:232)
  at com.Test.main(Test.java:51)

on line 14, column 24 in javabean.html: upperFC is not a Macro or Transform.
The problematic instruction:
----------
==> user-directive upperFC [on line 14, column 24 in javabean.html]
----------

Java backtrace for programmers:
----------
freemarker.template.TemplateException: on line 14, column 24 in javabean.html: upperFC is not a Macro or Transform.
  at freemarker.core.UnifiedCall.accept(UnifiedCall.java:140)
  at freemarker.core.Environment.visit(Environment.java:196)
  at freemarker.core.MixedContent.accept(MixedContent.java:92)
  at freemarker.core.Environment.visit(Environment.java:196)
  at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:172)
  at freemarker.core.Environment.visit(Environment.java:351)
  at freemarker.core.IteratorBlock.accept(IteratorBlock.java:95)
  at freemarker.core.Environment.visit(Environment.java:196)
  at freemarker.core.MixedContent.accept(MixedContent.java:92)
  at freemarker.core.Environment.visit(Environment.java:196)
  at freemarker.core.Environment.process(Environment.java:176)
  at freemarker.template.Template.process(Template.java:232)
  at com.Test.main(Test.java:51)
2012/10/10 09:20
回复
举报
更多评论
打赏
1 评论
10 收藏
0
分享
返回顶部
顶部