- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基于 this question看起来 CheckStyle 的默认模板将允许 if else 梯形图用换行符分隔 if
和 else
。
意思是我希望这个代码被标记为违规:
if (true)
{
System.out.println("20");
}
else
if (true)
{
System.out.println("30");
}
是否有 CheckStyle 规则来防止这种情况发生?查看文档,我没有看到任何文档,如果不需要的话,我宁愿不使用通用正则表达式规则。
此外,如果我使用 GenericIllegalRegexp 模块,多行正则表达式似乎不起作用。有什么补救办法吗?
最佳答案
我不确定您是否可以轻松编写 Checkstyle 扩展,因为 Checkstyle SDK Gui 的 AST 浏览代码之间没有任何区别:
else if
和
else
if
在每种情况下,else
都是一个 LITERAL_ELSE
,其中包含一个 if
...
因此通用正则表达式 else[\t]*[\r\n]+[\t]*if
确实是一种检测此类代码的快速方法。
修复正则表达式以包含以下情况:
当然,您不想使用 \s
空白正则表达式,因为它本身包含换行符。将空格与返回字符分开会更清楚。
注意:在 Checkstyle 5.0beta 中,不要使用“Generic Illegal Regexp”,而是使用 "Regexp" module :
您可以将该 RegExp 模块配置为“illegalPattern”(相关严重性为“警告”),并且您不必使用任何类型的“多行”标志:正则表达式就足够了。
Regexp
DescriptionA check that makes sure that a specified pattern exists, exists less than a set number of times, or does not exist in the file.
This check combines all the functionality provided by RegexpHeader, GenericIllegalRegexp and RequiredRegexp, except supplying the regular expression from a file.
It differs from them in that it works in multiline mode. It's regular expression can span multiple lines and it checks this against the whole file at once. The others work in singleline mode. Their single or multiple regular expressions can only span one line. They check each of these against each line in the file in turn.
The checkstyle AST does have line number information.
参见“Is there any Checkstyle/PMD/Findbugs rule to force “else if
” to be on the same line?”
(其中必须使用 custom check,而不是 regex match)。
关于java - 是否有 CheckStyle 规则强制 if else 关键字在 if/else 阶梯中位于同一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/387008/
发现一个类似的问题,关于 QAPlug-FindBugs 和 FindBugs-IDEA 之间的区别,但不是关于 CheckStyle... 我认为(根据以前的经验)这些插件的一个区别是 QAPlug
试图弄清楚我是否做错了什么,或者这是否是 maven checkstyle 插件中的错误。如果我执行 mvn checkstyle:check 我会得到这个: jonathanfisher@odin
我想确保我的代码不超过 5 级缩进,如下所示: class Foo { // 0 void bar() { // 1 if () { // 2 if (
FileTabCharacter检查似乎对我不起作用。下面是我的配置: .. .. .. .. .. .. 我有 XML 文件 - pom.xml和 checksty
有没有办法设置 checkstyle 来防止方法体中的前导、多个或尾随空行: 例如 private void a() { - int a = 1; doStuff(a); - - doMoreStuf
有没有办法设置 checkstyle 来防止方法体中的前导、多个或尾随空行: 例如 private void a() { - int a = 1; doStuff(a); - - doMoreStuf
我是 CheckStyle 等 Java 静态代码分析工具的新手。我下载了 Checkstyle 包并看到了 2 组检查: checkstyle_checks.xml sun_checks.xml。
我想使用//@checkstyle:off 和//@checkstyle:on 来免除代码的某些部分被 checkstyle 检查,但它仍然在提示。 public void invoke() {
我正在尝试将 import-control 添加到我们的 checkstyle 中,使 import-control 文件存在于制作 checstyle.xml 文件的项目中,而不是我们稍后构建的项目
我跑了mvn checkstyle:checkstyle ,但它没有使用我的自定义 checkstyle XML 文件。 请告诉我如何使用我自己的 checkstyle 文件而不是默认/现在配置的文件
执行命令mvn checkstyle:checkstyle时出现以下错误: An error has occurred in Checkstyle report generation. ... Cau
在我正在处理的一个项目中,我们有很多自定义 Checkstyle 检查。例如,我们使用 RegexpSingleline 模块排除所有类型的东西。我们的检查样式规则如下所示:
我正在使用 checkstyle获取有关我的源代码的报告。这个问题是关于MagicNumberCheck . 我正在使用 Date/(org.joda.)DateTime在我的源代码中是这样的: Da
我通过命令行上的 mvn 构建我的项目。 每当发生 checkStyle 错误时,我都会看到这种类型的输出: [ERROR] Failed to execute goal org.apache.mav
我有一个使用 Tycho 构建的 Eclipse 插件代码库。它使用在依赖 Artifact 中定义的父 POM,该 Artifact 指定 Checkstyle 配置的名称。 我还使用 checks
我是使用 Jenkins 的新手,我最近用它构建了一个项目并安装了“Checkstyle”插件并收到了一份报告。 我想知道如何配置“Checkstyle”以忽略空格或其他可能出现的标准? 谢谢! 最佳
我过去使用过检查样式,记得使用快捷键来应用检查样式修复(节省大量点击)。我不记得我是否为它设置了快捷方式,或者它是否已经附带了一个。 我的问题分为两部分:1)有没有一种简单的方法来检查是否有“Appl
我正在使用一个基本的 maven 项目,其中在 pom.xml 中只定义了以下内容: 依赖 javaee-api 依赖 junit 插件 maven-compile-plugin 插件 maven-w
我试图让 maven-checkstyle-plugin 对我们所有的项目使用相同的配置文件。 我尝试了几种方法,但没有一种是有效的。 唯一似乎有效的是当我将配置文件放在我的 maven 项目的根目录
我在 maven 构建中添加了 checkstyle 但由于某种原因它只检查 src/main 文件夹并忽略 src/test 文件夹,这是我的项目结构: Proj - Module A -
我是一名优秀的程序员,十分优秀!