sonarQube 扫描BUG处理

代码 BUG 处理

1. 代码问题 TradeInfo空指针问题

sonarQube 扫描出 可能会出现空指针问题

– 代码复现 DY001Service

//     sonarQube   源代码  扫描 可能会出现空指针异常       
	TradeInfo msfPrintPackMess = null; by yangkai
  if (msfPrintPackMess.getErrorList().size() == 0) {
  	// ingore;
  }

– 处理方法

修改后         TradeInfo msfPrintPackMess = (TradeInfo) tradeInfo;
2. 代码问题 finally中包含有 return

– 代码复现 YBT001Service

try {
    
    return xxx;
	
	} catch(Exception e) {
    	return xxx;
	
	} finally {
	
		return xxx;
	}

代码中 finally 不要包含 return;

– 例子如下

/**
 * @author : DJ032915
 * @description :
 * @program :
 * @date : 2023-01-16
 */
public class TryCatchTest {

    public static void main(String[] args) {
//	System.out.println("return的返回值:" + test());
        System.out.println("包含异常return的返回值:" + testWithException());
//       System.out.println("return的返回值:" + testWithObject().age); // 测试返回值类型是对象时
    }

    // finally是在return后面的表达式运算之后执行的,此时并没有返回运算之后的值
    //,而是把值保存起来,不管finally对该值做任何的改变,返回的值都不会改变,依然返回保存起来的值。
    //也就是说方法的返回值是在finally运算之前就确定了的。
    static int test() {
        int x = 1;
        try {
            return x++;

        } catch(Exception e){

        }finally {
            System.out.println("finally:" + x);//finally =2, 说明 try中return 后的表达式x++执行了
            ++x;
            System.out.println("++x:" + x);//x=3




            return x;  //执行到这里,程序便结束,此时返回的值为3,即test()执行结果为3 。说明try中return不会被执行

//编译器把finally中的return语句标识为一个warning. 警告为'return' inside 'finally' block
            //Inspection info: Reports return statements inside of finally blocks.
            //While occasionally intended, such return statements may mask thrown exceptions and complicate debugging.

//            翻译为中文
//            'finally'块中的'return'
//            检查信息:报告finally块内部的返回语句。
//            虽然偶尔是有意的,但这样的返回语句可能会掩盖抛出的异常并使调试复杂化。
//            所以 finally代码中最好不要包含return.


        }

    }

    static int testWithException(){
        Integer x = null;
        try {
            x.intValue(); // 造个空指针异常
            return x++;
        } catch(Exception e){
            e.printStackTrace();
            System.out.println("catch:" + x);// x=null
            x = 1;
            return x; // 返回1
//			return ++x; // 返回2
        }finally {
            x = 1;
            System.out.println("finally:" + x);
            ++x;
            System.out.println("++x:" + x);
            // finally代码中最好不要包含return,程序会提前退出,
            // 也就是说返回的值不是try或catch中的值
            return x;
        }
    }

    static Num testWithObject() {
        Num num = new Num();
        try {
            return num;
        } catch(Exception e){

        }finally {
            num.age++; // 改变了引用对象的值
            System.out.println("finally:" + num.age); //num.age=1

        }
        return num;
    }

    static class Num{
        public int age;
    }

}
3. 代码问题 方法返回null 是否会爆出空指针异常 答案:不会

– 代码复现

/**
 * @author : DJ032915
 * @description :
 * @program :
 * @date : 2023-01-16
 */
public class ReturnNull {
    public static void main(String[] args) {
        String value = null;
        try {
            value = (String) getNull();
            System.out.println(value);
            if (isBlank(value)) {
                System.out.println("true not have bugs");
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

    }
//    null 方法
    public static Object getNull(){
        return null;
    }
    // isBlank 是 封装的判空方法
    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

}

4. 代码问题 InterruptedException异常

– 问题复现

private void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            throw new RuntimeException(THREAD_NAME + "收到中断异常!", e);
        }

    }

–问题解决

private void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException(THREAD_NAME + "收到中断异常!", e);
        }

    }

5. 问题代码 流对象 空指针异常

– 代码复现

import java.io.IOException;
import java.io.InputStream;

/**
 * @author : DJ032915
 * @description :
 * @program :
 * @date : 2023-01-16
 */
public class StreamClose {
    public static void main(String[] args) {
        InputStream fin = null;
            try {
                fin.close();
            } catch (IOException e) {
                System.out.println("关闭流出错:" + e);
            }
    }
}

– 问题解决

import java.io.IOException;
import java.io.InputStream;

/**
 * @author : DJ032915
 * @description :
 * @program :
 * @date : 2023-01-16
 */
public class StreamClose {
    public static void main(String[] args) {
        InputStream fin = null;
        if (fin!=null) {
            try {
                fin.close();
            } catch (IOException e) {
                System.out.println("关闭流出错:" + e);
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值