有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java代码分析器:PMD和FindBugs

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()

其中objBRdrBufferedReader(FileReader)。会发生什么readLine()可以为空吗? 代码嵌套在while (objBRdr.ready())测试中,到目前为止,我在那里没有任何问题

更新1:2.2在我将代码替换为:

strLine = objBRdr.readLine();
    if (strLine != null) {
        strLine = strLine.trim();
    }

共 (2) 个答案

  1. # 1 楼答案

    1.1 How do i set the PMD checks [...]

    PMD将规则配置存储在称为规则集XML文件的特殊存储库中。此配置文件包含有关当前安装的规则及其属性的信息

    这些文件位于PMD发行版的rulesets目录中。在Eclipse中使用PMD时,请选中Customizing PMD

    1.2 Is it necessary to follow this warning advice?

    A class which only has private constructors should be final
    

    所有构造函数总是从调用超类构造函数开始。如果构造函数显式包含对超类构造函数的调用,则使用该构造函数。否则将隐含无参数构造函数。如果无参数构造函数不存在或对子类不可见,则会出现编译时错误

    因此,实际上不可能从每个构造函数都是私有的类中派生子类。因此,将这样的类标记为final是一个好主意(但不是必要的),因为它明确地防止了子类化

    1.3 What is that supposed to mean?

    The class 'Dog' has a Cyclomatic Complexity of 3 (Highest = 17)
    

    复杂度是一个方法中的决策点数量加上一个方法条目。决策点是“如果”、“虽然”、“对于”和“案例标签”。一般来说,1-4表示低复杂度,5-7表示中等复杂度,8-10表示高复杂度,11+表示非常高复杂度

    话虽如此,我只想引用Aggregate Cyclomatic complexity is meaningless的一些部分:

    [...] This metric only has meaning in the context of a single method. Mentioning that a class has a Cyclomatic complexity of X is essentially useless.

    Because Cyclomatic complexity measures pathing in a method, every method has at least a Cyclomatic complexity of 1, right? So, the following getter method has a CCN value of 1:

    public Account getAccount(){
       return this.account;
    }
    

    It’s clear from this boogie method that account is a property of this class. Now imagine that this class has 15 properties and follows the typical getter/setter paradigm for each property and those are the only methods available. That means the class has 30 simple methods, each with a Cyclomatic complexity value of 1. The aggregate value of the class is then 30.

    Does this value have any meaning, man? Of course, watching it over time may yield something interesting; however, on its own, as an aggregate value, it is essentially meaningless. 30 for the class means nothing, 30 for a method means something though.

    The next time you find yourself reading a copasetic aggregate Cyclomatic complexity value for a class, make sure you understand how many methods the class contains. If the aggregate Cyclomatic complexity value of a class is 200– it shouldn’t raise any red flags until you know the count of methods. What’s more, if you find that the method count is low yet the Cyclomatic complexity value is high, you will almost always find the complexity localized to a method. Right on!

    所以对我来说,这个PMD规则应该小心(实际上并不是很有价值)

    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.1 Is it really that bad to write to a static field, at some point later than its declaration? [...]

    我猜您会收到警告,因为该方法包含非易失性静态字段的非同步延迟初始化。而且,由于编译器或处理器可能会对指令进行重新排序,如果该方法可以被多个线程调用,线程就不能保证看到完全初始化的对象。可以使字段不稳定以更正问题

    2.2 [...] Immediate dereference of the result of readLine()

    如果没有更多要读取的文本行,readLine()将返回null和取消引用,这将生成null指针异常。所以你确实需要检查结果是否为空

  2. # 2 楼答案

    这里有一些想法/答案

    1.4为对象指定null的原因是什么?如果重复使用同一个变量,那么之前没有理由将其设置为null

    2.1发出此警告的原因是,确保Main类的所有实例都具有相同的静态字段。在你的主课上,你可以 静态日历appCalendar=日历。getInstance()

    关于你的2.2,你是对的,通过空检查,你确定你不会有任何NullPointerException。我们永远不知道你的BufferedReader什么时候会阻塞/垃圾,这种情况并不经常发生(以我的经验),但我们永远不知道硬盘何时会崩溃