有这样一类代码(方法){
conn = DBConnectionPool.getConnection();
//可以有代码
stmt = conn.prepareStatement("update t_base_company set name=?,re=? " +
"where id=?");
stmt.setString(1, name);
stmt.setString(2, re);
stmt.setShort(3, id);
stmt.execute();
//可以有代码
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBConnectionPool.closeStatementSafey(stmt);
DBConnectionPool.closeConnectionSafey(conn);
}现在我想通过eclipse搜索(通过正则表达式的方式)包含getConnection和closeConnectionSafey。
请问这样的正则表达式这样写:关键是换行(不确定有多少行)、空格。这能这类代码中找,不能跨代码 (方法)谢谢各位!!!

解决方案 »

  1.   

    getConnection和closeConnectionSafey
    必须同时出现在这个代码块中
    (这类代码块很多)不能出现如下的情况:代码块1
    {
                conn = DBConnectionPool.getConnection();
                //可以有代码
                stmt = conn.prepareStatement("update t_base_company set name=?,re=? " +
                        "where id=?");
                stmt.setString(1, name);
                stmt.setString(2, re);
                stmt.setShort(3, id);
                stmt.execute();
                //可以有代码
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                DBConnectionPool.closeStatementSafey(stmt);
            }代码块2:
    {
                conn = DBConnectionPool.getConnection();
                //可以有代码
                stmt = conn.prepareStatement("update t_base_company set name=?,re=? " +
                        "where id=?");
                stmt.setString(1, name);
                stmt.setString(2, re);
                stmt.setShort(3, id);
                stmt.execute();
                //可以有代码
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                DBConnectionPool.closeStatementSafey(stmt);
                DBConnectionPool.closeConnectionSafey(conn);
            }
    按正常情况只能找到---代码块2
      

  2.   

    (这类代码块很多)不能出现如下的情况: 
    代码块1
    {
                conn = DBConnectionPool.getConnection();
                //可以有代码
                stmt = conn.prepareStatement("update t_base_company set name=?,re=? " +
                        "where id=?");
                stmt.setString(1, name);
                stmt.setString(2, re);
                stmt.setShort(3, id);
                stmt.execute();
                //可以有代码
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                DBConnectionPool.closeStatementSafey(stmt);
            }代码块2:
    {
                conn = DBConnectionPool.getConnection();
                //可以有代码
                stmt = conn.prepareStatement("update t_base_company set name=?,re=? " +
                        "where id=?");
                stmt.setString(1, name);
                stmt.setString(2, re);
                stmt.setShort(3, id);
                stmt.execute();
                //可以有代码
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                DBConnectionPool.closeStatementSafey(stmt);
                DBConnectionPool.closeConnectionSafey(conn);
            }按正常情况只能找到---代码块2 
      

  3.   

      conn = DBConnectionPool.getConnection();
      DBConnectionPool.closeConnectionSafey(conn); 代码块2是同时出现这2个了啊
    代码块一又没有同时出现
      

  4.   

    请问这样的正则表达式怎样写?
    只能在这个范围找。找不到进入另一个这样的范围找。
                conn = DBConnectionPool.getConnection(); 
                //可以有代码 
                stmt = conn.prepareStatement("update t_base_company set name=?,re=? " + 
                        "where id=?"); 
                stmt.setString(1, name); 
                stmt.setString(2, re); 
                stmt.setShort(3, id); 
                stmt.execute(); 
                //可以有代码 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            }finally{ 
                DBConnectionPool.closeStatementSafey(stmt); 
            }getConnection和closeConnectionSafey 
    必须同时出现在这个代码块中
      

  5.   

    LZ的问题,我没有看懂,是不是:
       用正则表达式搜索getConnection 和 closeConnectionSafey 这两个字符串,并且他们得同时出现??
      

  6.   

    有很多个这样的代码块{
                conn = DBConnectionPool.getConnection();
                //可以有代码
                stmt = conn.prepareStatement("update t_base_company set name=?,re=? " +
                        "where id=?");
                stmt.setString(1, name);
                stmt.setString(2, re);
                stmt.setShort(3, id);
                stmt.execute();
                //可以有代码
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                DBConnectionPool.closeStatementSafey(stmt);
                DBConnectionPool.closeConnectionSafey(conn);
            }
    在这样的代码块中
    用正则表达式搜索getConnection 和 closeConnectionSafey 这两个字符串,并且他们得同时出现
    谢谢大家!!!
      

  7.   

    看看这里的
    http://topic.csdn.net/t/20040914/12/3370558.html
    http://developer.51cto.com/art/200909/152215.htm
      

  8.   

    最简单的一个,不过我没有考虑其他情况
    (.+)(getConnection)(.+)(closeConnectionSafey)(.+)lz出现错误则会话
      

  9.   

    哈哈,看明白了,楼主的意思是在同一个方法里面必须同时出现那两个单词,二不能跨越多个方法,是吧
    感觉如果想直接把整个代码当做是一个String来进行搜索的话,比较困难
    可以先把整个代码差分成若干个方法,用栈去做,然后再对每个方法进行搜索,应该就可以了吧
      

  10.   

    先定位代码块的起始位置“{”,然后分别查找getConnection或closeConnectionSafey这两个字段,如果先找到的是getConnection字段,那下一个查找就找closeConnectionSafey字段;如果是先找到closeConnectionSafey字段,那下一个就查找getConnection字段。如果碰到代码块结束符“}”则重新初始化查找进行下一个代码块查找,如果没有碰到代码块结束符“}”并且同时找到这两个字段,那就进行处理。
    不知道楼主的意思是不是这样?
      

  11.   

    或者楼主的意思是下面的这样子?
    先定位代码块的起始位置“{”,然后分别查找getConnection或closeConnectionSafey这两个字段,如果先找到的是getConnection字段,那下一个查找就找closeConnectionSafey字段;如果是先找到closeConnectionSafey字段,那下一个就查找getConnection字段。如果碰到代码块结束符“}”或者碰到代码块起始符“{”则重新初始化查找进行下一个代码块查找,如果没有碰到代码块结束符“}”或者没有碰到代码块起始符“{”并且同时找到这两个字段,那就进行处理。 
    如果楼主的意思是上面的这两种意思,可以按照思路进行设计查找方法了
      

  12.   


    如果第二个字段是在finally{}里??
      

  13.   

    不支持构造函数,如果构造函数含有这种代码,则寻找不到。
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class UseReflectFind {
    public void method2(){
    Method[] methods;
    System.out.println();
    }
    public static void main(String[] args){

    getConnection();
    closeConnectionSafey();
    List<String> fileList = iterateDir("D:\\hwj\\work\\workspace\\Test\\src");
    System.out.println("file number is:"+fileList.size());
    String searchGet = "getConnection";
    String searchClose = "closeConnectionSafey";
    System.out.println("such methods contain :\n"+findSpecialText(fileList,searchGet,searchClose));


    }
    public static List<String> iterateDir(String srcDir){
    File srcFile = new File(srcDir);
    File[] files = srcFile.listFiles();
    ArrayList<String> pathList = new ArrayList<String>();
    for(int i=0;i<files.length;i++){
    File tempFile = files[i];
    if(tempFile.isDirectory()){
    pathList.addAll(iterateDir(tempFile.getAbsolutePath()));
    }else{
    pathList.add(tempFile.getAbsolutePath());
    }
    }
    return pathList;
    }
      

  14.   

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class UseReflectFind {
    public void method2(){
    Method[] methods;
    System.out.println();
    }
    public static void main(String[] args){

    getConnection();
    closeConnectionSafey();
    List<String> fileList = iterateDir("D:\\hwj\\work\\workspace\\Test\\src");
    System.out.println("file number is:"+fileList.size());
    String searchGet = "getConnection";
    String searchClose = "closeConnectionSafey";
    System.out.println("such methods contain :\n"+findSpecialText(fileList,searchGet,searchClose));


    }
    public static List<String> iterateDir(String srcDir){
    File srcFile = new File(srcDir);
    File[] files = srcFile.listFiles();
    ArrayList<String> pathList = new ArrayList<String>();
    for(int i=0;i<files.length;i++){
    File tempFile = files[i];
    if(tempFile.isDirectory()){
    pathList.addAll(iterateDir(tempFile.getAbsolutePath()));
    }else{
    pathList.add(tempFile.getAbsolutePath());
    }
    }
    return pathList;
    }
      

  15.   

    public static String findSpecialText(List<String> fileList,String searchGet,String searchClose){
    int fileNum = fileList.size();
    String regex = "[a-zA-Z_$][a-zA-Z0-9_$]*(\\s*\\[\\s*\\])?\\s+[a-zA-Z_$][a-zA-Z0-9_$]*\\s*\\((\\s*[a-zA-Z_$][a-zA-Z0-9_$]*(\\s*\\[\\s*\\])?\\s+[a-zA-Z_$][a-zA-Z0-9_$]*(\\s*\\[\\s*\\])?\\s*|\\s*)\\)\\s*\\{";
    Pattern pattern = Pattern.compile(regex,Pattern.DOTALL);
    StringBuilder methodBuilder = new StringBuilder();
    String splitLine = "--------------------";
    for(int i=0;i<fileNum;i++){

    String tempPath = fileList.get(i);
    StringBuilder builder = new StringBuilder();
    try {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(tempPath)));
    byte[] buffer = new byte[1024];
    int readNum = 0;


    while((readNum = bis.read(buffer)) != -1){
    builder.append(new String(buffer,0,readNum));
    }
    bis.close();
    } catch (Exception e) {
    e.printStackTrace();
    }

    String code = builder.toString();

    Matcher matcher = pattern.matcher(code);
    ArrayList<PosName> positionList = new ArrayList<PosName>();
    while(matcher.find()){
    int start = matcher.start();
    int end = matcher.end();
    String name = code.substring(start,end);
    int tempIndex = name.indexOf('{');
    name = name.substring(0,tempIndex);
    positionList.add(new PosName(start,name));
    }
    int len = positionList.size();


    String methodText = null;
    int getIndex = 0;
    int closeIndex = 0;
    boolean appendFileName = false;
    if(len>0){
    for(int j=0;j<len-1;j++){
    PosName curPosName = positionList.get(j);
    PosName nextPosName = positionList.get(j+1);
    int curPos = curPosName.getPosition();
    int nextPos = nextPosName.getPosition();
    methodText = code.substring(curPos,nextPos);
    int tempIndex = methodText.lastIndexOf('}');
    methodText = methodText.substring(0,tempIndex+1);
    getIndex = methodText.indexOf(searchGet);
    closeIndex = methodText.indexOf(searchClose);
    if(getIndex>=0 && closeIndex>=0){
    if(!appendFileName){
    methodBuilder.append(splitLine+tempPath+splitLine+"\n");
    appendFileName = true;
    }
    methodBuilder.append(curPosName.getName()+"\n");
    }

    }
    PosName lastPosName = positionList.get(len-1);
    methodText = code.substring(lastPosName.getPosition());
    methodText = methodText.substring(0,methodText.lastIndexOf('}'));
    getIndex = methodText.indexOf(searchGet);
    closeIndex = methodText.indexOf(searchClose);
    if(getIndex>=0 && closeIndex>=0){
    if(!appendFileName){
    methodBuilder.append(splitLine+tempPath+splitLine+"\n");
    appendFileName = true;
    }
    methodBuilder.append(lastPosName.getName()+"\n");
    }
    }
    }
    return methodBuilder.toString();
    }
      

  16.   

    public static void method1(){
    getConnection();
    closeConnectionSafey();
    }
    public static void getConnection(){

    }
    public static void closeConnectionSafey(){

    }
    }
    class PosName{
    private int position;
    private String name;

    public PosName(int position,String name){
    this.position = position;
    this.name = name;
    } public int getPosition() {
    return position;
    } public void setPosition(int position) {
    this.position = position;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    }

    }