快速形成PageableResultSet2 的工具类
package sql;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReplaceUtil {
public static final String INPUTPATH = "C://practice//javabasic//sql//PageableResultSet2.java";
public static final String OUTPUTPATH = "C://practice//javabasic//sql//Test.java";
public boolean needReplace = false;
public int count = 0;
public String replaceString = "";
public void process() {
BufferedReader in = null;
BufferedWriter out = null;
StringBuffer sb = new StringBuffer();
try {
in = new BufferedReader(new FileReader(INPUTPATH));
out = new BufferedWriter(new FileWriter(OUTPUTPATH));
String line = "";
while ((line = in.readLine()) != null) {
getMethodName(line);
if (needReplace == true && count < 4) {
count++;
}
line = getNewLine(line);
sb.append(line);
sb.append("/r/n");
}
out.write(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* get Method Name
* @param line
*/
public void getMethodName(String line) {
if (line.trim().length() == 0 || line == null) {
return;
}
if (line.indexOf("get") > 0 || line.indexOf("is") > 0
|| line.indexOf("set") > 0 || line.indexOf("update") > 0) {
needReplace = true;
}
if (needReplace && count == 0) {
int from = 0;
if (line.indexOf("get") > 0) {
from = line.indexOf("get");
} else if (line.indexOf("is") > 0) {
from = line.indexOf("is");
} else if (line.indexOf("set") > 0) {
from = line.indexOf("set");
} else if (line.indexOf("update") > 0) {
from = line.indexOf("update");
}
int to = line.indexOf(")");
replaceString = line.substring(from, to + 1);
System.out.println("[method name:]" + replaceString);
}
}
/**
* get New Line
* @param line
* @return
*/
public String getNewLine(String line) {
if (needReplace == true && count == 3) {
line = concatReturnString(line);
needReplace = false;
count = 0;
replaceString = "";
}
return line;
}
/**
* concat the String
* @param line
* @return
*/
public String concatReturnString(String line) {
if (replaceString.indexOf("String") > 0) {
processReplaceString("String ");
}
if (replaceString.indexOf("int") > 0) {
processReplaceString("int ");
}
if (replaceString.indexOf("Calendar") > 0) {
processReplaceString("Calendar ");
}
if (replaceString.indexOf("Map") > 0) {
processReplaceString("Map ");
}
if (replaceString.indexOf("Array") > 0) {
processReplaceString("Array ");
}
if (replaceString.indexOf("InputStream") > 0) {
processReplaceString("InputStream ");
}
if (replaceString.indexOf("BigDecimal") > 0) {
processReplaceString("BigDecimal ");
}
if (replaceString.indexOf("Blob") > 0) {
processReplaceString("Blob ");
}
if (replaceString.indexOf("boolean") > 0) {
processReplaceString("boolean ");
}
if (replaceString.indexOf("byte") > 0) {
processReplaceString("byte ");
}
if (replaceString.indexOf("Reader") > 0) {
processReplaceString("Reader ");
}
if (replaceString.indexOf("Clob") > 0) {
processReplaceString("Clob ");
}
if (replaceString.indexOf("Date") > 0) {
processReplaceString("Date ");
}
if (replaceString.indexOf("double") > 0) {
processReplaceString("double ");
}
if (replaceString.indexOf("float") > 0) {
processReplaceString("float ");
}
if (replaceString.indexOf("long") > 0) {
processReplaceString("long ");
}
if (replaceString.indexOf("Object") > 0) {
processReplaceString("Object ");
}
if (replaceString.indexOf("Ref") > 0) {
processReplaceString("Ref ");
}
if (replaceString.indexOf("short") > 0) {
processReplaceString("short ");
}
if (replaceString.indexOf("Time") > 0) {
processReplaceString("Time ");
}
if (replaceString.indexOf("Timestamp") > 0) {
processReplaceString("Timestamp ");
}
String s = "";
if (line.indexOf("return") > 0) {
s += " return rs." + replaceString + ";";
} else {
s += " rs." + replaceString + ";";
}
return s;
}
/**
* replace the int String as ""
* @param replace
*/
private void processReplaceString(String replace) {
Pattern pPattern = Pattern.compile(replace + "+");
Matcher m = pPattern.matcher(replaceString);
replaceString = m.replaceAll("");
}
public static void main(String[] args) {
new ReplaceUtil().process();
}
}