ICE生成的model没有get和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 {
/**
* 获取一个包下所有类对象
*
* @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 重庆天极信息发展有限公司. All Rights Reserved.
*/
/**
* 操作文件的一些相关方法,该类不需要实例化,不允许继承。
*/
public final class FileUtil {
/**
* 该类不需要实例化
*/
private FileUtil() {
}
/**
* --------------------------------------------------------------------------
*
* @param fileFullName 文件名,包括路径
* @return 文件内容
* @throws IOException 读取例外。
*/
public static String fileRead(String fileFullName) throws IOException {
//---------------------------------
// 定义返回结果变量
//---------------------------------
String result = null;
InputStream in = null;
try {
File file = new File(fileFullName);
long len = file.length();
if (len > 0) {
//---------------------------------
// 如果文件的字节数大于0,打开流
//---------------------------------
in = new FileInputStream(file);
byte[] bytes = new byte[(int) len];
//---------------------------------
// 读入全部内容到byte数组中
//---------------------------------
in.read(bytes);
//---------------------------------
// 把byte数组中的内容转换成String
//---------------------------------
result = new String(bytes);
bytes = null;
}
} finally {
if (in != null) {
//---------------------------------
// 如果流不为空,则最后要关闭流。
//---------------------------------
try {
in.close();
in = null;
} catch (IOException e) {
//---------------------------------
// 该例外不需要处理。
//---------------------------------
}
}
}
return result;
}
/**
* 将String写入到文件,该方法是以文本形式写得到文件中<br>
* --------------------------------------------------------------------------
*
* @param fileFullName 文件全名
* @param fileContent 内容
* @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 内容
* @param append 是否追加
* @throws IOException 例外
*/
public static void fileWrite(File fileFullName, String fileContent, boolean append) throws IOException {
FileWriter writer = null;
try {
//---------------------------------
// 获得一个文件写入的句柄
//---------------------------------
writer = new FileWriter(fileFullName, append);
//---------------------------------
// 写入内容
//---------------------------------
writer.write(fileContent);
//---------------------------------
// 将内容写到碰盘上
//---------------------------------
writer.flush();
} finally {
if (writer != null) {
//---------------------------------
// 如果句柄不为空。则最后要关闭句柄
//---------------------------------
try {
writer.close();
writer = null;
} catch (IOException e) {
}
}
}
}
/**
* 将byte数组写入到文件,本方法是以二进制的形式写到碰盘上<br>
* --------------------------------------------------------------------------
*
* @param fileFullName 文件全名
* @param fileContent 内容
* @param append 是否追加
* @throws IOException 例外
*/
public static void fileWrite(String fileFullName, byte[] fileContent, boolean append) throws IOException {
fileWrite(new File(fileFullName), fileContent, append);
}
/**
* 将byte数组写入到文件,本方法是以二进制的形式写到碰盘上<br>
* --------------------------------------------------------------------------<br>
*
* @param fileFullName 文件全名
* @param fileContent 内容
* @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 {
//---------------------------------
// 获得一个二进制写入流的句柄
//---------------------------------
outputStream = new FileOutputStream(fileFullName,
append);
//---------------------------------
// 写入内容
//---------------------------------
outputStream.write(fileContent);
//---------------------------------
// 将内容写到碰盘上
//---------------------------------
outputStream.flush();
} finally {
if (outputStream != null) {
//---------------------------------
// 如果句柄不为空。则最后要关闭句柄
//---------------------------------
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) {//若目录下没有文件则直接删除
f.delete();
} else {//若有则把文件放进数组,并判断是否有下级目录
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());//递归调用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.运行此类
*
* @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());
}
}
}
}