veenter commented on PR #13015:
URL: https://github.com/apache/dubbo/pull/13015#issuecomment-1711716234

   我最新一次的提交终于让解决了sonarcloud的 
https://sonarcloud.io/organizations/apache/rules?open=java%3AS2445&rule_key=java%3AS2445&tab=why
 这个报错,但是现在的这个写法我真的是受不了,我认为这个检测是一个误报
   
   原写法是这样的,很明显没有考虑线程安全
   ```
        public static ClassPool getClassPool(ClassLoader loader) {
           if (loader == null) {
               return ClassPool.getDefault();
           }
   
           ClassPool pool = POOL_MAP.get(loader);
           if (pool == null) {
               pool = new ClassPool(true);
               pool.insertClassPath(new LoaderClassPath(loader));
               pool.insertClassPath(new DubboLoaderClassPath());
               POOL_MAP.put(loader, pool);
           }
           return pool;
       }
   ```
   
   第一次优化为下面这样,但检测出现了sonarcloud的报错
   ```
       public static ClassPool getClassPool(ClassLoader loader) {
           if (loader == null) {
               return ClassPool.getDefault();
           }
   
           ClassPool pool = POOL_MAP.get(loader);
           if (pool == null) {
               synchronized (loader) {
                   pool = POOL_MAP.get(loader);
                   if (pool == null) {
                       pool = new ClassPool(true);
                       pool.insertClassPath(new LoaderClassPath(loader));
                       pool.insertClassPath(new DubboLoaderClassPath());
                       POOL_MAP.put(loader, pool);
                   }
               }
           }
           return pool;
       }
   ```
   
   第二次我改成这样,依然不行
   ```
       public static ClassPool getClassPool(final ClassLoader loader) {
           if (loader == null) {
               return ClassPool.getDefault();
           }
   
           ClassPool pool = POOL_MAP.get(loader);
           if (pool == null) {
               synchronized (loader) {
                   pool = POOL_MAP.get(loader);
                   if (pool == null) {
                       pool = new ClassPool(true);
                       pool.insertClassPath(new LoaderClassPath(loader));
                       pool.insertClassPath(new DubboLoaderClassPath());
                       POOL_MAP.put(loader, pool);
                   }
               }
           }
           return pool;
       }
   ```
   
   最终我改成这样通过了sonarcloud检测,修改方案只是简单的赋值给一个final 变量,但是这样的写法也太愚蠢了
   ```
       public static ClassPool getClassPool(ClassLoader loader) {
           if (loader == null) {
               return ClassPool.getDefault();
           }
           
//https://sonarcloud.io/organizations/apache/rules?open=java%3AS2445&rule_key=java%3AS2445&tab=why
           final ClassLoader currentLoader = loader;
           ClassPool pool = POOL_MAP.get(currentLoader);
           if (pool == null) {
               synchronized (currentLoader) {
                   pool = POOL_MAP.get(currentLoader);
                   if (pool == null) {
                       pool = new ClassPool(true);
                       pool.insertClassPath(new LoaderClassPath(currentLoader));
                       pool.insertClassPath(new DubboLoaderClassPath());
                       POOL_MAP.put(currentLoader, pool);
                   }
               }
           }
           return pool;
       }
   ```
   
   所以我认为sonarcloud的这个检测是有问题的。
   
   而且我发现,搜索dubbo的整个代码库 类似这样的写法(锁方法参数)有好几处,不明白为何他们没被扫描到
   
   比如:
   
![image](https://github.com/apache/dubbo/assets/5327221/983298f0-758d-44bc-96f1-de2060e55105)
   
![image](https://github.com/apache/dubbo/assets/5327221/7d5ebd12-85cd-4f4f-92d7-b380750b9d8c)
   
![image](https://github.com/apache/dubbo/assets/5327221/b8da27d2-a299-4314-ba34-c80a41395af6)
   
![image](https://github.com/apache/dubbo/assets/5327221/7b1ce6e6-6d76-4af0-b53d-9a3807abecdd)
   
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org
For additional commands, e-mail: notifications-h...@dubbo.apache.org

Reply via email to