Findbugs缺陷規則

@[TOC]目錄

Internationalization

1.DM_CONVERT_CASE

危險等級:關注
建議解決優先級:低
Consider using Locale parameterized version of invoked method
規則說明:考慮使用調用方法的語言環境參數化版本
A String is being converted to upper or lowercase, using the platform’s default encoding. This may result in improper conversions when used with international characters. Use the
String.toUpperCase( Locale l )
String.toLowerCase( Locale l )
versions instead.
產生原因:正在使用平臺的默認編碼將字符串轉換爲大寫或小寫。當與國際字符一起使用時,這可能會導致不適當的轉換。使用
String.toUpperCase( Locale l )
String.toLowerCase( Locale l )
版本。
解決建議及實例**:)**
錯誤代碼:

public String transCase() {
 String title = "我是誰??think in eatting..";
 title = title.toUpperCase();
 return title; 
}

改正代碼:

public String transCase() {
 String title = "我是誰??think in eatting..";
 Locale local = new Locale("zh", "CN");
 // local = Locale.getDefault();獲取默認的Locale 
title = title.toUpperCase(local);
 return title; 
}

2.DM_DEFAULT_ENCODING

危險等級:關注
建議解決優先級:高

Reliance on default encoding
規則說明:依賴默認編碼
Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behaviour to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.
產生原因:找到一個方法的調用,該方法將執行字節到字符串(或字符串到字節)的轉換,並假定默認的平臺編碼是合適的。這將導致應用程序行爲在不同平臺之間發生變化。使用替代API並顯式指定字符集名稱或字符集對象。
解決建議及實例**:)**
錯誤代碼:

public void transWithoutEncoding() {
 byte[] b = "你是誰?".getBytes();//findbugs報錯
 String question = new String(b);//findbugs報錯System.out.println(question); 
}

改正代碼:

public void transEncoding() throws UnsupportedEncodingException {
 byte[] b = "你是誰?".getBytes("UTF-8");//指定編碼格式
 String question = new String(b, "UTF-8");
 System.out.println(question); 
}

我是帥哥

什麼是帥哥

我是大帥哥

帥哥的定義

我比你帥

孤獨寂寞冷
人生真是寂寥如雪
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章