一个简单的类代码生成器
2024-07-21 02:14:11
供稿:网友
花了一点点时间,写了一个类玩玩,希望有点用。
类是怎么用的呢?
比如:输入:
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.
本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。