zoukankan      html  css  js  c++  java
  • 一个逆向工程的尝试

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.regex.Pattern;
        
    public class InsertLogIntoFile {
            
        private static List<String> list = new LinkedList<>();
        private static final String resourceLocation = "/H:/git_studyspace/spring/src/main/java/org";      
        private static final String loggerPath = "lsh.spring.util.Log.info();";
        private static final String methodMarks = "byte,short,int,long,float,double,boolean,char,void";
            
        public static void main(String[] args) {
            readJavaFile(new File(resourceLocation));
        }
         
        public static String getCurrentPath(Class<?> clazz) {
            return clazz.getResource(".").getPath();
        }
            
        private static void readJavaFile(File file) {
            if(file.isDirectory()) {
                File[] files = file.listFiles();
                for (File unit : files) {
                    if(unit.isFile()) {//如果是文件,则进行进一步的编辑
                        if(unit.getName().endsWith("InsertLogIntoFile.java")
                                || unit.getName().contains("AddLogToFile.java")
                                || unit.getName().contains("Log.java")) {
                            continue;
                        }
                        judgeLineByLine(unit.getAbsolutePath());
                    }else {
                        readJavaFile(unit);
                    }
                }
            }else {
                if(file.getName().endsWith("InsertLogIntoFile.java")
                        || file.getName().contains("AddLogToFile.java")
                        || file.getName().contains("Log.java")) {
                    return;
                }
                judgeLineByLine(file.getAbsolutePath());
            }
        }
            
        private static void judgeLineByLine(String file) {
            BufferedReader br = null;
            try {
                //先将文件都按行读取到缓存中
                br = new BufferedReader(new FileReader(file));
                String line = null;
                while((line = br.readLine()) != null) {
                    list.add(line);
                }
                br.close();
    
                //从缓存中读取文件内容时按需要修改内容,重新写入到文件当中
                BufferedWriter bw = null;
                bw = new BufferedWriter(new FileWriter(file));
                for (String string : list) {
                    if(file.endsWith(".java")) {
                        String newLine = judgeMethodLocation(string);
                        bw.write(newLine);
                        bw.write("
    ");
                    }else {
                        bw.write(string);
                        bw.write("
    ");
                    }                
                }
                list.clear();
                bw.write("
    ");
                bw.flush();
                bw.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        //判断方法签名所在行,如果是方法签名,则在下一行插入日志并返回字符串
        private static String judgeMethodLocation(String str) {
            if(!(str.trim().contains("(")&&str.contains(")")&&str.contains("{")&&!str.contains("}"))||str.trim().startsWith("//")) {
                return str;
            }
            String[] arr = str.split("[(]");
            int mark = str.indexOf("(");
            //如果按照“(”进行字符串拆分得到的长度不小于2,且原字符串中字符“(”的前一个字符不为空
            if(arr.length == 2 && (str.toCharArray()[mark-1] !=' ')) {
                String[] arr2 = arr[arr.length-2].split(" ");
                    int length2 = arr2.length;
                    if(startWithLowerCase(arr2[length2 - 1]) &&length2 >= 2 && (startWithUpperCase(arr2[length2-2]) || matchBasicType(arr2[length2-2]))) {
                        return insertLoggerCommand(str);
                    }
                }
            return str;
        }
         
        private static boolean startWithUpperCase(String str) {
            if(str.trim().length() == 0) {
                return false;
            }
            String regex = "[A-Z]";
            Pattern p = Pattern.compile(regex);
            return p.matcher(str.substring(0, 1)).matches();
        }
         
        private static boolean startWithLowerCase(String str) {
            if(str.trim().length() == 0) {
                return false;
            }
            String regex = "[a-z]";
            Pattern p = Pattern.compile(regex);
            return p.matcher(str.substring(0, 1)).matches();
        }
         
        public static boolean matchBasicType(String str) {
            String[] arr = methodMarks.split(",");
            for (String string : arr) {
                if(string.equals(str) || (string+"[]").equals(string)) {
                    return true;
                }
            }
            return false;
        }
            
        //插入日志
        private static String insertLoggerCommand(String lineStr) {
            StringBuilder sb = new StringBuilder(lineStr);
            sb.append("
            ")
              .append(loggerPath);
            return sb.toString();
        }
        
    }
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    
    public class Log {
        
        private static int count = 0;
        
        private static PrintStream ps = null;
        
        static {
            try {
                ps = new PrintStream(new File("beanDefinitionLoad.md"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args) {
            
        }
    
        public static void info() {
            StackTraceElement[] ste = new Exception().getStackTrace();
            String className = ste[1].getClassName();
            String methodName = ste[1].getMethodName();
            int lineNumber = ste[1].getLineNumber();
            StringBuilder sb = new StringBuilder();
            for (int i=0; i <= count; i++) {
                sb.append(" ");
            }
            count++;
            ps.println(sb+"["+className+"."+methodName+": "+lineNumber+"]");
            if(count == 30) {
                count = 0;
            }
        }
        
        public static void info(String str) {
            StackTraceElement[] ste = new Exception().getStackTrace();
            String className = ste[1].getClassName();
            String methodName = ste[1].getMethodName();
            int lineNumber = ste[1].getLineNumber();
            StringBuilder sb = new StringBuilder();
            for (int i=0; i <= count; i++) {
                sb.append(" ");
            }
            count++;
            System.out.println(sb+"["+className+"."+methodName+": "+lineNumber+"] "+str);
            if(count == 30) {
                count = 0;
            }
        }
    }
  • 相关阅读:
    数据分析的数据来源都有哪些?
    数据分析的技能要求及分析流程
    (原创)使用matlab-cftools拟合工具的问题
    Spring加载xml配置文件的方式
    Spring-ResolvableType可解决的数据类型
    从list中取N个随机生成一个集合
    AOP统一处理修改人、创建人、修改时间、创建时间
    Java依据集合元素的属性,集合相减
    java去掉数字后面的0
    数字格式化NumberFormat
  • 原文地址:https://www.cnblogs.com/InformationGod/p/9566420.html
Copyright © 2011-2022 走看看