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捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
相关推荐
一个优秀的java代码规则检查工具。 可以上官方网站看看使用手册。 地址如下:xxx.com
checkStyle java代码检查工具
代码质量检查工具,elipse中可安装插件checkstyle
CheckStyle 代码优化工具 代码格式 java格式
checkstyle findbugs 包含幻灯片,资料全
介绍 Checkstyle是一个开发工具,可以帮助编程人员编写符合编码规范的java 代码 。程序员都非常讨厌程序的check工作,checkstyle能够自动地完成这项烦琐但又非常重要的工作 ,从而节省了的程序员的时间,也提高了...
Checkstyle是一个开发工具,可以帮助编程人员编写符合编码规范的java 代码
包括Checkstyle在Eclipse中和Maven中的配置详解,下了绝不后悔,一文在手,开发不愁
在本系列 八月份 的那期文章中,我得出了这样的结论:将检验工具集成到构建过程(例如,使用 Ant 或 Maven)中,能够建立起一种寻找潜在缺陷的方法。尽管这种方法使一致性成为可能并超越了 IDE,但它也有一点反作用...
myeclipse的checkstyle文件,将压缩包中的两个文件夹下的内容复制到myeclipse对应的文件夹下
基于Checkstyle XML报告注释请求请求 将转换为GitHub Pull Request 。 该脚本旨在在您的GitHub Action中使用。 这意味着您不再需要通过GitHub Action日志文件进行搜索。 无需解释每种工具格式不同的消息。 相反,...
代码检测工具(侧重于代码的风格检测,如缩进、空行、花括号的匹配等等) 下载地址:https://github.com/checkstyle/checkstyle/releases/
代码走查工具CheckStyle插件的安装和使用
最新版本的checkstyle插件,很好的代码走查工具,附带安装步骤.
checkstyle插件使用文档
已在myeclipse10.5中安装成功
CheckStyle代码检查工具使用说明
findbugs和Checkstyle安装包