ICE生成的model沒(méi)有g(shù)et和set方法
ackage com.yesky.wstsearch.common;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class ClassUtil {
/**
* 獲取一個(gè)包下所有類對(duì)象
*
* @param packageName
* @return
*/
public static List<Class> getClasses(String packageName) {
List<Class> res = new ArrayList<Class>();
String name = new String(packageName);
if (!name.startsWith("/")) {
name = "/" + name;
}
name = name.replace('.', '/');
URL url = Class.class.getResource(name);
if (url == null) {
return res;
}
File directory = new File(url.getFile());
if (directory.exists()) {
String[] files = directory.list();
for (int i = 0; i < files.length; i++) {
if (files[i].endsWith(".class")) {
String classname = files[i].substring(0,
files[i].length() - 6);
try {
String clsName = packageName + "." + classname;
Class clazz = Class.forName(clsName);
res.add(clazz);
} catch (Exception e) {
}
}
}
}
return res;
}
}
package com.yesky.wstsearch.common;
import java.io.*;
/*
* Copyright (c) 2005 重慶天極信息發(fā)展有限公司. All Rights Reserved.
*/
/**
* 操作文件的一些相關(guān)方法,該類不需要實(shí)例化,不允許繼承。
*/
public final class FileUtil {
/**
* 該類不需要實(shí)例化
*/
private FileUtil() {
}
/**
* --------------------------------------------------------------------------
*
* @param fileFullName 文件名,包括路徑
* @return 文件內(nèi)容
* @throws IOException 讀取例外。
*/
public static String fileRead(String fileFullName) throws IOException {
//---------------------------------
// 定義返回結(jié)果變量
//---------------------------------
String result = null;
InputStream in = null;
try {
File file = new File(fileFullName);
long len = file.length();
if (len > 0) {
//---------------------------------
// 如果文件的字節(jié)數(shù)大于0,打開(kāi)流
//---------------------------------
in = new FileInputStream(file);
byte[] bytes = new byte[(int) len];
//---------------------------------
// 讀入全部?jī)?nèi)容到byte數(shù)組中
//---------------------------------
in.read(bytes);
//---------------------------------
// 把byte數(shù)組中的內(nèi)容轉(zhuǎn)換成String
//---------------------------------
result = new String(bytes);
bytes = null;
}
} finally {
if (in != null) {
//---------------------------------
// 如果流不為空,則最后要關(guān)閉流。
//---------------------------------
try {
in.close();
in = null;
} catch (IOException e) {
//---------------------------------
// 該例外不需要處理。
//---------------------------------
}
}
}
return result;
}
/**
* 將String寫入到文件,該方法是以文本形式寫得到文件中<br>
* --------------------------------------------------------------------------
*
* @param fileFullName 文件全名
* @param fileContent 內(nèi)容
* @param append 是否追加
* @throws IOException 例外
*/
public static void fileWrite(String fileFullName, String fileContent, boolean append) throws IOException {
fileWrite(new File(fileFullName), fileContent, append);
}
/**
* 將String寫入到文件,該方法是以文本形式寫得到文件中<br>
* --------------------------------------------------------------------------
*
* @param fileFullName 文件全名
* @param fileContent 內(nèi)容
* @param append 是否追加
* @throws IOException 例外
*/
public static void fileWrite(File fileFullName, String fileContent, boolean append) throws IOException {
FileWriter writer = null;
try {
//---------------------------------
// 獲得一個(gè)文件寫入的句柄
//---------------------------------
writer = new FileWriter(fileFullName, append);
//---------------------------------
// 寫入內(nèi)容
//---------------------------------
writer.write(fileContent);
//---------------------------------
// 將內(nèi)容寫到碰盤上
//---------------------------------
writer.flush();
} finally {
if (writer != null) {
//---------------------------------
// 如果句柄不為空。則最后要關(guān)閉句柄
//---------------------------------
try {
writer.close();
writer = null;
} catch (IOException e) {
}
}
}
}
/**
* 將byte數(shù)組寫入到文件,本方法是以二進(jìn)制的形式寫到碰盤上<br>
* --------------------------------------------------------------------------
*
* @param fileFullName 文件全名
* @param fileContent 內(nèi)容
* @param append 是否追加
* @throws IOException 例外
*/
public static void fileWrite(String fileFullName, byte[] fileContent, boolean append) throws IOException {
fileWrite(new File(fileFullName), fileContent, append);
}
/**
* 將byte數(shù)組寫入到文件,本方法是以二進(jìn)制的形式寫到碰盤上<br>
* --------------------------------------------------------------------------<br>
*
* @param fileFullName 文件全名
* @param fileContent 內(nèi)容
* @param append 是否追加
* @throws IOException 例外
*/
public static void fileWrite(File fileFullName, byte[] fileContent, boolean append) throws IOException {
File parent = fileFullName.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
FileOutputStream outputStream = null;
try {
//---------------------------------
// 獲得一個(gè)二進(jìn)制寫入流的句柄
//---------------------------------
outputStream = new FileOutputStream(fileFullName,
append);
//---------------------------------
// 寫入內(nèi)容
//---------------------------------
outputStream.write(fileContent);
//---------------------------------
// 將內(nèi)容寫到碰盤上
//---------------------------------
outputStream.flush();
} finally {
if (outputStream != null) {
//---------------------------------
// 如果句柄不為空。則最后要關(guān)閉句柄
//---------------------------------
try {
outputStream.close();
outputStream = null;
} catch (Exception e) {
}
}
}
}
public static void fileWrite(File path, String fileName, String content, boolean append) throws IOException {
if (!path.exists() || !path.isDirectory()) {
path.mkdirs();
}
File file = new File(path, fileName);
fileWrite(file.getPath(), content, append);
}
public static void delFile(String filepath) throws IOException {
File f = new File(filepath);//定義文件路徑
boolean flag = false;
if (f.exists() && f.isDirectory()) {//判斷是文件還是目錄
if (f.listFiles().length == 0 && flag == true) {//若目錄下沒(méi)有文件則直接刪除
f.delete();
} else {//若有則把文件放進(jìn)數(shù)組,并判斷是否有下級(jí)目錄
flag = true;
File delFile[] = f.listFiles();
int i = f.listFiles().length;
for (int j = 0; j < i; j++) {
if (delFile[j].isDirectory()) {
delFile(delFile[j].getAbsolutePath());//遞歸調(diào)用del方法并取得子目錄路徑
}
delFile[j].delete();//刪除文件
}
}
}
}
public static void main(String[] args) {
try {
delFile("D:\\temp");
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
package com.yesky.wstsearch.common;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
/**
* 使用方法:
* 1.編譯ICE文件
* 2.編譯此類及ClassUtil,FileUtil;
* 3.運(yùn)行此類
*
* @author Owner
*/
public class AddGetterSetter {
public String getGetterSetter(Class className) {
StringBuffer setergeters = new StringBuffer();
if (className != null) {
Field[] fieldobject = className.getFields();
String getter, setter;
for (int i = 0; i < fieldobject.length; i++) {
String objectname = fieldobject[i].getName();
String objDataType = (fieldobject[i].getType().getName()).toString();
if ("[Ljava.lang.String;".equals(objDataType)) {
objDataType = "String[]";
} else if ("[I".equals(objDataType)) {
objDataType = "int[]";
}
getter = "\tpublic " + objDataType + " get" + (char) (objectname.charAt(0) - 32)
+ objectname.substring(1) + "() {\r\n\t\t";
try {
if ("java.lang.String".equals(objDataType)) {
getter += "if(" + objectname + " == null) {\r\n\t\t\t" + objectname + " = \"\";\r\n\t\t}\r\n\t\t";
}
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
getter += "return "
+ objectname + ";\r\n\t}\r\n\r\n";
setter = "\tpublic void set" + (char) (objectname.charAt(0) - 32) + objectname.substring(1)
+ "(" + objDataType + " " + objectname + ") {\r\n\t\t"
+ "this." + objectname + " = " + objectname + ";\r\n\t}\r\n\r\n";
setergeters.append(getter).append(setter);
}
if (fieldobject.length > 0) {
setergeters.append("\t//icemodel2bean generated.");
}
}
return setergeters.toString();
}
public void writeSource(String sourcePath, String content) {
try {
String fileContent = FileUtil.fileRead(sourcePath);
if (fileContent.indexOf("icemodel2bean") == -1) {
int end = fileContent.lastIndexOf("}");
String subContent = fileContent.substring(0, end);
fileContent = subContent + content + "\r\n}";
FileUtil.fileWrite(sourcePath, fileContent, false);
} else {
System.out.println(sourcePath + " icemodel2bean already generated.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AddGetterSetter adder = new AddGetterSetter();
List<Class> classList = ClassUtil.getClasses("com.tmg.rescenter.serviceice.modelice");
String realPath = Class.class.getResource("/").getPath();
realPath = realPath.substring(0, realPath.indexOf("rescenter") + 9) + "/src/com/tmg/rescenter/serviceice/modelice/";
String clazzName;
for (Class clazz : classList) {
clazzName = clazz.getSimpleName();
if (clazzName.matches(".+Ice$")) {
adder.writeSource(realPath + clazzName + ".java", adder.getGetterSetter(clazz));
System.out.println(clazz.getSimpleName());
}
}
//------------------------------
List<Class> classList1 = ClassUtil.getClasses("com.tmg.rescenter.serviceice.modelice.productextension");
realPath = realPath.substring(0, realPath.indexOf("rescenter") + 9) + "/src/com/tmg/rescenter/serviceice/modelice/productextension/";
String clazzName1;
for (Class clazz : classList1) {
clazzName1 = clazz.getSimpleName();
if (clazzName1.matches(".+Ice$")) {
adder.writeSource(realPath + clazzName1 + ".java", adder.getGetterSetter(clazz));
System.out.println(clazz.getSimpleName());
}
}
//------------------------------
List<Class> classList2 = ClassUtil.getClasses("com.yesky.wstsearch.modelIce");
realPath = "E:/workspace/WsSearch2010/src/com/yesky/wstsearch/modelIce/";
String clazzName2;
for (Class clazz : classList2) {
clazzName2 = clazz.getSimpleName();
System.out.println(clazzName2);
if (clazzName2.matches(".+Ice$")) {
System.out.println(realPath);
adder.writeSource(realPath + clazzName2 + ".java", adder.getGetterSetter(clazz));
System.out.println(clazz.getSimpleName());
}
}
}
}