Java Bean 生成器

类别:Java 点击:0 评论:0 推荐:

这是一个java bean生成器,利用一个文本文件(不妨称为bean定义文件)生成java bean。

开发过程中我们可能会用到很多数据对象(值对象),大都被封装成一个bean。虽然各大

IDE都提供了方便生成这种数据bean的工具,但都是一步一步填对话框式的,不像这个,

这个可以在一个文件里定义多个Bean,一次生成所有bean。当然它也有局限性,目前只会

简单的生成private数据域和public 的setter、getter;并且不会替你导入必要的包。但尽管如此,

也能大大方便数据bean比较多,或bean字段比较多的应用。

bean定义文件(.bd)格式如下:

// 这是注释,以"//"打头。
// 用"#"表示bean名称。
// 注释不能和bean名称或数据字段写在同一行。
#MyBean1
int i
long l
String s

#MyBean2
String user
String pwd

#MyBean3
Object o
...

...

import java.io.*;
import java.util.*;


public class CreateBean {

 private BufferedReader in;
 private String inputFile;
 private HashMap beans = new HashMap();
 private static final char beanMark = '#';

 public CreateBean(String inputFileName) {
  inputFile = inputFileName;
  try {
   in = new BufferedReader(new FileReader(inputFileName));
  } catch (FileNotFoundException e) {
   System.out.println("file " + inputFileName + " not found!");
   e.printStackTrace();
  }
 }

 public void generate() {
  String ln = null;
  String beanName = null;
  ArrayList properties = new ArrayList();

  try {
   ln = in.readLine().trim(); // throw io excption

   while (ln != null) {
    if(
     ( ln.length() < 1                                      ) ||
     ( ln.startsWith("//") )                                  || // comments
     ( ln.charAt(0)!=beanMark && ln.split("\\s+").length!=2 )
    ){}
    else if (ln.charAt(0) == beanMark) {
     if (beanName != null) {
      beans.put(beanName, properties); // save last bean to hashMap
     }

     properties = new ArrayList();
     
     // gen new bean name
     beanName = upperFirst(ln.substring(1));
     System.out.println("bean: " + beanName);
    }
    else {
     System.out.println("\t " + ln);
     properties.add(ln);
    }
    
    ln = in.readLine();
    if(ln==null){ // last bean
     if (beanName != null) {
      beans.put(beanName, properties);
     }
    }
    else{
     ln = ln.trim();
    }
   } //while
   
   System.out.println( beans.size() + " beans tested ...");
   dumpBeans();

  } catch (IOException e) {
   System.out.println("read file failed!");
  } finally {
   try {
    if (in != null)
     in.close();
   } catch (IOException e2) {
    System.out.println("close input file failed!");
    e2.printStackTrace();
   }
  }

 }

 public static String upperFirst(String s) {
  StringBuffer buf = new StringBuffer(s);
  if (buf.charAt(0) >= 'a' && buf.charAt(0) <= 'z') {
   buf.setCharAt(0, (char)(buf.charAt(0) - 32));
  }
  return buf.toString();
 }

 private void dumpBeans() {
  System.out.println("creating ...");
  
  StringBuffer buf = null;
  String key = null;
  PrintWriter pw = null;
  Iterator itr = null;
  Set keySet = beans.keySet();
  
  if(keySet!=null)
   itr = keySet.iterator();
  
  while (itr.hasNext()) {
   buf = new StringBuffer();
   key = (String) itr.next();
   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.");

 }

}

本文地址:http://com.8s8s.com/it/it10559.htm