IDEA Inspections詳解

以前用Eclipse的時候就特別喜歡琢磨eclipse的warning級別(黃歎號),以期待coding出最模範的代碼。

換成IDEA後,發現其代碼檢查功能更加複雜,遂仔細琢磨分析,尋找最適合本身的配置java

Abstraction issues

Titleexpress

默認數組

建議less

描述eclipse

'instance of' a concrete classide

 

warningoop

instance of通常判斷是不是一個接口或者抽象類的實例優化

'instance of' check for thisthis

 

warning 線程

使用this確定知道是哪一個Class

Magic number

 

warning

不容許任何魔法值(即未經定義的常量)直接出如今代碼中 ,參考阿里規範

Overly strong type cast

 

warning

高強強制類型轉換,有時候咱們強轉List便可卻強轉成ArrayList

Assignment issues 

Title

默認

建議

描述

Assignment to for 'loop' parameter

 

warning

在for循環中改變了循環的參數

Constructor assigns value to field defined in superclass

 

warning

在構造方法中對父類成員變量賦值,這樣作不合理,應該調用父類構造方法

Class Metrics

Title

默認

建議

描述

Class with too many constructors

 

warning

類的構造方法過多(默認限制5個)

Class structure

Title

默認

建議

描述

Class name differs from file name

 

warning

類名和文件名不一樣

No-op method in abstract class

 

warning

抽象類中的空方法沒有被聲明成abstract,比較少見

'protected' member in final class

 

warning

定義成final的類沒法被繼承,protected的使用不正確

Code maturity

Title

默認

建議

描述

Call to printStackTrace()

 

warning

成熟的代碼應該使用log

Call to Thread.dumpStack

 

warning

靜態dumpstack()方法提供一個new exception ("stack trace").printstacktrace ()的封裝,打印一個追蹤當前線程的堆棧,調試用。

Use of absolute collection type

 

warning

使用了java.util.Vector or java.util.Hashtable這些不推薦使用的類

Use of System.out or System.err

 

warning

使用了System.out or System.err,用log替代

Code style issues

Title

默認

建議

描述

Blocker marker comment

 

warning

註釋位置不合理

while (i < 10) {
i++;
} // end while參考阿里規範,寫在while上方

C-style array declaration

No high lighting,only fix 

warning

C語言風格的數組聲明

public String process(String value[])[] {
return value;
}

Control flow statement without braces

No high lighting,only fix 

warning

條件或者循環語句括號沒打好

(expression).equals("literal")rather than("literal").equals(expression)

 

warning

減小空指針的好習慣

indexOf expression is replacable with contains

 

warning

Reports any List.indexOf() expressions which can be replaced with the method List.contains(). 

Missorted modifiers

 

warning

修飾詞順序不符合規範 

Multiple variables in one declaration

 

warning

一行代碼聲明瞭多個變量

Redundant no-arg constructor

 

warning

多餘的無參構造方法

size==0 replacable with isEmpty

 

warning

很實用,判斷list非空isEmpty一目瞭然

Unnessarily null check before equals call

 

warning

多餘的空指針校驗

Variables of different types in one declaration

 

warning

一行聲明多個不一樣類型的變量,String s = "", array[];

Compiller issues

Title

默認

建議

描述

Unchecked warning

warning

不少check多餘,可關閉此warning

Control flow issues

Title

默認

建議

描述

Boolean expression could be replaced with conditional expression

 

warning

Boolean類型表達式優化

Reports any boolean expressions which can be expressed more compactly, and arguably more clearly, as a conditional expression. Take for example the following expression:

a && b || !a && c;

which may be expressed as:

a ? b : c;

Conditional can be pushed inside branch expression

No high lighting,only fix    

warning

條件表達式優化

Reports conditional expressions with then and else branches so similar that the conditional expression can be pushed inside, thereby shortening the code.

For example the following conditional expression:

condition ? message("value: " + 1) : message("value: " + 2)

Can be pushed inside and transformed into:

message("value: " + (condition ? 1 : 2))

default not last case in switch statement

 

warning

在switch中,default不在最後

duplicate condition in if statement

 

warning

if中出現了重複的條件

duplicate condition on && or ||

 

warning

條件重複

fallthrough in switch statement

 

warning

swich中未使用break

if statement could be replaced with conditional expression

 

warning

三元運算符簡寫if

if statement with negated condition

 

warning

if的條件是否認,能夠調換if else順序

negated equality expression

 

warning

!(i == 1)

pointless indexOf comparison

 

warning

indexOf>-1則無心義

redundant if statement

warning

多餘的if

For example:

if (foo()) {

return true;

} else {

return false;

}

can be simplified to

return foo();

有時候爲了邏輯清晰,會有這樣寫的必要

switch statement without default branch

 

warning

switch缺乏default

Declaration redundancy

Title

默認

建議

描述

Declaration access can be weaker

warning

能夠定義更低的訪問權限public->protected->default->private,

但長遠考慮有時候會有這方面須要

Declaration can have final modifier

warning

聲明能夠加上final

Empty method

warning

空方法

Method can be void

warning

方法能夠聲明成void的,

雖然返回值沒用起來,可是將來極可能會被使用

Method returns the same value

warning

方法返回值老是相同,很常見

remove redundant lambda parameter types

No high lighting,only fix    

warning

優化lambda參數自動推測

Example:

Map<String, Integer> map = ...

map.forEach((String s, Integer i) -> log.info(s + "=" + i));

Error handling

Title

默認

建議

描述

instanceof on catch parameter

 

warning

使用instanceof來區分異常不如使用多個catch塊

Nested try statement

 

warning

嵌套try

Java language level migration aids

Title

默認

建議

描述

try finally replacable with try with resources

warning

Before Java 7, the usual pattern was something like this:

Connection con = null; PreparedStatement prep = null; try{ con = getConnection(); prep = prep.prepareStatement("Update ..."); ... con.commit(); } catch (SQLException e){ con.rollback(); throw e; } finally{ if (prep != null) prep.close(); if (con != null) con.close(); }

With Java 7 you can go for:

try(Connection con = getConnection(); PreparedStatement prep = con.prepareConnection("Update ..."){ ... con.commit(); }

lambda can be replaced with method reference

warning

不一樣風格的lambda寫法

Numberic issues

Title

默認

建議

描述

divdide by zero

warning

error

除零

equals called on java.math.BigDecimal

 

warning

使用compareTo

Performance

Title

默認

建議

描述

Single charactor string argument in String.indexOf call

 

warning

單字符串String無需indexOf直接equals便可

String.equals("")

 

warning

直接.length==0,可能null則使用StringUtils.isEmpty

Probable bugs

Title

默認

建議

描述

Array comparison using == instead of Array.equals

 

warning

正確比較數組每一個元素相等的方法Arrays.equals()

Call to default toString

 

warning

未覆寫ToString時使用只會打印地址

Collection added to itself

warning

Reports cases where the argument of a method call on a java.util.Collection or java.util.Map is the collection or map itself. This includes adding a collection to itself, which can lead to a java.lang.StackOverflowError when, for example, calling hashCode() on the self-containing collection.

equals and hashCode are not pared

 

warning

兩個對象若是不相等,hashCode不強制要求不同,可是若是能保證不同,對哈希的效率會比較有幫助最重要的是第二點,相等的對象必須有相同的hashCode,因爲默認的hashCode方法針對每個對象返回一個固定的隨機值(有的實現是根據對象地址返回值,至關於每個對象對應一個固定的隨機值),因此當咱們使用equals方法的同時,必須override(重寫)hashCode方法,以知足這一點。

Object comparison using == instead of equals

No high lighting,only fix    

warning

比較對象相等通常不是要比較地址

Verbose or redundant code constructs

Title

默認

建議

描述

unnecessary default for enum switch statement

 

warning

enum case就這麼多無需default

相關文章
相關標籤/搜索