JavaBean的get、set方法生成器
來(lái)源:http://www.crazyit.org/redirect.php?tid=3779&goto=lastpost
?
BeanGenerator.java
package codegen;??
import java.io.File;? ?
import java.io.BufferedReader;? ?
import java.io.FileReader;? ?
import java.io.RandomAccessFile;? ?
import java.util.List;? ?
import java.util.ArrayList;? ?
??
public class BeanGenerator? ??
{? ?
? ? //存放Bean的類名? ?
? ? private String className;? ?
? ? //接收生成Bean的文件? ?
? ? private File file;? ?
? ? //Bean屬性和其屬性對(duì)應(yīng)的類型的元數(shù)據(jù)List? ?
? ? private List<BeanInfo> bList = new ArrayList<BeanInfo>();? ?
? ? //寫入流? ?
? ? private RandomAccessFile raf = null;? ?
? ? //構(gòu)造方法的參數(shù)字符串? ?
? ? private StringBuffer paramsBuffer = new StringBuffer();? ?
? ? //用來(lái)標(biāo)識(shí)構(gòu)造器參數(shù)何時(shí)加上","? ?
? ? private boolean bFirst = true;? ?
??
? ? //解析Bean的屬性和其屬性對(duì)應(yīng)的類型? ?
? ? private void parse(File file) throws Exception? ?
? ? {? ?
? ?? ???//保存解析的Bean文件? ?
? ?? ???this.file = file;? ?
? ?? ???//解析出類名? ?
? ?? ???this.className = file.getName().substring(0, file.getName().indexOf("."));? ?
? ?? ???//讀取Bean文件? ?
? ?? ???BufferedReader br = new BufferedReader(new FileReader(file));? ?
? ?? ???String strLine = null;? ?
? ?? ???//逐行讀取文件內(nèi)容? ?
? ?? ???while ((strLine = br.readLine()) != null)? ??
? ?? ???{? ?
? ?? ?? ?? ?//當(dāng)行內(nèi)容同時(shí)包含private關(guān)鍵字和";"號(hào)時(shí)則解析該行字符串? ?
? ?? ?? ?? ?if (strLine.contains("private") && strLine.contains(";"))? ??
? ?? ?? ?? ?{? ?
? ?? ?? ?? ?? ? //獲取類型字符串第一個(gè)字符的起始位置? ?
? ?? ?? ?? ?? ? Integer leftLoc = strLine.indexOf("private ") + "private ".length();? ?
? ?? ?? ?? ?? ? //獲取屬性名最后一個(gè)字符的位置? ?
? ?? ?? ?? ?? ? Integer rightLoc = strLine.indexOf(";");? ?
? ?? ?? ?? ?? ? //解析出屬性類型和屬性名,數(shù)組[0]為屬性類型,數(shù)組[1]為屬性名? ?
? ?? ?? ?? ?? ? String[] tmpStr = strLine.substring(leftLoc, rightLoc).split(" ");? ?
? ?? ?? ?? ?? ? //裝Bean元數(shù)據(jù)封裝并放入集合中? ?
? ?? ?? ?? ?? ? bList.add(new BeanInfo(tmpStr[0], tmpStr[1]));? ?
? ?? ?? ?? ?}? ?
? ?? ???}? ???
? ? }? ?
??
? ? //生成Bean的get、set方法和構(gòu)造方法? ?
? ? private void generateSetGetConstructor() throws Exception? ?
? ? {? ?? ???
? ?? ???raf = new RandomAccessFile(file, "rw");? ?
? ?? ???raf.seek(file.length() - 3);? ?
? ?? ???raf.writeBytes("/r/n");? ?
??
? ?? ???//--------------------生成Bean的構(gòu)造方法-------------------? ?
? ?? ???for (BeanInfo beanInfo : bList)? ??
? ?? ???{? ?
? ?? ?? ?? ?if (bFirst)? ??
? ?? ?? ?? ?{? ?
? ?? ?? ?? ?? ? //第一次構(gòu)造方法的參數(shù)后不加","? ?
? ?? ?? ?? ?? ? paramsBuffer.append(beanInfo.getType() + " "? ??
? ?? ?? ?? ?? ?? ???+ beanInfo.getAttr());? ?
? ?? ?? ?? ?? ? bFirst = false;? ?
? ?? ?? ?? ?}? ?
? ?? ?? ?? ?else? ??
? ?? ?? ?? ?{? ?
? ?? ?? ?? ?? ? //當(dāng)參數(shù)列表過(guò)長(zhǎng)時(shí)自動(dòng)換行? ?
? ?? ?? ?? ?? ? if (paramsBuffer.length() >= 40)? ??
? ?? ?? ?? ?? ? {? ?
? ?? ?? ?? ?? ?? ???paramsBuffer.append("/r/n/t/t" + ", " + beanInfo.getType()? ??
? ?? ?? ?? ?? ?? ?? ?? ?+ " " + beanInfo.getAttr());? ?
? ?? ?? ?? ?? ? }? ?
? ?? ?? ?? ?? ? else? ??
? ?? ?? ?? ?? ? {? ?
? ?? ?? ?? ?? ?? ???paramsBuffer.append(", " + beanInfo.getType()? ??
? ?? ?? ?? ?? ?? ?? ?? ?+ " " + beanInfo.getAttr());? ?
? ?? ?? ?? ?? ? }? ?? ?
? ?? ?? ?? ?}? ?
? ?? ???}? ?
??
? ?? ???//避免中文問(wèn)題輸出時(shí)是亂碼? ?
? ?? ???raf.write("/t//無(wú)參數(shù)構(gòu)造器/r/n".getBytes());? ?
? ?? ???//無(wú)參數(shù)的構(gòu)造器? ?
? ?? ???raf.writeBytes("/tpublic " + className + "()/r/n/t{/r/n/t}/r/n");? ?
??
? ?? ???//增加注釋? ?
? ?? ???raf.write("/t//初始化全部屬性的構(gòu)造器/r/n".getBytes());? ?
? ?? ???raf.writeBytes("/tpublic " + className + "("? ??
? ?? ?? ?? ?+ paramsBuffer.toString() + ")" + "/r/n/t{/r/n");? ?
? ?? ???for (BeanInfo beanInfo : bList)? ??
? ?? ???{? ?
? ?? ?? ?? ?raf.writeBytes("/t/t");? ?
? ?? ?? ?? ?raf.writeBytes("this." + beanInfo.getAttr()? ??
? ?? ?? ?? ?? ? + " = " + beanInfo.getAttr() + ";");? ?
? ?? ?? ?? ?raf.writeBytes("/r/n");? ?
? ?? ???}? ?
? ?? ???raf.writeBytes("/t}/r/n");? ?
? ?? ???//----------------------------------------------------------? ?
??
? ?? ???for (BeanInfo beanInfo : bList)? ??
? ?? ???{? ?? ?
? ?? ?? ?? ?//增加注釋? ?
? ?? ?? ?? ?raf.writeBytes("/t//" + beanInfo.getAttr());? ?? ?
? ?? ?? ?? ?raf.write("屬性的setter和getter方法".getBytes());? ?
? ?? ?? ?? ?raf.writeBytes("/r/n");? ?
? ?? ?? ?? ?//--------------------生成Bean的set方法---------------------? ?
? ?? ?? ?? ?raf.writeBytes("/tpublic void set"? ??
? ?? ?? ?? ?? ? + beanInfo.getAttr().substring(0, 1).toUpperCase()? ??
? ?? ?? ?? ?? ? + beanInfo.getAttr().substring(1)? ??
? ?? ?? ?? ?? ? + "(" + beanInfo.getType() + " " + beanInfo.getAttr()? ??
? ?? ?? ?? ?? ? + ")" + "/r/n");? ?
? ?? ?? ?? ?raf.writeBytes("/t{" + "/r/n");? ?
? ?? ?? ?? ?raf.writeBytes("/t/tthis." + beanInfo.getAttr()? ??
? ?? ?? ?? ?? ? + " = " + beanInfo.getAttr() + ";" + "/r/n");? ?
? ?? ?? ?? ?raf.writeBytes("/t}" + "/r/n");? ?
? ?? ?? ?? ?//----------------------------------------------------------? ?
??
? ?? ?? ?? ?//--------------------生成Bean的get方法---------------------? ?
? ?? ?? ?? ?raf.writeBytes("/tpublic "? ??
? ?? ?? ?? ?? ? + beanInfo.getType() + " get"? ??
? ?? ?? ?? ?? ? + beanInfo.getAttr().substring(0, 1).toUpperCase()? ?
? ?? ?? ?? ?? ? + beanInfo.getAttr().substring(1) + "()" + "/r/n");? ?
? ?? ?? ?? ?raf.writeBytes("/t{" + "/r/n");? ?
? ?? ?? ?? ?raf.writeBytes("/t/treturn " + beanInfo.getAttr()? ??
? ?? ?? ?? ?? ? + ";" + "/r/n/t}/r/n");? ?
? ?? ?? ?? ?//----------------------------------------------------------? ?
? ?? ???}? ?
? ?? ???raf.writeBytes("}/r/n");? ?
? ?? ???//關(guān)閉寫入流? ?
? ?? ???raf.close();? ?
? ? }? ?
??
? ? public static void main(String[] args) throws Exception? ??
? ? {? ?
? ?? ???BeanGenerator beanGen = new BeanGenerator();? ?
? ?? ???beanGen.parse(new File(args[0]));? ?
? ?? ???beanGen.generateSetGetConstructor();? ?
? ? }? ?
}?
BeanInfo.java
package codegen;
//保存Bean屬性和其屬性類型
class BeanInfo?
{
? ? private String type;
? ? private String attr;
? ? public BeanInfo(String type, String attr)
? ? {
? ?? ???this.type = type;
? ?? ???this.attr = attr;
? ? }
? ? public void setType(String type)
? ? {
? ?? ???this.type = type;
? ? }
? ? public String getType()
? ? {
? ?? ???return type;
? ? }
? ? public void setAttr(String attr)
? ? {
? ?? ???this.attr = attr;
? ? }
? ? public String getAttr()
? ? {
? ?? ???return attr;
? ? }
}
配置EditPlus:
測(cè)試:
總結(jié)
以上是生活随笔為你收集整理的JavaBean的get、set方法生成器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MATLAB R2022b 原生适配 A
- 下一篇: 外媒:三星S23 Ultra是安卓阵营的