首页 > 其他分享 >SonarLint实践总结

SonarLint实践总结

时间:2024-05-21 15:41:23浏览次数:21  
标签:总结 ... smell void 实践 SonarLint ThreadLocal Noncompliant public

SonarLint实践总结

1、集合要用isEmpty()判空。

Minor code smell

Use isEmpty() to check whether the collection is empty or not.

问题代码:

 

 

 Rule:

复制代码
Using Collection.size() to test for emptiness works, but using Collection.isEmpty() makes the code more readable and can be more performant. 
The time complexity of any isEmpty() method implementation should be O(1) whereas some implementations of size() can be O(n). Noncompliant Code Example  if (myCollection.size() == 0) { // Noncompliant    /* ... */  }   Compliant Solution  if (myCollection.isEmpty()) {    /* ... */  }
复制代码

 

2、重复的字符串要定义常量。

Critical code smell

Define a constant instead of duplicating this literal "total" 3 times.

rule:

复制代码
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
  public void run() {    
   prepare("action1"); // Noncompliant - "action1" is duplicated 3 times    execute("action1");    
   release("action1"); 
 }    

@SuppressWarning("all") // Compliant - annotations are excluded  private void method1() { /* ... / }  
@SuppressWarning("all")  
private void method2() { /
... */ }    
public String method3(String a) {    
  System.out.println("'" + a + "'"); // Compliant - literal "'" has less than 5 characters and is excluded    
  return ""; // Compliant - literal "" has less than 5 characters and is excluded  }  
Compliant Solution
 private static final String ACTION_1 = "action1"; // Compliant    public void run() {    
  prepare(ACTION_1); // Compliant    execute(ACTION_1);   release(ACTION_1);  
}  
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.

复制代码

 

3、方法不要返回null

Major code smell

Return an empty collection instead of null.

问题代码:

 

 rule:

复制代码
Returning null instead of an actual array or collection forces callers of the method to explicitly test for nullity, making them more complex and less readable.
Moreover, in many cases, null is used as a synonym for empty.
Noncompliant Code Example
  public static List<Result> getResults() {
    return null; // Noncompliant
  }
    public static Result[] getResults() {
    return null; // Noncompliant
  }
    public static void main(String[] args) {
    Result[] results = getResults();
      if (results != null) { // Nullity test required to prevent NPE 
     for (Result result: results) {        /* ... */      }
    }
  }    
Compliant Solution
  public static List<Result> getResults() {
    return Collections.emptyList(); // Compliant
  }
    public static Result[] getResults() {
    return new Result[0];
  }
    public static void main(String[] args) {
    for (Result result: getResults()) {      /* ... */    }
  }  
See
CERT, MSC19-C. - For functions that return an array, prefer returning an empty array over a null value
CERT, MET55-J. - Return an empty array or collection instead of a null value for methods that return an array or collection
复制代码

 

4、父类的静态成员不应该使用子类访问。

Critial code smell

Use static access with "com.alibaba.fastjson.JSON" for "parseObject".

问题代码:

修改方案:

 rule:

复制代码
"static" base class members should not be accessed via derived types

Code smell

Critical
squid:S3252

In the interest of code clarity, static members of a base class should never be accessed using a derived type's name. Doing so is confusing and could create the illusion that two different static members exist.
Noncompliant Code Example
 class Parent {
   public static int counter;
 }
   class Child extends Parent {
   public Child() {
     Child.counter++; // Noncompliant
   }
 }  
Compliant Solution
 class Parent {
   public static int counter;
 }
   class Child extends Parent {
   public Child() {
     Parent.counter++;
   }
 }

复制代码

 

5、Boolean包装类应该避免在表达式中使用。

Minor code smell

Use the primitive boolean expression here.

问题代码:

 修改方案:

rule:

复制代码
Boxed "Boolean" should be avoided in boolean expressions

Code smell

Minor
squid:S5411

When boxed type java.lang.Boolean is used as an expression it will throw NullPointerException if the value is null as defined in Java Language Specification §5.1.8 Unboxing Conversion.
It is safer to avoid such conversion altogether and handle the null value explicitly.
Noncompliant Code Example
 Boolean b = getBoolean();
 if (b) { // Noncompliant, it will throw NPE when b == null
   foo();
 } else {
   bar();
 }  
Compliant Solution
 Boolean b = getBoolean();
 if (Boolean.TRUE.equals(b)) {
   foo();
 } else {
   bar(); // will be invoked for both b == false and b == null
 }  
See
* Java Language Specification §5.1.8 Unboxing Conversion

复制代码

 

6、

Minor code smell

Remove this use of "Integer";it is deprecated.

问题代码:

 

 修改方案:

rule:

复制代码
"@Deprecated" code should not be used

Code smell

Minor
squid:CallToDeprecatedMethod

Once deprecated, classes, and interfaces, and their members should be avoided, rather than used, inherited or extended. Deprecation is a warning that the class or interface has been superseded, and will eventually be removed. The deprecation period allows you to make a smooth transition away from the aging, soon-to-be-retired technology.
Noncompliant Code Example
 /   * @deprecated As of release 1.3, replaced by {@link #Fee}   */  @Deprecated  public class Fum { ... }
   public class Foo {
   /
    * @deprecated As of release 1.7, replaced by {@link #doTheThingBetter()}     */
   @Deprecated
   public void doTheThing() { ... }
     public void doTheThingBetter() { ... }
 }
   public class Bar extends Foo {
   public void doTheThing() { ... } // Noncompliant; don't override a deprecated method or explicitly mark it as @Deprecated
 }
   public class Bar extends Fum { // Noncompliant; Fum is deprecated      public void myMethod() {
     Foo foo = new Foo(); // okay; the class isn't deprecated      foo.doTheThing(); // Noncompliant; doTheThing method is deprecated
   }
 }  
See
MITRE, CWE-477 - Use of Obsolete Functions
CERT, MET02-J. - Do not use deprecated or obsolete classes or methods

复制代码

 

7、不要通过创建随机对象的方式获取随机值。

Critical code smell

Save and re-use this "Random".

问题代码:

 

修改方案:

 

 

 rule:

复制代码
"Random" objects should be reused

Bug

Critical
squid:S2119

Creating a new Random object each time a random value is needed is inefficient and may produce numbers which are not random depending on the JDK.
For better efficiency and randomness, create a single Random, then store, and reuse it.
The Random() constructor tries to set the seed with a distinct value every time. However there is no guarantee that the seed will be random or even
uniformly distributed. Some JDK will use the current time as seed, which makes the generated numbers not random at all.
This rule finds cases where a new Random is created each time a method is invoked and assigned to a local random variable.
Noncompliant Code Example
 public void doSomethingCommon() {
   Random rand = new Random(); // Noncompliant; new instance created with each invocation
   int rValue = rand.nextInt();
   //...  
Compliant Solution
 private Random rand = SecureRandom.getInstanceStrong(); // SecureRandom is preferred to Random
   public void doSomethingCommon() {
   int rValue = this.rand.nextInt();
   //...  
Exceptions
A class which uses a Random in its constructor or in a static main function and nowhere else will be ignored by this rule.
See
OWASP Top 10 2017 Category A6 - Security Misconfiguration

复制代码

 

8、方法的复杂度不能太高。

Critical code smell

Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.

修改方案:

(重构这种方法,将其认知复杂性从22降低到15。)

表示一个方法行数很多比较复杂,同样的解决办法就是抽取方法,讲一个方法拆分几个方法。

 rule:

复制代码
Cognitive Complexity of methods should not be too high

Code smell

Critical
squid:S3776

Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.
See
Cognitive Complexity

复制代码

 

9、在循环中不要多次使用break和continue语句。

Minor code smell

Reduce the total number of break and continue statements in this loop to use at most one.
在这个循环中减少break和continue语句的总数,最多使用一个。

rule:

复制代码
Loops should not contain more than a single "break" or "continue" statement

Code smell

Minor
squid:S135

Restricting the number of break and continue statements in a loop is done in the interest of good structured programming.
One break and continue statement is acceptable in a loop, since it facilitates optimal coding. If there is more than one, the code should be refactored to increase readability.
Noncompliant Code Example
 for (int i = 1; i <= 10; i++) { // Noncompliant - 2 continue - one might be tempted to add some logic in between
   if (i % 2 == 0) {
     continue;
   }
     if (i % 3 == 0) {
     continue;
   }
     System.out.println("i = " + i);
 }

复制代码

 

10、局部变量应该遵守命名规则。

Minor code smell

Rename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'

问题代码:

String access_token = weChatUtil.getAccessToken();

修改方案:

String accessToken = weChatUtil.getAccessToken();

rule:

复制代码
Local variable and method parameter names should comply with a naming convention

Code smell

Minor
squid:S00117

Shared naming conventions allow teams to collaborate effectively. This rule raises an issue when a local variable or function parameter name does not match the provided regular expression.
Noncompliant Code Example
With the default regular expression [1][a-zA-Z0-9]*$:
 public void doSomething(int my_param) {
   int LOCAL;
   ...
 }  
Compliant Solution
 public void doSomething(int myParam) {
   int local;
   ...
 }  
Exceptions
Loop counters are ignored by this rule.
 for (int i_1 = 0; i_1 < limit; i_1++) { // Compliant
   // ...
 }  
as well as one-character catch variables:
 try {
 //...
 } catch (Exception e) {
// Compliant
 }

复制代码

 

 11、"ThreadLocal" variables should be cleaned up when no longer used

Major code smell

Call "remove()" on "SyncHttpClients".

问题代码:

复制代码
1     //同步
2     private static ThreadLocal<CloseableHttpClient> SyncHttpClients = new ThreadLocal<CloseableHttpClient>() {
3         @Override
4         protected CloseableHttpClient initialValue() {
5             return HttpClients.createDefault();
6         }
7 
8     };
复制代码

修改方案:

rule:

复制代码
"ThreadLocal" variables should be cleaned up when no longer used

Bug

Major
squid:S5164

ThreadLocal variables are supposed to be garbage collected once the holding thread is no longer alive. Memory leaks can occur when holding threads are re-used which is the case on application servers using pool of threads.
To avoid such problems, it is recommended to always clean up ThreadLocal variables using the remove() method to remove the current thread’s value for the ThreadLocal variable.
In addition, calling set(null) to remove the value might keep the reference to this pointer in the map, which can cause memory leak in some scenarios. Using remove is safer to avoid this issue.
Noncompliant Code Example
 public class ThreadLocalUserSession implements UserSession {      private static final ThreadLocal<UserSession> DELEGATE = new ThreadLocal<>();
   public UserSession get() {
     UserSession session = DELEGATE.get();
     if (session != null) {
       return session;
     }
     throw new UnauthorizedException("User is not authenticated");
   }
   public void set(UserSession session) {
     DELEGATE.set(session);
   }
    public void incorrectCleanup() {
      DELEGATE.set(null); // Noncompliant
    }
     // some other methods without a call to DELEGATE.remove()
 }  
Compliant Solution
 public class ThreadLocalUserSession implements UserSession {      private static final ThreadLocal<UserSession> DELEGATE = new ThreadLocal<>();
     public UserSession get() {
     UserSession session = DELEGATE.get();
     if (session != null) {
       return session;
     }
     throw new UnauthorizedException("User is not authenticated");
   }
    public void set(UserSession session) {
     DELEGATE.set(session);
   }
   public void unload() {
     DELEGATE.remove(); // Compliant
   }
     // ...
 }  
Exceptions
Rule will not detect non-private ThreadLocal variables, because remove() can be called from another class.
See
Understanding Memory Leaks in Java

复制代码

 

12、用ThreadLocal.withInitial创建匿名内部类

Minor code smell

Replace this anonymous class with a call to "ThreadLocal.withInitial".

rule:

复制代码
"ThreadLocal.withInitial" should be preferred

Code smell

Minor
squid:S4065

Java 8 introduced ThreadLocal.withInitial which is a simpler alternative to creating an anonymous inner class to initialise a ThreadLocal instance.
This rule raises an issue when a ThreadLocal anonymous inner class can be replaced by a call to ThreadLocal.withInitial.
Noncompliant Code Example
 ThreadLocal<List<String>> myThreadLocal =
     new ThreadLocal<List<String>>() { // Noncompliant
         @Override
         protected List<String> initialValue() {
             return new ArrayList<String>();
         }
 };  
Compliant Solution
 ThreadLocal<List<String>> myThreadLocal = ThreadLocal.withInitial(ArrayList::new);

复制代码

 

13、Replace this lambda with a method reference.

Minor code smell

Replace this lambda with a method reference.

问题代码:

    //同步
    private static ThreadLocal<CloseableHttpClient> syncHttpClients = ThreadLocal.withInitial(() -> HttpClients.createDefault());

修改方案:

    //同步
    private static ThreadLocal<CloseableHttpClient> syncHttpClients = ThreadLocal.withInitial(HttpClients::createDefault);

rule:

复制代码
Lambdas should be replaced with method references

Code smell

Minor
squid:S1612

Method/constructor references are more compact and readable than using lambdas, and are therefore preferred. Similarly, null checks can be replaced with references to the Objects::isNull and Objects::nonNull methods.
Note that this rule is automatically disabled when the project's sonar.java.source is lower than 8.
Noncompliant Code Example
 class A {
   void process(List<A> list) {
     list.stream()
       .map(a -> a.<String>getObject())
       .forEach(a -> { System.out.println(a); });
   }
     <T> T getObject() {
     return null;
   }
 }  
Compliant Solution
 class A {
   void process(List<A> list) {
     list.stream()
       .map(A::<String>getObject)
       .forEach(System.out::println);
   }
     <T> T getObject() {
     return null;
   }
 }

复制代码

 

14、Remove this array creation and simply pass the elements.

Minor code smell

Remove this array creation and simply pass the elements.

问题代码:

ALL(0, "全部", Arrays.asList(new Integer[]{15,16,20}))

修改方案:

ALL(0, "全部", Arrays.asList(15,16,20))

rule:

复制代码
Arrays should not be created for varargs parameters

Code smell

Minor
squid:S3878

There's no point in creating an array solely for the purpose of passing it as a varargs (...) argument; varargs is an array. Simply pass the elements directly. They will be consolidated into an array automatically.
Incidentally passing an array where Object ... is expected makes the intent ambiguous: Is the array supposed to be one object or a collection of objects?
Noncompliant Code Example
 public void callTheThing() {
   //...
   doTheThing(new String[] { "s1", "s2"}); // Noncompliant: unnecessary
   doTheThing(new String[12]); // Compliant
   doTheOtherThing(new String[8]); // Noncompliant: ambiguous
   // ...
 }
   public void doTheThing (String ... args) {
   // ...
 }
   public void doTheOtherThing(Object ... args) {
   // ...
 }  
Compliant Solution
 public void callTheThing() {
   //...
   doTheThing("s1", "s2");
   doTheThing(new String[12]);
   doTheOtherThing((Object[]) new String[8]);
    // ...
 }
   public void doTheThing (String ... args) {
   // ...
 }
   public void doTheOtherThing(Object ... args) {
   // ...
 }

复制代码

 

 

结束。

分类: 插件
<div id="blog_post_info">
好文要顶 关注我 收藏该文 微信分享 金色的鱼儿
粉丝 - 18 关注 - 4
+加关注 1 0 升级成为会员
<a href="https://www.cnblogs.com/it-deepinmind/p/13049388.html" class="p_n_p_prefix">« </a> 上一篇:    <a href="https://www.cnblogs.com/it-deepinmind/p/13049388.html" data-featured-image="" title="发布于 2020-06-05 14:03">Java代码规范与质量检测插件SonarLint</a>
<br>
<a href="https://www.cnblogs.com/it-deepinmind/p/13050650.html" class="p_n_p_prefix">» </a> 下一篇:    <a href="https://www.cnblogs.com/it-deepinmind/p/13050650.html" data-featured-image="" title="发布于 2020-06-05 16:50">Java开发中的23种设计模式详解(转)</a>
发表于 2020-06-05 15:19  金色的鱼儿  阅读(29465)  评论(1)  编辑  收藏  举报
        </div>
    </div>

原文链接:https://www.cnblogs.com/it-deepinmind/p/13049913.html


  1. a-z ↩︎

标签:总结,...,smell,void,实践,SonarLint,ThreadLocal,Noncompliant,public
From: https://www.cnblogs.com/sunny3158/p/18204186

相关文章

  • Kubernetes 数据存储:从理论到实践的全面指南
    本文深入解析Kubernetes(K8S)数据存储机制,探讨其架构、管理策略及最佳实践。文章详细介绍了K8S数据存储的基础、架构组成、存储卷管理技巧,并通过具体案例阐述如何高效、安全地管理数据存储,同时展望了未来技术趋势。关注【TechLeadCloud】,分享互联网架构、云服务技术的全......
  • 树链剖分总结
    本博客为本人对树剖可解决题目的总结。。。概念1:\(fa[u]\):\(u\)的父亲2:\(size[u]\):\(u\)节点\(u\)为子树的节点个数3:\(dep[u]\):\(u\)节点的深度4:\(wson[u]\):\(u\)节点的重儿子编号5:\(top[u]\):\(u\)节点所在重链的顶部端点6:\(dfn[u]\):\(u\)节点......
  • 2020年迟到的年终总结
    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解`2020年迟到的年终总结日期:2021-1-26阿珏谈天说地浏览:545次评论:7条我一直在想究竟要不要写年终总结,又该写点什么好呢纠结了一个多月,这......
  • 跨端自渲染绘制的实践与探索
    在过去的大半年中,我一直投身于一个跨端自渲染项目的研发工作中,负责其中的渲染模块。现在通过这篇文章,我想记录并分享我们在这个项目中的经验和挑战,希望能为大家日常开发中的涉及到渲染相关的工作带来一些启发和帮助。跨端自渲染的初衷跨端自渲染项目的愿景在于构建一个后端渲......
  • Stream流常用方法总结
    Stream流思想:先得到集合或者数组的Stream流(就是一根传送带);把元素放上去;然后就用这个Stream流简化的API来方便的操作元素。 Stream流的三类方法:1、获取Stream流:创建一条流水线,并把数据流放到流水线上准备进行操作;2、中间方法:流水线上的操作,一次操作完毕之后,还可以继续进行其......
  • 20240520刷题总结
    T1(状态置换,搜索与dp,dp存值结构体)T376。还是从搜索角度去考虑:时间,前i物品,最多拿多少。这样我们去设计状态,我一开始设置:时间,前i,值是拿多少。会发现这样会爆。其实换一下,优化效果更好。前i物品,最多拿j,用的最少时间。实际转移就是背包。存值就存结构体。#include<iostream>......
  • 登录页面漏洞总结
    汇总一些项目中挖到过的,和做项目的时候会用到的测试方法,最近也是疲于面试,讲的是时候总是一时想不起来,所以决定还是总结一下吧,以后我还是要多放网络安全相关的,面试官看时候也知道我了解哪些点,但奈何笔记太多需要整理一下再放出来,以前不敢放是因为确实一直觉得自己太菜了。如果后面......
  • 5_20总结
    tomcat设置①On'Update'action:从字面上理解就是手工触发update动作的时候做什么updateresources ----更新静态的资源,比如html,js,css等运行模式和调试模式都是立即生效。updateclassesandresources ----更新java,jsp和静态资源java修改后,会被编译成.cla......
  • 蟒蛇书(Python编程:从入门到实践)第17章使用API 17.1.4处理API响应报错Caused by ProxyEr
    书上提供的原始代码:importrequests#执行API调用并存储响应url='https://api.github.com/search/repositories?q=language:python&sort=stars'headers={'Accept':'application/vnd.github.v3+json'}r=requests.get(url,headers=headers)prin......
  • 推进 OKR 目标管理落地的最佳实践
    企业如何通过Tita推进OKR目标管理落地?本文将从OKR推进落地的四大阶段,来分别为大家介绍企业推进的全流程。这四个阶段分别是:OKR实施准备阶段OKR制定阶段OKR执行推阶段OKR 复盘阶段准备:OKR实施准备阶段OKR实施准备阶段是开始部署和准备落地OKR的关键阶段。在......