#cec726
1:String concatenation as argument to 'StringBuilder.append()'
詳細提示
String concatenation as argument to 'StringBuilder.append()' call less... (Ctrl+F1)
Inspection info: Reports String concatenation used as the argument to StringBuffer.append(), StringBuilder.append() or Appendable.append(). Such calls may profitably be turned into chained append calls on the existing StringBuffer/Builder/Appendable, saving the cost of an extra StringBuffer/Builder allocation.
This inspection ignores compile time evaluated String concatenations, which when converted to chained append calls would only worsen performance.
解釋
append方法就是為了解決String += String的效率問題的,而上面代碼在append方法里使用了字符串連接,可能降低性能。解決提示:不要在append方法里寫字符串連接代碼。
2:C-style array declaration of local variable 'arr' less...
詳細提示
C-style array declaration of local variable 'arr' less... (Ctrl+F1)
Inspection info: Reports array declarations made using C-style syntax, with the array indicator brackets positioned after the variable name or after the method parameter list. For example:
public String process(String value[])[] {
return value;
}
Most code styles prefer Java-style array declarations, with the array indicator brackets attached to the type name.
解釋
數組對象,把中括號放在變量后面是C語言的風格,而Java習慣放在變量類型后面
3:'for' loop replaceable with 'foreach' less...
詳細提示
'for' loop replaceable with 'foreach' less... (Ctrl+F1)
Inspection info: Reports for loops which iterate over collections or arrays, and can be replaced with the foreach iteration syntax, available in Java 5 and newer.
解釋
循環遍歷集合或數組,可以替換為foreach迭代語法
4:Local variable 's' is redundant less...
詳細提示
Local variable 's' is redundant less... (Ctrl+F1)
Inspection info: Reports unnecessary local variables, which add nothing to the comprehensibility of a method. Variables caught include local variables which are immediately returned, local variables that are immediately assigned to another variable and then not used, and local variables which always have the same value as another local variable or parameter.
解釋
定義的變量名稱是冗余的
5:Field injection is not recommended less...
詳細提示
Field injection is not recommended less... (Ctrl+F1)
Inspection info: Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".
解釋
Spring團隊建議:“始終在bean中使用基於構造函數的依賴注入
6:Found duplicate code
鼠標放在重復代碼塊,Alt+Enter
查看其它重復代碼,可以做比較
7:Call to 'toArray()' with pre-sized array argument 'new String[prdCodeList.size()]'
詳細提示
Call to 'toArray()' with pre-sized array argument 'new String[prdCodeList.size()]' less... (Ctrl+F1)
Inspection info: There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).
In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.
This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).
解釋
有兩種樣式將集合轉換成數組:
1. c.toArray(new String[c.size()])
2. c.toArray(new String[0])
在舊的Java版本中,建議使用預大小的數組,因為創建適當大小的數組所必需的反射調用非常慢。
JDK6開始,與預設置大小的版本相比,空數組版本的性能相同,有時甚至更好。現在推薦使用空數組版本
8:'if' statement can be simplified
詳細提示
'if' statement can be simplified less... (Ctrl+F1)
Inspection info: Reports if statements which can be simplified to single assignment, return or assert statements.
For example:
if (foo()) {
return true;
} else {
return false;
}
can be simplified to
return foo();
解釋
因為你if語句里的判斷結果就是Boolean類型的,所以你不必脫了褲子放屁。【JDK源碼和Spring源碼,這種代碼隨處可見】
9:Can be replaced with single 'Map.putIfAbsent' method call
詳細提示
Can be replaced with single 'Map.putIfAbsent' method call less... (Ctrl+F1)
Inspection info: Reports calls to Map.get() which could be replaced with getOrDefault(), computeIfAbsent() or putIfAbsent().
Map.getOrDefault method could be used to replace the code like this:
String val = map.containsKey(key) ? map.get(key) : "none";
Map.computeIfAbsent method could be used to replace the code like this:
List<String> list = map.get(key);
if (list == null) {
list = new ArrayList<>();
map.put(key, list);
}
Map.putIfAbsent method could be used to replace the code like this:
String val = map.get(key);
if (val == null) map.put(key, newVal);
Map.merge method could be used to replace the code like this:
Integer val = map.get(key);
if (val == null) map.put(key, 1);
else map.put(key, val + 1);
Note that replacement with computeIfAbsent() or merge() may work incorrectly for some Map implementations if the code extracted to lambda expression modifies the same Map. By default, warning is not issued if this code may have side effects. If desired, use the last checkbox to issue warning always.
解釋
傳統的操作可以用Map提供的新方法替代,比如上面判斷不存在才添加,可以寫成 map.putIfAbsent("A", "AAA");
10. Diamond types are not supported at language level '5'
出現
11. StandardCharsets.UTF_8 can be used instead
詳細提示
StandardCharsets.UTF_8 can be used instead less... (Ctrl+F1)
Inspection info: Reports methods and constructors where constant charset String literal is used (like "UTF-8") which could be replaced with a predefined Charset object like StandardCharsets.UTF_8. This may work a little bit faster, because charset lookup becomes unnecessary. Also catching UnsupportedEncodingException may become unnecessary as well. In this case the catch block will be removed automatically.
解釋
字符串設置字符集,可以更改為預定義的常量,比如 java.nio.charset.StandardCharsets.UTF_8