Several common findbugs problems

1. USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES lock
2. WEM_WEAK_EXCEPTION_MESSAGING log details

     For example, the log record is as follows, log.info("file not found."), this information is very abstract, we need to add information about which file cannot be found, log.info("file {} not found.", file). Variable information must be added to the log by means of variable substitution.

3. EQ_OVERRIDING_EQUALS_NOT_SYMMETRIC The base class has @Data annotation, and the subclass also has @Data annotation
4. NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
      desDirPath = zipPath.getParent().toAbsolutePath().toString()
      ;

if (StringUtils.isNotBlank(zipPath)) {
    String parentPath = zipPath.getParent();
    if (StringUtils.isNotBlank(parentPath)) {
    
       String absPath = parentPath.toAbsolutePath();
       if (StringUtils.isNotBlank(absPath)) {
          desDirPath = absPath.toString();
       }
    }
}

5. IS2_INCONSISTENT_SYNC needs to be synchronized, directly add the synchronized keyword to the method declaration.
6. WMI_WRONG_MAP_ITERATOR uses entrySet to traverse

7. LSC_LITERAL_STRING_COMPARISON
      if (childProp != null && !childProp.equals("")) { // Put "" in front

       Although it was judged that childProp is not null, we'd better develop this habit. The constant information needs to be defined at the beginning, and the comparison here needs to advance the constant "". Generally speaking, it is recommended to use some tools and methods. Here, StringUtils.isNotBlank(childProp) can be done with one line of code.

8. SIC_INNER_SHOULD_BE_STATIC_ANON Do not use anonymous inner classes.

9. If an exception is thrown inside the DRE_DECLARED_RUNTIME_EXCEPTION class, the method should not declare that an exception is thrown.
10. UTWR_USE_TRY_WITH_RESOURCES handles closing an automatically closed resource.

      Using the try(){}catch(){} method to operate the stream, that is, try-with-resources, there is no need to manually close the stream.

11. UCPM_USE_CHARACTER_PARAMETERIZED_METHOD single character problem

Method passes constant String of length 1 to character overridden method
This method passes a constant literal String of length 1 as a parameter to a method, when a similar method is exposed that takes a char. 
It is simpler and more expedient to handle one character, rather than a String.

    In StringBuffer and StringBuilder adding string operations, if you encounter a single string, such as " " or "a", if you still use sb.append("") or sb.append("a"), it is generally recommended to use sb .append('') or sb.append('a') instead, although the two methods are similar, there are still differences, one is a string and the other is a character. When using the string indexOf() method, it is similar. If it is a single string, it is also recommended to use characters to find the index.

Guess you like

Origin blog.csdn.net/feinifi/article/details/131737461