代码分析器:PMD &查找错误
1.关于 PMD:
1.1 如何设置 PMD 检查,以忽略其中一些检查,例如“变量名称太短或太长”、“删除空构造函数等” - 如果我这样做,另一个出现警告,指出该类必须有一些静态方法。基本上,该类是空的,以供以后开发,我喜欢暂时保留这种状态。
1.2 是否有必要遵循此警告建议?
A class which only has private constructors should be final
1.3 这意味着什么?
The class 'Dog' has a Cyclomatic Complexity of 3 (Highest = 17)
1.4 这个呢?我很想改变这一点,但目前我没有想到任何关于改变的事情:
Assigning an Object to null is a code smell. Consider refactoring.
2.关于 FindBugs:
2.1 在晚于其静态字段的某个时刻写入静态字段真的那么糟糕吗?宣言?以下代码给了我一个警告:
Main.appCalendar = Calendar.getInstance();
Main.appCalendar.setTimeInMillis(System.currentTimeMillis());
其中 appCalendar
是静态变量。
2.2 这段代码:
strLine = objBRdr.readLine().trim();
给出警告:
Immediate dereference of the result of readLine()
其中 objBRdr
是一个 BufferedReader(FileReader)
。会发生什么? readLine()
可能为 null? 该代码嵌套在 while (objBRdr.ready())
测试中,到目前为止,我在那里遇到的问题为零。
Update1:当我用以下代码替换代码时,2.2 已修复:
strLine = objBRdr.readLine();
if (strLine != null) {
strLine = strLine.trim();
}
1. Regarding PMD:
1.1 How do I set the PMD checks, to ignore some of them, like "Variable name is too short, or too long", "Remove empty constructor, etc" - and if I do that, another warning appears that says the class must have some static methods. Basically, the class was empty, for later development, and I like to leave it that way for now.
1.2 Is it necesarry to follow this warning advice?
A class which only has private constructors should be final
1.3 What is that supposed to mean?
The class 'Dog' has a Cyclomatic Complexity of 3 (Highest = 17)
1.4 What about this one? I would love to change this, but nothing crosses my mind at the moment regarding the change:
Assigning an Object to null is a code smell. Consider refactoring.
2.Regarding FindBugs:
2.1 Is it really that bad to write to a static field, at some point later than its declaration? The following code gives me a warning:
Main.appCalendar = Calendar.getInstance();
Main.appCalendar.setTimeInMillis(System.currentTimeMillis());
where appCalendar
is a static variable.
2.2 This code:
strLine = objBRdr.readLine().trim();
gives the warning:
Immediate dereference of the result of readLine()
where objBRdr
is a BufferedReader(FileReader)
. What could happen? readLine()
could be null?
The code is nested in while (objBRdr.ready())
test, and so far, I have zero problems there.
Update1: 2.2 was fixed when I replaced the code with:
strLine = objBRdr.readLine();
if (strLine != null) {
strLine = strLine.trim();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PMD 将规则配置存储在称为规则集 XML 文件的特殊存储库中。该配置文件包含有关当前安装的规则及其属性的信息。
这些文件位于 PMD 发行版的
rulesets
目录中。将 PMD 与 Eclipse 结合使用时,请检查自定义 PMD。所有构造函数始终以调用超类构造函数开始。如果构造函数显式包含对超类构造函数的调用,则使用该构造函数。否则隐含无参构造函数。如果无参构造函数不存在或对子类不可见,则会出现编译时错误。
因此,实际上不可能从每个构造函数都是私有的类派生子类。因此,将这样的类标记为
final
是一个好主意(但不是必需的),因为它显式地防止子类化。复杂度是方法中决策点的数量加上方法入口的1。决策点是“if”、“while”、“for”和“case labels”。一般来说,1-4 表示低复杂度,5-7 表示中等复杂度,8-10 表示高复杂度,11+ 表示非常高复杂度。
话虽如此,我只引用 聚合圈复杂度没有意义:
所以对我来说,这个 PMD 规则应该小心对待(实际上并没有多大价值)。
不确定你对此有何不明白。
我的猜测是,您会收到警告,因为该方法包含非易失性静态字段的不同步延迟初始化。而且由于编译器或处理器可能会对指令重新排序,因此如果该方法可以由多个线程调用,则不能保证线程看到完全初始化的对象。您可以使该字段变得不稳定来纠正该问题。
如果没有更多文本行可供读取,
readLine()
将返回 null 并取消引用产生空指针异常。所以你确实需要检查结果是否为空。PMD stores rule configuration in a special repository referred to as the Ruleset XML file. This configuration file carries information about currently installed rules and their attributes.
These files are located in the
rulesets
directory of the PMD distribution. When using PMD with Eclipse, check Customizing PMD.All constructors always begin by calling a superclass constructor. If the constructor explicitly contains a call to a superclass constructor, that constructor is used. Otherwise the no-argument constructor is implied. If the no-argument constructor does not exist or is not visible to the subclass, you get a compile-time error.
So it's actually not possible to derive a subclass from a class whose every constructor is private. Marking such a class as
final
is thus a good idea (but not necessary) as it explicitly prevent subclassing.The complexity is the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Having that said, I'll just quote some parts of Aggregate Cyclomatic complexity is meaningless:
So to me, this PMD rule should be taken with care (and is actually not very valuable).
Not sure what you don't get about this one.
My guess is that you get a warning because the method contains an unsynchronized lazy initialization of a non-volatile static field. And because the compiler or processor may reorder instructions, threads are not guaranteed to see a completely initialized object, if the method can be called by multiple threads. You can make the field volatile to correct the problem.
If there are no more lines of text to read,
readLine()
will return null and dereferencing that will generate a null pointer exception. So you need indeed to check if the result is null.这里有一些想法/答案
1.4 将 null 分配给对象的原因是什么?如果您重复使用相同的变量,则之前没有理由将其设置为 null。
2.1 出现此警告的原因是为了确保 Main 类的所有实例都具有相同的静态字段。在你的主课中,你可以有
静态日历 appCalendar = Calendar.getInstance() ;
关于你的2.2,你是对的,通过空检查,你确信你不会有任何NullPointerException。我们永远不知道你的 BufferedReader 何时会阻塞/垃圾,这种情况并不经常发生(根据我的经验),但我们永远不知道硬盘驱动器何时崩溃。
Here some idea / answer
1.4 What is the reason to assign null to a object? If you reuse the same variable, there's not reason to set it to null before.
2.1 The reason about this warning, is to be sure that all your instance of the class Main have the same static fields. In your Main class, you could have
static Calendar appCalendar = Calendar.getInstance() ;
about your 2.2 you're right, with the null check, you are sure that you'll not have any NullPointerException. We never know when your BufferedReader can block/trash, this doesn't happen often (in my experience) but we never know when a hard drive crash.