IDEA Inspections详解
Posted caizhiqin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IDEA Inspections详解相关的知识,希望对你有一定的参考价值。
之前用Eclipse的时候就特别喜欢琢磨eclipse的warning级别,以期待coding出最模范的代码。
换成IDEA后,发现其代码检查功能更加复杂,遂仔细琢磨分析,寻找最适合自己的配置
Abstraction issues(抽象问题)
Title |
默认 |
建议 |
描述 |
Cast to a concrete class |
接口引用指向实现类 |
||
Chain of instance checks |
在一串if else中,连续判断一个对象instance of某个类,比较少见 |
||
Class references one of its subclasses |
父类引用指向子类对象 |
||
Collection declared by class,not interface |
声明集合左边是接口 |
||
Feature envy |
当一个类的方法调用另一个类的方法三次或更多次时发生 |
||
‘instance of‘ a concrete class |
warning |
instance of一般判断是否是一个接口或者抽象类的实例 |
|
‘instance of‘ check for this |
error |
使用this肯定知道是哪个Class |
|
interface method clashes with method in ‘java.lang.Object‘ |
warning |
error |
接口定义的方法名和Object的方法冲突,如clone方法 |
Local variable of a concrete class |
在类的方法里创建本类的实例对象 |
||
Magic number |
warning |
不允许任何魔法值(即未经定义的常量)直接出现在代码中 ,参考阿里规范 |
|
Method parameter of a concrete class |
方法参数不允许有实体类,比较常见 |
||
Method return of a concrete class |
方法return实体类,比较常见 |
||
‘Optional‘ used as field or parameter type |
warning |
error |
java.util.Optional一般用作防空指针,定义成成员变量或者参数比较奇怪 |
Overly strong type cast |
warning |
高强强制类型转换,有时候我们强转List即可却强转成ArrayList |
|
Private method only used from inner class |
私有方法仅在内部类使用,那么可以挪到内部类中 |
||
Public method not exposed in interface |
public方法没有定义在接口中 |
||
Public method with ‘boolean‘ parameter |
public方法有boolean类型参数 |
||
Static field of concrete class |
静态成员变量定义在实体类中 |
||
Static method only used from one other class |
静态方法只在另外某个类中使用 |
||
Type may be weakened |
声明变量可以用List的时候不要用ArrayList |
||
Type of instance field is concrete class |
成员变量是实体类,常见 |
Arquillian
Arquillian 可让你在远程或者嵌入式的容器里测试业务逻辑,同时可作为一个压缩包发布到容器中,并通过客户端来进行交互测试。
Assignment issues(赋值问题)
Title |
默认 |
建议 |
描述 |
Assignment replaceable with operator assignment |
赋值操作可以用赋值运算符代替 |
||
Assignment to for ‘loop‘ parameter |
warning |
在for循环中改变了循环的参数 |
|
Assignment to catch block parameter |
对try代码块的参数赋值 |
||
Assignment to lambda parameter |
对lambda表达式中的参数进行赋值 |
||
Assignment to method parameter |
对方法参数赋值 |
||
Assignment to static field from instance context |
在实例方法中对静态成员赋值 |
||
Assignment used as condition |
把赋值作为if、for、for或do语句的条件 |
||
Constructor assigns value to field defined in superclass |
warning |
在构造方法中对父类成员变量赋值,这样做不合理,应该调用父类构造方法 |
|
‘null‘ assignment |
赋值null,很多时候会用到 |
||
Result of assignment used |
赋值表达式结果被使用 |
||
Value of ++ or -- used |
嵌套在其他表达式中的增量或减量表达式 |
Class Metrics(类的指标)
Title |
默认 |
建议 |
描述 |
Anonymous inner class has too many methods |
匿名内部类方法过多(默认限制1) |
||
Class too deep in inheritance tree |
类的继承过于复杂(默认限制2层) |
||
Class with too many constructors |
warning |
类的构造方法过多(默认限制5个) |
|
Class with too many fields |
类的成员变量过多(默认限制10个) |
||
Class with too many methods |
类的方法过多(默认限制20个) |
||
Inner class too deeply nested |
内部类中含有内部类 |
||
Overly complex anonymous class |
匿名类过于复杂 |
||
Overly complex class |
类过于复杂 |
||
Overly coupled class |
类refer的类过多 |
Class structure(类结构)
Title |
默认 |
建议 |
描述 |
Abstract class may be interface |
No highlighting,only fix |
warning |
抽象类可以定义成接口 |
Anonymous inner class |
使用匿名内部类 |
||
Class may extend adaptor instead of implementing listener |
可以继承适配器代替实现监听器 |
||
Class name differs from file name |
warning |
类名和文件名不同 |
|
Class with only private constructors should be defined final |
只有私有构造方法的类无法被继承 |
||
Constant declared in abstract class |
常量定义在抽象类里 |
||
Constant declared in interface |
常量定义在接口中 |
||
Empty class |
空类 |
||
Field can be local |
warning |
成员变量可以声明成final的 |
|
‘final‘ class |
class声明为final的 |
||
‘final‘ method |
method声明为final的 |
||
‘final‘ method in ‘final‘ class |
在final class中定义final method |
||
inner class of interface |
接口中定义内部类 |
||
interface may be annotated as @FunctionalInterface |
@FunctionalInterface:这个注解表示一个函数式接口元素。函数式接口是一种只有一个抽象方法(非默认)的接口。编译器会检查被注解元素,如果不符,就会产生错误。 |
||
Limited-scope inner class |
Reports any limited-scope inner classes.Some code standards discourage the use of limited-scope inner classes, and they are unusual enough as to possibly be confusing. |
||
Marker interface |
空接口做标记用,在hibernate validator中常见 |
||
Method returns per-class constant |
方法只返回常量 |
||
Multiple top level classes in single final |
一个java文件中有多个顶级类 |
||
No-op method in abstract class |
warning |
抽象类中的空方法没有被声明成abstract,比较少见 |
|
Non-‘final‘ field in enum |
枚举的field没有加上final描述 |
||
Non-‘static‘ initializer |
Reports any non-static initializers in classes.Some coding standards prohibit instance initializers, preferring initialization to be done in constructors or field initializers.Non-static initializers may also be inadvertently created by deleting the static keyword, resulting in obscure bugs.This inspection does not warn on instance initializers in anonymous classes. |
||
Parameter can be local |
warning |
参数可以声明成局部变量 |
|
‘private‘ method declared final |
warning |
error |
私有方法被声明成final的了,冗余 |
‘protected‘ member in final class |
warning |
定义成final的类无法被继承,protected的使用不正确 |
|
‘public‘ constructor |
public构造方法,正常 |
||
‘public‘ constructor in non public class |
非public类中有public构造方法 |
||
Singleton |
使用了单例 |
||
‘static‘ method declared final |
warning |
error |
静态方法被定义成final,无意义 |
‘static‘,non ‘final‘ field |
静态变量未定义成final |
||
Utility class |
所有字段和方法都是static的类 |
||
Utility class can be ‘enum‘ |
Utility class可以改造成枚举 |
||
Utility class is not final |
Utility class未被定义成final的 |
||
Utility class with public constructor |
Utility class有public构造方法 |
||
Utility class without private constructor |
Utility class没有private构造方法 |
Cloning issues
clone方法相关的问题,罕见
Code maturity(成熟的代码)
Title |
默认 |
建议 |
描述 |
Call to printStackTrace() |
error |
成熟的代码应该使用log |
|
Call to Thread.dumpStack |
warning |
静态dumpstack()方法提供一个new exception ("stack trace").printstacktrace ()的封装,打印一个追踪当前线程的堆栈,调试用。 |
|
Deprecated API usage |
warning |
error |
使用了过时的API |
Deprecated member is still used |
warning |
error |
使用了过时的member |
Inspection suppression annotation |
Reports any inspection suppression comments or annotations. |
||
Throwable printed to system.out |
warning |
error |
用system.out来print异常 |
Usage of API marked as removal |
error |
使用了被@Deprecated(forRemoval=true) 标记的方法 |
|
Use of absolute collection type |
error |
使用了java.util.Vector or java.util.Hashtable这些不推荐使用的类 |
|
Use of absoulte date-time API |
warning |
使用了java.util.Date, java.util.Calendar, java.util.GregorianCalendar, java.util.TimeZone, and java.util.SimpleTimeZone.jdk1.8推荐使用LocalDate等工具 |
|
Use of System.out or System.err |
error |
使用了System.out or System.err,用log替代 |
Code style issues(代码风格问题)
Title |
默认 |
建议 |
描述 |
Array creation without new expression |
No high lighting,only fix |
int[] a = {42}推荐声明int[] a = new int[]{42}. |
|
assert message is not a string |
断言非string,影响不大 |
||
Blocker marker comment |
warning |
注释位置不合理 while (i < 10) { |
|
C-style array declaration |
No high lighting,only fix |
error |
C语言风格的数组声明 public String process(String value[])[] { |
Call to String.concat() can be replaced with ‘+‘ |
可以用+替换concat方法 |
||
Chained equality comparisons |
error |
类似(i.e. a==b==c).难看 |
|
Chained method calls |
链式编程,常见 |
||
Class explicitly extends java.lang.Object |
warning |
error |
直接声明一个类继承了Object,无意义 |
Code block contains single statement |
No high lighting,only fix |
none |
代码块只有一行,常见 |
Conditional can be replaced with Optional |
No high lighting,only fix |
return str == null ? "" : str.trim(); |
|
Confusing octal escape sequence |
字符串中包含8进制字符序列+数字,可能会混淆 |
||
Constant expression can be evaluated |
No high lighting,only fix |
none |
常量不用计算式直接用算好的值,不推荐 |
Constant on left side of comparision |
常数放在比较表达式的左边,左右看个人喜好,推荐左边 |
||
Constant on right side of comparision |
No high lighting,only fix |
常数放在比较表达式的右边,左右看个人喜好,推荐左边 |
|
Control flow statement without braces |
No high lighting,only fix |
warning |
条件或者循环语句括号没打好 |
Diamond can be replaced with explicit type arguments |
No high lighting,only fix |
none |
Example of use the diamond: List <String> list = new ArrayList<>() |
equals called on Enum value |
对于枚举,使用equals的情况可以直接使用== |
||
(expression).equals("literal")rather than("literal").equals(expression) |
error |
减少空指针的好习惯 |
|
Field assignment can be moved to initializer |
No high lighting,only fix |
This intention replaces initialization of field via assignment with field initializer. |
|
Field may be final |
成员变量可以声明成final |
||
if statement may be replaced with && or || expression |
No high lighting,only fix |
none |
if statements of the form if (condition) return true else return foo or if (condition) return false else return foo. These expressions may be safely simplified to return condition || foo or return !condition && foo, respectively. |
Implicit call to super() |
隐式调用super |
||
indexOf expression is replacable with contains |
error |
Reports any List.indexOf() expressions which can be replaced with the method List.contains(). |
|
Instance field access not qualified with this |
使用field有时候没加this |
||
Instantce method call not qualified with this |
使用method有时候没加this |
||
Lambda body can be code block |
No high lighting,only fix |
none |
This inspection reports lambdas which body is an expression, e.g. n -> n + 1 |
Lambda can be replaced with anonymous class |
No high lighting,only fix |
none |
lambda可以换成你匿名类写法 |
Lambda parameter type can be specified |
No high lighting,only fix |
none |
This inspection reports lambda parameters which don‘t have their type specified, e.g. a -> a + 1 |
Local variable or parameter can be final |
局部变量或者参数可以加上final修饰 |
||
Method reference can be replaced with lambda |
No high lighting,only fix |
This inspection reports method references, like MyClass::myMethod and myObject::myMethod.The quick fix for the inspection replaces the method reference with an equivalent lambda expression that invokes the method.For example, the method reference System.out::println is replaced with s -> System.out.println(s) |
|
Missorted modifiers |
error |
修饰词顺序不符合规范 |
|
Multi-catch can be splict into seperate catch blocks |
No high lighting,only fix |
none |
多异常multi catch可拆开 |
Multiple variables in one declaration |
warning |
一行代码声明了多个变量 |
|
nested method call |
Reports method calls used as parameters of another method call. |
||
Null value for Optional type |
warning |
error |
Optional本身常用来防空指针,不应为null |
Optional contains array or collection |
Optional get到的数据是数组或者集合,ok |
||
Raw type can be generic |
No high lighting,only fix |
warning |
可以使用泛型的地方未使用 |
Redundant new expression in constant array creation |
int[] a = new int[]{42} which can be replaced with an array initializer, e.g. int[] a = {42} 跟Array creation without new expression正好相反,不推荐 |
||
Redundant field initialization |
Reports fields explicitly initialized to the same values that the JVM would initialize them to by default. |
||
Redundant interface declaration |
Reports any cases of classes declaring that they implement or extend an interface, when that interface is already declared as implemented by a superclass or extended by another interface of that class.Such declarations are unnecessary, and may be safely removed.与继承关系有关多余的接口声明 |
||
Redundant no-arg constructor |
warning |
多余的无参构造方法 |
|
Replace Optional.isPresent() checks with functional-style expressions |
warning |
none |
Optional.isPresent() 可以改成函数式风格 |
Return of this |
返回this对象,常见 |
||
Return seperated from computation of result |
No high lighting,only fix |
none |
感觉不怎么实用 For example, the code below could be simplified: |
Simplifiable annotation |
Reports annotations which can be simplified to their ‘single element‘ or ‘marker‘ shorthand form.Annotations that contain whitespace between the @-sign and the name of the annotation are also reported. |
||
Single-element annotation |
No high lighting,only fix |
none |
这个风格感觉不怎么好,简洁好点 |
size==0 replacable with isEmpty |
error |
很实用,判断list非空isEmpty一目了然 |
|
Stream API call chain can be replaced with loop |
No high lighting,only fix |
none |
可用循环代替使用stream |
Subsequent steps can be fused into Stream API chain |
warning |
链式编程优化Detects when some transformations are performed on Stream API result which could be incorporated into the Stream API call chain directly. E.g.: |
|
Try statement with multiple resources can be split |
No high lighting,only fix |
none |
try多个操作拆分,不推荐 The quick fix suggests to split such statements into nested try-with-resources statements. Example of try statement with multiple resources: |
Type parameter explicitly extends java.lang.Object |
warning |
error |
参数显式继承Object |
Unclear expression |
表达式括号没打好之类 Reports binary, conditional or instanceof expressions consisting of multiple terms with different operators without parentheses. Such expressions can be unclear because not every developer is intimately familiar with all the precedence rules of the different operators. This inspection has a quickfix which adds clarifying parentheses. |
||
Unnessarily qualified inner class access |
内部类有些场景不用加上Outter.Inner使用 |
||
Unnessarily qualified static access |
本类中访问静态元素不需ClassName. |
||
Unnessarily qualified staticly imported element |
静态导入后,访问对应的静态元素不需ClassName |
||
Unnessarily final on local variable or parameter |
与上面的Local variable or parameter can be final正好相反 |
||
Unnessarily null check before equals call |
error |
多余的空指针校验 |
|
Unnessarily super qualifier |
多余的super. |
||
Unnessarily this qualifier |
多余的this. |
||
Unnessarily String.valueOf() |
warning |
如StringBuffer.append(String.valueOf(1)); |
|
Unnessarily call to super() |
默认构造方法隐式调用 |
||
Unnessarily call to toString() |
如syso(StringBuffer.toString()) |
||
Unnessarily code block |
No high lighting,only fix |
Reports code blocks which are unnecessary to the semantics of the program, and can be replaced by their contents. Code blocks which are the bodies of if, do, while or for statements will not be reported by this inspection. |
|
Unnessarily enum modifier |
warning |
枚举多余的修饰词 |
|
Unnessarily full qualified name |
No high lighting,only fix |
可以不使用类全名的场景 |
|
Unnessarily interface modifier |
warning |
接口声明中多余的修饰词 |
|
Unnessarily parentheses |
No high lighting,only fix |
没必要的括号 |
|
Unnessarily quelifier for this or super |
Unnessarily super qualifier+Unnessarily this qualifier |
||
Unnessarily semicolon |
warning |
多余的分号 |
|
Unqualified inner class access |
Unnessarily qualified inner class access的反面 |
||
Unqualified static access |
Unnessarily qualified static access 的反面 |
||
Variables of different types in one declaration |
warning |
一行声明多个不同类型的变量,String s = "", array[]; |
Complier issues
Title |
默认 |
建议 |
描述 |
Javac quirks |
warning |
javac注释不合规问题 |
|
Uncheckd warning |
warning |
Signals places where an unchecked warning is issued by the compiler, for example: void f(HashMap map) { map.put("key", "value"); } |
Concurrency annotation issues
@GuardedBy安卓相关
Control flow issues(控制流问题)
Title |
默认 |
建议 |
描述 |
Assertation can be replaced with if statement |
No high lighting,only fix |
none |
This inspection looks for assert statements. For example, assert param != null; Quick fix replaces it with if statement throwing an java.lang.AssertionError. Example: if (param == null) throw new AssertionError(); |
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; |
|
break statement |
break语句,正常 |
||
break statement with label |
带标签的break语句 |
||
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)) |
Conditional expression(?:) |
No high lighting,only fix |
none |
使用三元运算符 |
Conditional expression with identical branches |
Reports conditional expressions with identical then and else branches. Such expressions are almost certainly a mistake. |
||
Conditional expression with negated condition |
warning |
Reports conditional expressions of the form true?result1:result2 or false?result1:result2. These expressions sometimes occur as the result of automatic refactorings, and may obviously be simplified. |
|
continue statement |
使用continue |
||
continue statement with label |
带标签的continue语句 |
||
default not last case in switch statement |
error |
在switch中,default不在最后 |
|
double negation |
warning |
error |
双重否定 Reports double negation. For example: if (!!functionCall()) |
duplicate condition in if statement |
warning |
if中出现了重复的条件 |
|
duplicate condition on && or || |
warning |
条件重复 |
|
enum switch statement that misses case |
No high lighting,only fix |
switch enum的时候漏了case |
|
fallthrough in switch statement |
warning |
swich中未使用break |
|
for loop may be replaced with while loop |
No high lighting,only fix |
none |
可用while循环替代for循环 |
for loop with missing components |
Reports for loops that lack initialization, condition, or update clauses. Some coding styles prohibit such loops. |
||
idempotent loop body |
warning |
error |
幂等循环体,不会被结束 int suffix = 1; String name = baseName; while(names.contains(name)) { name = baseName + suffix; // error: suffix is not updated making loop body idempotent } |
if statement could be replaced with conditional expression |
warning |
if语句可以换三元运算符 if (foo == null) { bar = null; } else { bar = foo.get(); } may be expressed as: bar = foo == null ? null : foo.get(); |
|
if statement with common parts |
if体中有重复部分 Before: if (x > 12) { doSomethingBefore(); doSomethingDifferent1(); doSomethingAfter(); } else { doSomething(); doSomethingDifferent2(); doSomethingAfter(); } After: doSomethingBefore(); if (x > 12) { doSomethingDifferent1(); } else { doSomethingDifferent2(); } doSomethingAfter(); |
||
if statement with negated condition |
warning |
if的条件是否定,可以调换if else顺序 |
|
if statement with too many branches |
if分支过多,默认3个 |
||
infinite loop statement |
warning |
无限循环语句 |
|
labeled statenebt |
带标签语句 |
||
local variable used and declared in different switch branches |
局部变量的声明和使用在不同switch分支 |
||
loop statement that does not loop |
warning |
error |
循环语句不会启动 |
loop variable not updated inside loop |
warning |
error |
循环标记变量在循环中不改变 |
loop with implicit termination condition |
Reports any while, do-while and for loops which have the constant true as their only condition, but which still can be terminated by a containing if statement which can break out of the loop. This if statement must be the first or only statement in a while or for loops and the last or only statement in a do-while loop. Such a loop would be clearer if the if statement was removed and its condition was made an explicit loop condition. |
||
move condition to loop |
warning |
循环优化 while(true) { if(i == 23) break; i++; } Will be replaced with: while(i != 23) { i++; } |
|
negated conditional expression |
Reports conditional expressions which are negated with a prefix expression. For example: !(i == 1 ? a : b) |
||
negated equality expression |
warning |
!(i == 1) |
|
nested switch statement |
switch嵌套switch |
||
nested conditional expression |
三元运算符嵌套 |
||
overly complex boolean expression |
boolean表达式过度复杂(默认三个以上条件) |
||
pointless indexOf comparison |
warning |
indexOf>-1则无意义 |
|
pointless boolean expression |
warning |
无意义的boolean表达式 |
|
redundant else |
No high lighting,only fix |
warning |
冗余的else |
redundant if statement |
warning |
error |
多余的if For example: if (foo()) { return true; } else { return false; } can be simplified to return foo(); |
redundant conditional expression |
warning |
error |
多余的三元运算符 Example Replacement condition ? true : false condition condition ? false : true !condition value == null ? null : value value result != 0 ? result : 0 result a == b ? a : b b
|
simplifiable boolean expression |
warning |
error |
boolean表达式可以简化 Reports boolean expressions of the form !(foo ^ bar) and (foo && bar) || !foo. Such expressions may be safely simplified to foo == bar and !foo || bar respectively. |
simplifiable conditional expression |
warning |
error |
可简化的三元运算符 Reports conditional expressions of the following form condition ? true : foo condition ? false : foo condition ? foo : !foo condition ? !foo : foo These expressions may respectively be safely simplified to condition || foo !condition && foo condition == foo condition != foo |
statement can be replaced with assert or Object.requireNotNull |
No high lighting,only fix |
none |
Reports if statements (with no else branch) throwing java.lang.Throwable. For example: if (param == 2) throw new Exception(); or guava‘s: Preconditions.checkNotNull(param, message) Quick fix replaces it with an assert statement. Example: assert param != 2; |
switch statement |
switch语句 |
||
switch statement with too few branches |
switch case太少(默认2个) |
||
switch statement with too low of a branch density |
分支密度过低 Reports switch statements with too low a ratio of switch labels to executable statements. Such switch statements may be confusing, and should probably be refactored. |
||
switch statement with too many branches |
switch case太多(默认10个) |
||
switch statement without default branch |
warning |
switch缺少default |
|
Unnessary null check before instanceof expression or call |
warning |
error |
instanceof无需校验null Here is an example of a violation: if (x != null && x instanceof String) { ... } The quickfix changes this code to: if (x instanceof String) { ... } |
To be Finished
以上是关于IDEA Inspections详解的主要内容,如果未能解决你的问题,请参考以下文章
idea pom引入依赖 Dependency ‘xxx‘ not found less... (Ctrl+F1) Inspect
IntelliJ IDEA 好用插件之analyze inspect code
IntelliJ IDEA 好用插件之analyze inspect code