欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

checkstyle 解决 没有注释 没有括号问题 工具

程序员文章站 2022-03-27 14:21:15
...

 

checkstyle  解决 没有类注释  没有行注释  if else 没有括号问题 ,可以使用下面工具类,进行自动添加 ,节省修改时间 

 

 

 

package com.xxx.checkstyle;

public class SearchResult {
    String filename;
    String lineString;
    String error;
    int line;
}

 

package com.xxx.checkstyle;

public interface IProcessLine {
    public String processLine(String line);
    
    public int getLineNumber(SearchResult result);
}

 

package com.xxx.checkstyle;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.log4j.Logger;

public class CheckStyleResultFileParser {

    private static final Logger logger = Logger
            .getLogger(CheckStyleResultFileParser.class);
    File f;
    BufferedReader fileReader;

    String currentFileName;

    public CheckStyleResultFileParser(String filename)
            throws FileNotFoundException {
        this.f = new File(filename);
        fileReader = new BufferedReader(new FileReader(f));
    }

    public SearchResult find(String type) throws IOException {
        String s;
        if (!this.fileReader.ready())
            return null;
        while ((s = this.nextLine()) != null) {
            s = s.trim();
            if (s.startsWith("<file")) {
                /*
                 * <filename=
                 * "C:\work\dev\proj\newsvn\java/util\aoi-protocol\trunk\src\main\java\com\cmcc\rd\aoi\log\LoggerFactory.java""
                 * >
                 */
                int startIndex = s.indexOf("name=\"");
                int endIndex = s.indexOf("\"", startIndex + 6);
                this.currentFileName = s.substring(startIndex + 6, endIndex);
            } else if (s.startsWith("<error")) {
                /*
                 * <error line="31" column="5" severity="error" message="Missing a Javadoc comment."
                 * source="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck"/>
                 */
                if (s.indexOf(type) > 0) {
                    SearchResult result = new SearchResult();
                    int startIndex = s.indexOf("line=\"");
                    int endIndex = s.indexOf("\"", startIndex + 6);
                    String lineString = s.substring(startIndex + 6, endIndex);
                    result.line = Integer.parseInt(lineString);
                    result.error = type;
                    result.filename = this.currentFileName;
                    result.lineString = s;
                    return result;
                }
            }
        }
        this.fileReader.close();
        return null;
    }

    private String nextLine() throws IOException {
        return this.fileReader.readLine();
    }
    
  

}

 

package com.xxx.checkstyle;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class LineFileModifier {

    private static final Logger logger = Logger.getLogger(LineFileModifier.class);

    public boolean process(List<SearchResult> list, IProcessLine processor) {
        if (list == null || list.size() == 0)
            return true;
        else {
            // 读文件,先把文件每行存到一个 list里
            File file = new File(list.get(0).filename);
            logger.warn(" ===============================");
            logger.warn(" FileName: " + list.get(0).filename + ", add number: " + list.size());
            logger.warn(" ===============================");
            BufferedReader reader = null;
            List<String> lineList = new ArrayList<String>();
            try {
                reader = new BufferedReader(new FileReader(file));
                String s = null;
                while ((s = reader.readLine()) != null) {
                    lineList.add(s);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally {
                if (reader != null)
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
            // 将文件按需求处理
            for (SearchResult result : list) {
                String tmp = lineList.remove(processor.getLineNumber(result));
                tmp = processor.processLine(tmp);
                lineList.add(processor.getLineNumber(result), tmp);
            }
            // 打印日志,不是必须的
            for (String line : lineList) {
                logger.debug(line);
            }
            // 把list内容写回文件中
            FileWriter writer = null;
            try {
                writer = new FileWriter(file);
                for (String s : lineList) {
                    writer.write(s);
                    writer.write("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (writer != null)
                    try {
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
        return true;
    }
}

 

package com.xxx.checkstyle;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class ProcessNeedJavaDocComment implements IProcessLine {

    private static final Logger logger = Logger.getLogger(ProcessNeedJavaDocComment.class);
    String type = "JavadocMethodCheck";

    public ProcessNeedJavaDocComment() {
    }

    public void run(String filename) throws Exception {
        CheckStyleResultFileParser parser = new CheckStyleResultFileParser(filename);
        SearchResult result;
        SearchResult lastResult = null;
        List<SearchResult> list = new ArrayList<SearchResult>();
        LineFileModifier moFileModifier = new LineFileModifier();
        while ((result = parser.find(type)) != null) {
            logger.debug(result.filename);
            logger.debug(result.lineString);
            logger.debug(result.line);
            if (lastResult == null) {
                list.add(result);
            } else if (lastResult.filename.equals(result.filename)) {
                list.add(result);
            } else {
                process(moFileModifier, list);
                list.add(result);
            }
            lastResult = result;
        }
        process(moFileModifier, list);
    }

    public void process(LineFileModifier modifier, List<SearchResult> list) {
        modifier.process(list, this);
        list.clear();
    }

    public static void main(String[] args) {

        String checkstyleResultFile = "I:\\cache\\ea-ws\\FUSIONDataEngine\\target\\checkstyle-result.xml";
        try {
            new ProcessNeedJavaDocComment().run(checkstyleResultFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String processLine(String line) {
        line = line.trim();
        if (line.startsWith("/*") || line.startsWith("*/"))
            return line;
        else {
            return "/**   */" + line ;
        }
    }

    @Override
    public int getLineNumber(SearchResult result) {
        return result.line -1 ;
    }
}

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。


checkstyle 解决 没有注释  没有括号问题 工具
            
    
    博客分类: checkstyle checkstyle没有注释没有括号问题工具 checkstyle 解决 没有注释  没有括号问题 工具
            
    
    博客分类: checkstyle checkstyle没有注释没有括号问题工具 checkstyle 解决 没有注释  没有括号问题 工具
            
    
    博客分类: checkstyle checkstyle没有注释没有括号问题工具 
 
 
 谢谢您的赞助,我会做的更好!