一个简单的类代码生成器

发表于:2007-07-04来源:作者:点击数: 标签:
类是怎么用的呢? 比如:输入: public className; private String variable1; private Integer variable2; private Double variable3; 点击 Generate!,后,输出: public className { private String variable1; private Integer variable2; private Doubl


类是怎么用的呢?

比如:输入:


public className;
private String variable1;
private Integer variable2;
private Double variable3;



点击 Generate!,后,输出:


public className {
private String variable1;
private Integer variable2;
private Double variable3;
public String getVariable1()
{
return variable1;
}
public void setVariable1(String variable1)
{
this.variable1 = variable1;
}
public Integer getVariable2()
{
return variable2;
}
public void setVariable2(Integer variable2)
{
this.variable2 = variable2;
}
public Double getVariable3()
{
return variable3;
}
public void setVariable3(Double variable3)
{
this.variable3 = variable3;
}
}





哈哈,大家就知道它的用处了吧

源代码:

ClassGen.java com.jrock.util



package com.jrock.util;
/**
* 输入的字符串格式为
* 限定符 类名;
* 变量;变量;变量;...
* 变量格式为{限定符 数据类型 变量名}
* 其中: 限定符 (private/...) 不能省
* 类名 不能省
* 数据类型 (Integer/...) 不能省
* 变量名 不能省
* @version 1.0
*/
public class ClassGen {
public ClassGen() {
}
//去掉多余的空格
private static void dismissBlank(String[] array)
{
int len= array.length;
boolean over =false;
while(!over) {
over=true;
for (int i = 0; i < len; i++) {
if (array[i].equals("")) {
for (int j = i; j < len - 1; j++) {
array[j] = array[j + 1];
}
len--;over=false;
}
}
}
}
public static String outGetter(String parStr)
{
String[] array = parStr.split(" ");
if(array.length<3) return null;
dismissBlank(array);
String type = array[1];
String par = array[2];
StringBuffer result = new StringBuffer();
result.append("public " + type + " get" + par.substring(0,1).toUpperCase() + par.substring(1));
result.append("()\n{\n\t return " + par + ";\n}\n");
return result.toString();
}
public static String outSetter(String parStr)
{
String[] array = parStr.split(" ");
if(array.length<3) return null;
dismissBlank(array);
String type = array[1];
String par = array[2];
StringBuffer result = new StringBuffer();
result.append("public void set" + par.substring(0,1).toUpperCase() + par.substring(1));
result.append("(" + type + " " + par + ")\n{\n\t this."+ par + " = " + par + ";\n}");
return result.toString();
}
//获得函数的主体
public static String getClassContent(String in)
{
if(in==null) return "";
in=in.replace(''\n'','' '').trim();
if(in.equals("")) return "";
StringBuffer result = new StringBuffer();
String [] parArray = in.split(";");
if(parArray.length>0) {
//输出类名
result.append(parArray[0] + " {\n");
//输出变量
for (int i = 1; i < parArray.length; i++) {
result.append(parArray[i] + ";\n");
}
//输出setter和getter
for (int i = 1; i < parArray.length; i++) {
result.append(outGetter(parArray[i]) + "\n");
result.append(outSetter(parArray[i]) + "\n");
}
result.append("}");
}
return result.toString();
}
//测试用的
public static void main(String[] args) {
String inStr = "public className;\n" +
"private String variable1;\n" +
"private Integer variable2;\n" +
"private Double variable3;\n";
System.out.println(getClassContent(inStr));
}
}





GUI:

ClassGenApplication.java com.jrock.util



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.jrock.util.ClassGen;
public class ClassGenApplication {
public Component createComponents() {
String inStr = "public className;\n" +
"private String variable1;\n" +
"private Integer variable2;\n" +
"private Double variable3;";
final JTextArea text1 = new JTextArea(inStr,20,40);
final JTextArea text2 = new JTextArea("",20,50);
text1.setFont(new java.awt.Font("Arial",java.awt.Font.PLAIN,20));
text2.setFont(new java.awt.Font("Arial",java.awt.Font.PLAIN,12));
// Create a scrollable text area
JScrollPane sp1 = new JScrollPane(text1);
JScrollPane sp2 = new JScrollPane(text2);
JButton button = new JButton("Generate!");
button.setMnemonic(KeyEvent.VK_I);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text2.setText(
ClassGen.getClassContent(text1.getText()));
}
});
JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createEmptyBorder(
20, //top
20, //left
200, //bottom
200) //right
);
pane.setLayout(new GridLayout(0, 1));
pane.add(sp1);
pane.add(button);
pane.add(sp2);
return pane;
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) { }
//Create the top-level container and add contents to it.
JFrame frame = new JFrame("类生成工具");
ClassGenApplication app = new ClassGenApplication();
Component contents = app.createComponents();
frame.getContentPane().add(contents, BorderLayout.CENTER);
//Finish setting up the frame, and show it.
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}





Thanks.



原文转自:http://www.ltesting.net