IDEA Inspections详解

之前用Eclipse的时候就特别喜欢琢磨eclipse的warning级别(黄叹号),以期待coding出最模范的代码。

换成IDEA后,发现其代码检查功能更加复杂,遂仔细琢磨分析,寻找最适合自己的配置

Abstraction issues

Title

默认

建议

描述

'instance of' a concrete class

 

warning

instance of一般判断是否是一个接口或者抽象类的实例

'instance of' check for this

 

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

 

posted @ 2018-08-02 19:33  疯狂阿贾克斯  阅读(2393)  评论(0编辑  收藏  举报