日期:2014-05-20  浏览次数:20852 次

java文件如何自动生成的?
一些工具可以生成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

------解决方案--------------------
myeclipse下可以,点击右键->source->Generate Getters and Setters
------解决方案--------------------
自动生成代码有什么问题?如果是像
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都要程序自己生成。总的来说楼主的项目相当复杂
------解决方案--------------------
用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及其依赖包
------解决方案--------------------
上面的2个有几处笔误.
Java code
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"));

    }
}