一些工具可以生成java文件,其中有大量的代码,请问如何编程实现?
例如:
 用户通过界面输入: name,我想自动生成java文件如下:
public class a
{
  String name;
  public void setName(String a)
  {
    name=a;
  }
  
  public void getName(String b)
  {
    name=b;
  }
}
请问如何实现?最好能给出进一步编译,生成Web Service的代码?多谢!!!!!!!!!!1

解决方案 »

  1.   

    qianxf2008兄弟,给点解释吧,最好能给个例子。谢谢。
      

  2.   

    qianxf2008兄弟,给点解释吧,最好能给个例子。谢谢。
      

  3.   

    qianxf2008兄弟,给点解释吧,最好能给个例子。谢谢。
      

  4.   

    qianxf2008兄弟,给点解释吧,最好能给个例子。谢谢。
      

  5.   

    qianxf2008兄弟,给点解释吧,最好能给个例子。谢谢。
      

  6.   

    qianxf2008兄弟,给点解释吧,最好能给个例子。谢谢。
      

  7.   

    qianxf2008兄弟,给点解释吧,最好能给个例子。谢谢。
      

  8.   

    qianxf2008兄弟,给点解释吧,最好能给个例子。谢谢。
      

  9.   

     学习
    不过这些在Eclipse上都会自动生成吧
    可找下Eclipse的代码怎实现的
      

  10.   

    myeclipse下可以,点击右键->source->Generate Getters and Setters
      

  11.   

    是想让程序自动实现,也就是说做一个类似二次开发的东西,
    如用户在界面上的文本框中输入相应的变量名a,就生成以下的java文件:
    public   class   a 

        String   name; 
        public   void   setName(String   a) 
        { 
            name=a; 
        } 
        
        public   void   getName(String   b) 
        { 
            name=b; 
        } 

    是要自己的程序有这个功能,不是让eclipse做事情.
      

  12.   

    自动生成代码有什么问题?如果是像
    public       class       a   
    {   
            String       name;   
            public       void       setName(String       a)   
            {   
                    name=a;   
            }   
            
            public       void       getName(String       b)   
            {   
                    name=b;   
            }   
    }
    简单的代码是没有问题的如果问题比较复杂的话建议楼主看看Java虚拟机规范.
    编译代码可以用jdk1.6的java complier api或者使用asm(http://asm.objectweb.org/)直接生成bytecode
    生成web service就比较复杂了,那要对web service很熟悉了,因为xml都要程序自己生成。总的来说楼主的项目相当复杂
      

  13.   

    palm_civet 兄弟,说的没错,我的项目好麻烦.那暂时先不考虑自动生成web service了,
    能否给一个程序自动生成java文件的例子.谢谢先
      

  14.   

    用velocity模板包生成1、建一个模板
    CREATE TABLE A_20$date
     (PACK_ID         INTEGER         NOT NULL,
      CARD_GROUP      SMALLINT,
      CARD_BIN        VARCHAR(6),
      CARD_NBR        VARCHAR(20),
      BILL_DATE       VARCHAR(10),
      ZIP_CODE        VARCHAR(6)                DEFAULT NULL,
      CUST_NAME       VARCHAR(30)               DEFAULT NULL
    )2、写一个工具类
    import java.io.BufferedWriter;
    import java.io.FileWriter;import org.apache.velocity.Template;
    import org.apache.velocity.VelocityContext;
    import org.apache.velocity.app.Velocity;
    public class VelocityUtil {

    public static boolean builder(String vmFile,String tarFile,
    VelocityContext context,boolean append){
    return builder(vmFile,tarFile,context,"gbk",append);
    }

    public static boolean builder(String vmFile,String tarFile,
    VelocityContext context,String encode,boolean append){

    try {
    Velocity.init();
    Template template = Velocity.getTemplate(vmFile,encode);
    BufferedWriter writer = new BufferedWriter(new FileWriter(tarFile,append));
    if (template != null)
    template.merge(context, writer);
    writer.flush();
    writer.close();
    } catch (Exception e) {
    e.printStackTrace();
    return false;
    }

    return true;
    }
    }3、生成类
    public class MainSqlBuilder {
    public static void main(String[] args) {
    createMain();
    // createAlterMain();
    // createAlterMonthMain();

    System.out.println("end");
    }

    private static void createMain(){
    String vmFile = "main_sql.vm";

    Date startDate = DateUtil.getDate(2007, 11, 19);
    Date endDate = DateUtil.getDate(2008, 1, 1);

    Date curDate = startDate;
    while( curDate.before(endDate) ){
    String strDate = DateUtil.format(curDate, "yyMMdd");

    VelocityContext context = new VelocityContext();
    context.put("date", strDate);
    String tarFile = "E:/my_work/test/main_sql/main_"+strDate+".sql";
    VelocityUtil.builder(vmFile, tarFile, context,"utf-8",false);

    curDate = DateUtil.addDay(curDate, 1);
    }
    }
    }以上是一个生成批量表sql文件的例子,jar包:volecity.jar及其依赖包
      

  15.   

    class Code { private static String attribute = "", setter = "", getter = ""; public static String createAttribute(String type, String name) {
    attribute += "private " + type + " " + name + ";\n";
    return attribute;
    } public static String createSetter(String type, String name) {
    setter += "\tpublic " + type + "  set"
    + name.substring(0, 1).toUpperCase()
    + name.substring(1, name.length()) + "(" + type + " " + name
    + "){\n\t\tthis." + name + " = " + name + ";\n\t}\n";
    return setter;
    } public static String createGetter(String type, String name) {
    getter += "\tpublic " + type + "  get"
    + name.substring(0, 1).toUpperCase()
    + name.substring(1, name.length()) + "(){\n\t\treturn a;\n\t}";
    return getter;
    } public static String createCode(String type, String name) {
    return createAttribute(type, name) + createSetter(type, name)
    + createGetter(type, name);
    } public static void main(String[] args) { System.out.println(createCode("int", "count"));
    }
    }
      

  16.   

    class Code { private static String attribute = "", setter = "", getter = ""; public static String createAttribute(String type, String name) {
    attribute = "private " + type + " " + name + ";\n";
    return attribute;
    } public static String createSetter(String type, String name) {
    setter = "\tpublic " + type + "  set"
    + name.substring(0, 1).toUpperCase()
    + name.substring(1, name.length()) + "(" + type + " " + name
    + "){\n\t\tthis." + name + " = " + name + ";\n\t}\n";
    return setter;
    } public static String createGetter(String type, String name) {
    getter = "\tpublic " + type + "  get"
    + name.substring(0, 1).toUpperCase()
    + name.substring(1, name.length())
    + "(){\n\t\treturn a;\n\t}\n";
    return getter;
    } public static String createCode(String type, String name) {
    return createAttribute(type, name) + createSetter(type, name)
    + createGetter(type, name);
    } public static void main(String[] args) { System.out.println(createCode("int", "count")
    + createCode("String", "temp")); }
    }
      

  17.   

    上面的2个有几处笔误.
    class Code { private static String attribute = "", setter = "", getter = ""; public static String createAttribute(String type, String name) {
    attribute = "private " + type + " " + name + ";\n";
    return attribute;
    } public static String createSetter(String type, String name) {
    setter = "\tpublic " + type + "  set"
    + name.substring(0, 1).toUpperCase()
    + name.substring(1, name.length()) + "(" + type + " " + name
    + "){\n\t\tthis." + name + " = " + name + ";\n\t}\n";
    return setter;
    } public static String createGetter(String type, String name) {
    getter = "\tpublic " + type + "  get"
    + name.substring(0, 1).toUpperCase()
    + name.substring(1, name.length()) + "(){\n\t\treturn " + name
    + ";\n\t}\n";
    return getter;
    } public static String createCode(String type, String name) {
    return createAttribute(type, name) + createSetter(type, name)
    + createGetter(type, name);
    } public static void main(String[] args) { System.out.println(createCode("int", "count")
    + createCode("String", "temp")); }
    }
    这里的效果,格式LZ自己调整.
    private int count;
    public int  setCount(int count){
    this.count = count;
    }
    public int  getCount(){
    return count;
    }
    private String temp;
    public String  setTemp(String temp){
    this.temp = temp;
    }
    public String  getTemp(){
    return temp;
    }
      

  18.   

    这个只是简单的生成javabean
    LZ其实按照规则慢慢写是没问题的
    就是格式比较麻烦 总得调试
      

  19.   

    source->generate getters and setters