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

 

转载于:https://www.cnblogs.com/caizhiqin/p/9366036.html

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: idea inspections设置是指在IntelliJ IDEA中设置代码检查的规则和方式,以便在编写代码时能够及时发现潜在的问题并进行修复。通过设置不同的检查项和级别,可以帮助开发者提高代码质量和效率,减少错误和漏洞的出现。同时,还可以根据具体的项目需求和开发环境,自定义检查规则和配置,以满足不同的开发需求。 ### 回答2: Idea inspections是指在IntelliJ IDEA中内置的一种静态代码分析工具,它通过检查Java代码中的错误、警告、提示等信息,帮助开发人员在编码的过程中对代码进行优化和改进,从而提高代码的质量、可读性和可维护性。Idea inspections主要使用Java内置的静态代码分析器和一系列自定义规则进行代码检查,包括代码规范、代码质量、性能、安全、可扩展性等方面。 Idea inspections设置可以帮助开发人员更好地利用这个工具,并将它应用到开发项目中。它可以通过设置自定义的代码检查规则、开启或关闭特定的检查项、设置检查的范围和目标等方式来适应不同的项目环境和开发需求。以下是Idea inspections设置的一些重点: 首先,设置检查规则。Idea inspections包含各种预定义的规则集,如基本的Java代码规范、常见的BUG集合、性能优化、安全集合等。开发人员可以在全局或项目级别上选择启用或禁用这些规则,也可以根据自己的需求编写自定义规则来检查特定代码区域。 其次,设置检查范围和目标。Idea inspections可以对整个项目进行检查,也可以只针对部分文件或代码行进行检查。还可以设置启用只针对Test代码或Production代码进行检查,以确保代码的健壮性和功能性。 最后,Idea inspections设置还可以进行实时检查和扫描,以帮助开发人员在开发代码的同时检查代码错误和问题,提高工作效率。 总的来说,Idea inspections设置是IntelliJ IDEA中非常强大和实用的工具之一,它在开发过程中起到了优化代码的重要作用。开发人员可以根据自己的需要和代码质量要求,灵活地进行设置和使用,提高自己代码的质量和效率。 ### 回答3: Idea inspections是一种可以检查代码质量并提供修复建议的工具,它可以在IntelliJ IDEA这样的IDE中自动运行。Idea inspections的目的是帮助开发人员编写更高效、更可靠的代码。 Idea inspections可以帮助开发人员在编程过程中发现并修复代码中的常见问题。它可以检查代码中的语法错误、类型不匹配、无用的代码、代码重复等问题。除此之外,Idea inspections还可以检查潜在的性能问题和安全问题,并给出相应的修复建议。 Idea inspections的设置可以通过IDE的设置菜单进行配置。在这里,开发人员可以定义哪些检查应该在代码中运行,哪些应该被忽略。开发人员甚至可以自定义他们自己的检查规则。 Idea inspections可以帮助开发人员节省宝贵的工作时间,因为它可以自动检查代码并提供建议。这意味着,开发人员可以更专注于代码开发的高级方面,而不必担心代码中的低级问题。 Idea inspections不仅可以在IntelliJ IDEA中运行,还可以在其他IDE中使用。这意味着,这个工具可以帮助许多开发人员提高他们的代码质量,无论他们是使用哪个IDE。 总之,Idea inspections是一个非常有用的工具,可以帮助开发人员编写更高效、更可靠的代码。通过在IDE中自动运行,它可以提供即时的修复建议,使开发人员能够更专注于高级代码开发方面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值