codingmiao
3/20/2017 - 7:56 AM

生成装饰器模式的ResultSet代码

生成装饰器模式的ResultSet代码

package org.wowtools.dao;

import java.sql.SQLException;
import java.util.function.IntConsumer;

/**
 * @author liuyu
 * @date 2017/3/20
 */
public class T44 {
    //    @Override
//    public String getString(int columnIndex) throws SQLException {
//        return null;
//    }
    public static void main(String[] args) {
        String s = org.wowtools.common.utils.ResourcesReader.readStr("D:\\IDEA\\work\\mygithub\\catframe\\catframe\\catframe-dao\\src\\main\\java\\org\\wowtools\\dao\\DecoratorResultSet.java");
        String[] methods = s.split("@Over" + "ride");
        for (String method : methods) {
            int e = method.indexOf("(");
            int b = method.lastIndexOf(" ", e);
            String name = method.substring(b, e);

            b = e;
            e = method.indexOf(")") + 1;
            String param = method.substring(b, e);
            StringBuilder sb = new StringBuilder();
            sb.append("rs.");
            sb.append(name.trim());
            sb.append("(");
            IntConsumer ac = new IntConsumer() {
                boolean in = false;

                @Override
                public void accept(int value) {
                    char c = (char) value;
                    if (c == ' ') {
                        in = !in;
                    } else {
                        if (in && ')' != c) {
                            sb.append(c);
                        }
                    }
                }
            };
            param.chars().forEach(ac);
            sb.append(");");

            e = method.indexOf("}");
            b = method.lastIndexOf("{",e);
            if (b<0){
                continue;
            }
            b++;
            String rt = method.substring(b,e+1).trim();
            String rt1 = sb.toString();
            if (rt.indexOf("return ")>=0){
                rt1 = "return "+rt1;
            }
            method = method.replace(rt,rt1);
            method="@Over" + "ride"+method+"}";
            System.out.println(method);
        }
    }
}