Java 源码解析之局部变量检查

 

 

 

package com.tang;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class ParseSource {
    
    public static boolean inMethod = false;
    public static int braces = 0 ;
    
    // 名字,模板,类型的正则
    public static String namePattern = "[_a-zA-Z]+\\w*";
    public static String templatePattern = "(<[_a-zA-Z]+\\w*(, [_a-zA-Z]+\\w*)*>)?";
    public static String typePattern = namePattern + templatePattern + "(\\[\\]){0,2}";

    // 参数的正则
    public static String argPatternZero = "\\s*";       // 无参数
    public static String argPatternSingle = typePattern + "\\s+" + namePattern;
    public static String argPatternDynamic = typePattern + "\\s*" + "\\.\\.\\." + "\\s*" + namePattern; // 动态参数
    public static String argPattern = argPatternSingle+ "(\\s*,\\s*" + argPatternSingle + ")*" + "(\\s*,\\s*" + argPatternDynamic + ")?";  //多个参数 + 动态参数
    public static String argsPattern = "(" + argPatternZero + "|" + argPatternDynamic + "|" + argPattern + ")";

    public static void main(String[] args) {
        
        String filePath = "G:\\workspace2\\JavaJFX\\src\\com\\bowen\\LocalVarTest.java";
        parseFile(filePath);
        
//        String mSign = "public boolean skipTo(int a,Object...o) {";
//        boolean b = isMethod(mSign);
//        System.out.println(b);
    }

    private static void parseFile(String filePath) {
        if(filePath == null || "".equals(filePath))
            return;
        
        File file = new File(filePath);
        if(!file.exists())
            return;
        
        BufferedReader reader = null;
        PrintWriter writer = null;
        String pathname = "C:\\LocalVarTest.java";
        
        String rBrace = "\\s*\\)?\\s*\\{";
        String rLocalVar = "\\s*" + argPatternSingle + "\\s*" + ";";
        //String rLocalVarV = argPatternSingle + "\\s+" + "=";
        
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            writer = new PrintWriter(new File(pathname));
            byte[] b = new byte[8192];
            String buffer;
            while((buffer = reader.readLine()) != null){
                if(isMethod(buffer)){
                    inMethod = true;
                    System.out.println(buffer);
                    if(buffer.indexOf('{') != -1)
                        braces++;
                    writer.write(buffer);
                    writer.println();
                    continue;
                }
                
                if(inMethod){
                    if(buffer.indexOf('{') != -1){
                        braces++;
                    }
                    
                    if(buffer.indexOf('}') != -1){
                        braces--;
                    }
                    
                    if(braces == 0)
                        inMethod = false;
                    
                    if(buffer.matches(rLocalVar)){
                        int pos = buffer.indexOf(';');
                        if(buffer.indexOf("int") != -1 || 
                            buffer.indexOf("short") != -1 ||
                            buffer.indexOf("byte") != -1 ||
                            buffer.indexOf("long") != -1){
                            buffer = buffer.substring(0, pos) + " = 0;";
                        } else if(buffer.indexOf("float") != -1 ||
                                buffer.indexOf("double") != -1){
                            buffer = buffer.substring(0, pos) + " = 0.0;";
                        } else if(buffer.contains("boolean")){
                            buffer = buffer.substring(0, pos) + " = false;";
                        } else if(buffer.contains("char")){
                            buffer = buffer.substring(0, pos) + " = \\' \\' ;";
                        } else {
                            buffer = buffer.substring(0, pos) + " = null;";
                        } 
                    }
                }
                
                writer.write(buffer);
                writer.println();
            }
            
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            if(writer != null){
                writer.close();
            }
        }
        
    }

    public static boolean isMethod(String mSign){
        boolean b = false;
 
        // 方法签名的正则
        String methodSignaturePattern = "\\s*((public|protected|private)\\s)?\\s*(\\s*static\\s+)?(\\s*final\\s+)?" +
                typePattern + "\\s+" + namePattern + "\\s*\\(\\s*" + argsPattern + "\\s*\\)" + "\\s*(\\{)?";
 
        
//        System.out.println(methodSignaturePattern);
        b = mSign.matches(methodSignaturePattern);
        
        return b;
    }
    
    
}

 

posted @ 2016-08-28 18:05  ivantang  阅读(303)  评论(0编辑  收藏  举报