Java Bean 生成器__教程 |
|
日期:2007-5-20 1:15:13 人气:61 [大 中 小] |
|
|
|
ArrayList l = (ArrayList) beans.get(key); buf.append("public class "); buf.append(key); buf.append(" {\r\n\n");
// add properties for (int i = 0; i < l.size(); i++) { buf.append("\tprivate "); buf.append((String) l.get(i)); buf.append(";\r\n"); } buf.append("\r\n");
// add getter and setter for (int i = 0; i < l.size(); i++) { // getter String item = (String) l.get(i); // like "int a" String type = item.split("\\s+", 2)[0]; // tok needed!!! String var = item.split("\\s+", 2)[1]; buf.append("\tpublic "+ type + " get"); buf.append(upperFirst(var)); buf.append("() {\r\n\t\treturn this."); buf.append(var); buf.append(";\r\n\t}\n");
//setter buf.append("\tpublic void set"); buf.append(upperFirst(var)); buf.append("(" + type + " " + var + ") {\r\n\t\tthis."); buf.append(var); buf.append(" = "); buf.append(var); buf.append(";\r\n\t}\n"); } buf.append("}\r\n");
//dump to a named file try{ pw = new PrintWriter( new BufferedWriter( new FileWriter(key+".java"))); pw.write(buf.toString()); pw.close();
}catch(IOException e){ System.out.println("create java file failed!"); e.printStackTrace(); }
}
}
/********************************/
public static void main(String[] args) throws Exception {
if(args.length<1){ System.out.println("usage:"); System.out.println("\tjava CreateBean yourInputFileName"); System.out.println("where your input file should like this:"); System.out.println("#myBean1"); System.out.println("int field1"); System.out.println("long field2"); System.out.println("String filed3"); System.out.println("...\n"); System.out.println("#myBean2"); System.out.println("char field1"); System.out.println("Object field2"); System.out.println("..."); System.out.println("\n..."); return; } System.out.println("begin ..."); CreateBean cb = new CreateBean(args[0]); cb.generate(); System.out.println("complete.");
}
} |
|
出处:本站原创 作者:佚名 |
|
|