Findbugs, checkStyle, PMD common mistakes summary


title: Findbugs, checkStyle, PMD common mistakes summary

2019-05-12 10:46:01

personal blog


The record is to use a software engineering base experiment code audit tool (based on Eclipse) some type of error to be able to audit the majority from the network, summarized here, for easy viewing later. The main tool used here is checkstyle, findbugs, PMD, while the actual use of the tool in practice code review performed problems discovered by the images displayed.


CheckStyle:

1 Type is missing a javadoc commentClass

  缺少类型说明  

2 “{” should be on the previous line

  “{” 应该位于前一行。解决方法:把“{”放到上一行去  

3 Methos is missing a javadoc comment

  方法前面缺少javadoc注释。解决方法:添加javadoc注释,类似下述这样:
  • set default mock parameter. (instructions)
  • @param additionalParameters parameter additional (parameter name)
  • @return data manager (return value)
  • @throws Exception if has error (Exception description)

4 Expected @throws tag for “Exception”

在注释中希望有@throws的说明

Workaround: Add the following line comments before the method was: * @throws Exception if has error (Exception description)

5 “.” Is preceeded with whitespace “.”

前面不能有空格。解决方法:把“.”前面的空格去掉   

6 “.” Is followed by whitespace“.”

后面不能有空格。解决方法:把“.”后面的空格去掉    

7 “=” is not preceeded with whitespace

“=” 前面缺少空格。解决方法:在“=”前面加个空格   

8“=” is not followed with whitespace“=”

后面缺少空格。解决方法:在“=”后面加个空格     

9“}” should be on the same line“}”

应该与下条语句位于同一行。解决方法:把“}”放到下一行的前面    

10 Unused @param tag for “unused”

没有参数“unused”,不需注释

Solution: "* @param unused parameter additional (parameter name)" this comment line to remove unused parameters. "

11 Variable “CA” missing javadoc

变量“CA”缺少javadoc注释

Workaround: Add before "CA" variable javadoc comment: / ** CA. * / (note:. "" I remember a certain plus)

12 Line longer than 80characters

行长度超过80  。解决方法:把它分成多行写。必要时候,可以ctrl+shif+f        

13 Line contains a tab character

行含有”tab” 字符。    

14 Redundant “Public” modifier

冗余的“public” 修饰符 。解决方法:去掉“public”修饰符     

15 Final modifier out of order with the JSL suggestion

Final modifier的顺序错误     

16 Avoid using the “.*” form of import

import格式避免使用“.*”。解决方法:可以先不添加import,再需要用到时直接“ctrl + o”导入即可。     

17 Redundant import from the same package

从同一个包中Import内容,多余,删掉即可      

18 Unused import-java.util.list

Import进来的java.util.list没有被使用。解决方法:去掉导入的多余的类     

19 Duplicate import to line 13

重复Import同一个内容。解决方法:去掉导入的多余的类     

20 Import from illegal package

从非法包中 Import内容     

21 “while” construct must use “{}”

“while” 语句缺少“{}”      

22 Variable “sTest1” must be private and have accessor method

变量“sTest1”应该是private的,并且有调用它的方法     

23 Variable “ABC” must match pattern “1[a-zA-Z0-9]*$”

变量“ABC”不符合命名规则“^[a-z][a-zA-Z0-9]*$”,

Solution: put the name changed to conform to the rules of naming "aBC"

24 “(” is followed by whitespace

“(” 后面不能有空格 25“)”is proceeded by whitespace“)” 前面不能有空格。

Solution: the preceding or succeeding space removed

25 First sentence should end with a period

Solution: Your first line of text should be added to the end of a comment. "."

26 Redundant throws: ‘NameNotFoundException’ is subclass of ‘NamingException’.

'NameNotFoundException '是'NamingException'的子类重复抛出异常。

Solution: If the two throws exception, an exception is another subclass of class, simply write the parent
removed NameNotFoundException abnormal, corresponding javadoc comments need to remove the exception of notes

27 Parameter docType should be final.

参数docType应该为final类型, 解决方法:在参数docType前面加个final      

28 Line has trailing spaces.

多余的空行 ,解决方法:去掉这行空行    

29 Must have at least one statement.

至少一个声明    

Solution:} the catch (a NumberFormatException NFE) {
log.error ( "Auto renews The Agreement failed", NFE); // can not be empty inside capture abnormal, the abnormality which add a word. Such as printing, etc.

30 ‘>’ is not followed by whitespace.并且又有 ‘(’ is preceded with whitespace.

定义集合和枚举的时候的时候,最后一个“>”后面要有空格,“(”前面不容许有空格。
解决方法:去掉泛型    

31 Got an exception - java.lang.RuntimeException: Unable to get class information for @throws tag ‘SystemException’.

原因:不合理的throws。

Workaround: To ensure that certain types, such as certain class, interface, not throws. Remove the abnormal declared. Thrown in the implementation class

    1. This is CheckStyle reported fault. Usually it requires Refreh, clean / build this Project.
      If not, you can try to clean all projects, restart Eclipse.
    1. Because the class is not compiled in the classpath checkstyle's. So, as long as the compiled class configuration to the checkstyle / in the classpath I do not have this problem. In addition, the line length was also found checkstyle seems to have a problem, obviously no more than 120 characters, but still an error. Instead, I put Maximum line Eclipse in java> code style> formatter is changed with 100, and then format it, basically no problem.

32 File does not end with a newline.

Solution: delete the error class, a new class of the same name, copy all the code in the past

33 Utility classes should not have a public or default constructor.

接口中的内部类中不应该有公共的或者默认的构造方法

Solution: internal class, we define a private constructor, and an internal class final type. If, after front static, it must also be placed on static final

34 Variable ‘functionCode’ must be private and have accessor methods.

变量要改成private然后提供访问的方法

Solution: Give modifier these variables into private, then offer set, get method, plus the corresponding method javadoc comment, the comment parameters. And adding before final return and parameter types. And to call this variable into place by means of access

35 ‘X’ hides a field.

public class Foo
{
    private int bar;

    public Foo(int bar)
    {
        this.bar = bar;
    }

    public final int getBar()
    {
        return bar;
    }
}

Global private int bar; and local public Foo (int bar) bar of variable names repeated.
Solution: the method under which the parameters change of name on it.

36 Got an exception - Unexpected character 0xfffd in identifier

这是因为CheckStyle不能识别制定的编码格式。

Online Reference Solution:

    1. Eclipse can be configured, can specify Other-> checker in
    1. Checkstyle can modify the configuration file:
<module name="Checker">

<property name="severity" value="warning"/>

<property name="charset" value="UTF-8"/>

<module name="TreeWalker">

If it is, then UTF-8, they add that statement in bold and italic on it.

37 Got an exception - java.lang.RuntimeException: Unable to get class information for @throws tag whatever.

Solution: Check CheckSytle of JavaDoc -> Method JavaDoc -> logLoadErrors. If CheckStyle error when loading their own, make a Log on it, do not the whole Errors scary. There may also be a package out of the same mistakes. Coding Problems -> Redundant Throws -> logLoadErrors selected to

38 Expected @param tag for ‘dataManager’.

 缺少dataManager参数的注释   

Workaround: Add @param dataManager DataManager in comments

39 Parameter X should be final.

public class Foo
{
    private int bar;

    public Foo(int bar)
    {
        this.bar = bar;
    }

    public final int getBar()
    {
        return bar;
    }
}

Explanation: public Foo (int bar) local variable is considered to be immutable, final checks need to add keyword defines public Foo (final int bar) this error can be ignored without checking.

40 Redundant ‘X’ modifier.

public interface CacheHRTreeService extends Manager {

/**
  * Organization Tree
  * @param orgDto
  * @return
  * @throws Exception
  */
public void setOrganization(OrganizationDTO orgDto) throws Exception;

/**
  * Organization Tree
  * @return
  * @throws Exception
  */
public OrganizationDTO getOrganization() throws Exception;

Explanation: extra fields. public OrganizationDTO getOrganization () throws Exception; At this time, when the public is redundant field, as defined interface is public, it is necessary to check.

41 Class X should be declared as final.

Explanation: For single design pattern, returns the unique requirements of the class object. But HRFactory and ContextFactory for the optimization of the two classes, do not need to check other singleton class, still needs to be checked.

42 Method ‘addChildrenId’ is not designed for extension - needs to be abstract, final or empty.

Explanation: inherited from the parent class, which can ignore such a little special.

43 Variable ‘id’ must be private and have accessor methods.

Explained: BaseHRDTO class, parent class, subclass inherits the properties to, rather special. But the other classes, the need to add reputation scope 'private' keyword. You need to check.

44 Array brackets at illegal position.

Explanation: The wording of the code, habits are different. Need to check, only tips.


1
2


Findbugs:

FindBugs找出的bug有3中颜色:
1. 黑色的臭虫标志是分类
2. 红色的臭虫表示严重bug发现后必须修改代码
3. 橘黄色的臭虫表示潜在警告性bug 尽量修改
每次使用Find Bugs(文件,文件夹上右键--> Find Bugs-->Find Bugs)命令时,如果改动了代码,需要先Build Project;
修改完相关bug之后,在文件上右键-Find Bugs-->Clear Bug Marker清除Find Bugs的标志

2
2

PMD:

1. 潜在的bug:空的try/catch/finally/switch语句
2. 未使用的代码:未使用的局部变量、参数、私有方法等
3. 可选的代码:String/StringBuffer的滥用
4. 复杂的表达式:不必须的if语句、可以使用while循环完成的for循环
5. 重复的代码:拷贝/粘贴代码意味着拷贝/粘贴bugs
6. 循环体创建新对象:尽量不要再for或while循环体内实例化一个新对象
7. 资源关闭:Connect,Result,Statement等使用之后确保关闭掉

555
Here Insert Picture Description


  1. a-z ↩︎

Published 25 original articles · won praise 42 · views 8982

Guess you like

Origin blog.csdn.net/qq_36552566/article/details/90138853