ITeye博客

BeanGenerator_5

flybird1985 · 2012-03-09 16:58 · java相关技术文档 · 编程语言 · Markdown

	/**
	 * Step 3:根据“内容”和“输出路径”导出文件
	 * @param content 内容
	 * @param outputPath 输出路径
	 */
	private void output(String content, String outputBasePath) {
		PrintWriter printWriter = null;
		try {
			printWriter = new PrintWriter(new BufferedWriter(new FileWriter(
					outputBasePath)));
			printWriter.println(content);
			printWriter.flush();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			printWriter.close();
		}
	}

	public void generate() {
		String templatePath = "";
		String result = "";
		String fileName = "";
		importBeans();
		for (int i = 0; i < templatePaths.length; i++) {
			templatePath = templatePaths[i];
			if (Util.isNull(templatePath))
				continue;
			for (Bean bean : beans) {
				result = importTemplate(templatePath, bean);
				if (templatePath.indexOf(".java") > 0)
					fileName = bean.getName()
							+ templatePath.substring(templatePath
									.lastIndexOf("."));
				else
					fileName = Util.one2LowerCase(bean.getName())
							+ templatePath.substring(templatePath
									.lastIndexOf("."));
				output(result, outputBasePath + fileName);
			}
		}
	}
}

 bean.java

for (Parameter parameter : parameters) {
			this.setA(this.getA()+"this."+parameter.getName()+"="+parameter.getName()+";\n");
			this.setB(this.getB()+parameter.getClazz()+" "+parameter.getName()+",");
			this.setC(this.getC()+"+\""+parameter.getName()+": \"+this."+parameter.getName()+"\n");
			this.setD(this.getD()+"@param "+parameter.getName()+": "+parameter.getClazz()+" "+parameter.getNote()+"\n * ");
		}
		
		this.setA(this.getA().substring(0,this.getA().length()-1));
		this.setB(this.getB().substring(0,this.getB().length()-1));
		this.setC(this.getC().substring(0,this.getC().length()-1));
		this.setC("\"{\""+this.getC()+"+\"}\"");
		this.setD(this.getD().substring(0,this.getD().length()-4));

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。