安裝
首選項
插件Plugins
輸入checkstyle點擊search
找到checkStyle-idea
點擊install
開始安裝
安裝完畢后重啟idea 就可以找到Checkstyle
默認的CheckStyle是sun公司的 但是一些規范不太適用 所以我找了一個網上的checkstyle 最后附上checkStyle代碼。選擇自己的checkStyle目錄,填上自定義名稱
選擇添加的checkStyle 點擊 apply 應用
顏色設置
默認的checkstyle樣式 跟idea默認的差不多 不好區分 。所以我更改了一下顏色設置
找到inspections-Checkstyle
選擇Edit severities
輸入自定義名稱
選擇Background和Error Stripe mark顏色
更改之后效果圖
鼠標移動綠色區域之后提示
鼠標移動紅色區域之后提示
安裝完成
CheckStyle
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<!-- Generated by RHY @will_awoke -->
<module name="Checker">
<property name="charset" value="UTF-8"/>
<property name="severity" value="warning"/>
<!-- Checks for Size Violations. -->
<!-- 檢查文件的長度(行) default max=2000 -->
<module name="FileLength">
<property name="max" value="2500"/>
</module>
<!-- Checks that property files contain the same keys. -->
<!-- 檢查**.properties配置文件 是否有相同的key
<module name="Translation">
</module>
-->
<module name="TreeWalker">
<!-- Checks for imports -->
<!-- 必須導入類的完整路徑,即不能使用*導入所需的類 -->
<module name="AvoidStarImport"/>
<!-- 檢查是否從非法的包中導入了類 illegalPkgs: 定義非法的包名稱-->
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
<!-- 檢查是否導入了不必顯示導入的類-->
<module name="RedundantImport"/>
<!-- 檢查是否導入的包沒有使用-->
<module name="UnusedImports"/>
<!-- Checks for whitespace
<module name="EmptyForIteratorPad"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>
-->
<!-- 檢查類和接口的javadoc 默認不檢查author 和version tags
authorFormat: 檢查author標簽的格式
versionFormat: 檢查version標簽的格式
scope: 可以檢查的類的范圍,例如:public只能檢查public修飾的類,private可以檢查所有的類
excludeScope: 不能檢查的類的范圍,例如:public,public的類將不被檢查,但訪問權限小于public的類仍然會檢查,其他的權限以此類推
tokens: 該屬性適用的類型,例如:CLASS_DEF,INTERFACE_DEF -->
<module name="JavadocType">
<property name="authorFormat" value="\S"/>
<property name="scope" value="protected"/>
<property name="tokens" value="CLASS_DEF,INTERFACE_DEF"/>
</module>
<!-- 檢查方法的javadoc的注釋
scope: 可以檢查的方法的范圍,例如:public只能檢查public修飾的方法,private可以檢查所有的方法
allowMissingParamTags: 是否忽略對參數注釋的檢查
allowMissingThrowsTags: 是否忽略對throws注釋的檢查
allowMissingReturnTag: 是否忽略對return注釋的檢查 -->
<module name="JavadocMethod">
<property name="scope" value="private"/>
<property name="allowMissingParamTags" value="false"/>
<property name="allowMissingThrowsTags" value="false"/>
<property name="allowMissingReturnTag" value="false"/>
<property name="tokens" value="METHOD_DEF"/>
<property name="allowUndeclaredRTE" value="true"/>
<property name="allowThrowsTagsForSubclasses" value="true"/>
<!--允許get set 方法沒有注釋-->
<property name="allowMissingPropertyJavadoc" value="true"/>
</module>
<!-- 檢查類變量的注釋
scope: 檢查變量的范圍,例如:public只能檢查public修飾的變量,private可以檢查所有的變量 -->
<module name="JavadocVariable">
<property name="scope" value="private"/>
</module>
<!--option: 定義左大括號'{'顯示位置,eol在同一行顯示,nl在下一行顯示
maxLineLength: 大括號'{'所在行行最多容納的字符數
tokens: 該屬性適用的類型,例:CLASS_DEF,INTERFACE_DEF,METHOD_DEF,CTOR_DEF -->
<module name="LeftCurly">
<property name="option" value="eol"/>
</module>
<!-- NeedBraces 檢查是否應該使用括號的地方沒有加括號
tokens: 定義檢查的類型 -->
<module name="NeedBraces"/>
<!-- Checks the placement of right curly braces ('}') for else, try, and catch tokens. The policy to verify is specified using property option.
option: 右大括號是否單獨一行顯示
tokens: 定義檢查的類型
<module name="RightCurly">
<property name="option" value="alone"/>
</module> -->
<!-- 檢查在重寫了equals方法后是否重寫了hashCode方法 -->
<module name="EqualsHashCode"/>
<!-- Checks for illegal instantiations where a factory method is preferred.
Rationale: Depending on the project, for some classes it might be preferable to create instances through factory methods rather than calling the constructor.
A simple example is the java.lang.Boolean class. In order to save memory and CPU cycles, it is preferable to use the predefined constants TRUE and FALSE. Constructor invocations should be replaced by calls to Boolean.valueOf().
Some extremely performance sensitive projects may require the use of factory methods for other classes as well, to enforce the usage of number caches or object pools. -->
<module name="IllegalInstantiation">
<property name="classes" value="java.lang.Boolean"/>
</module>
<!-- 代碼縮進
<module name="Indentation">
</module> -->
<!-- Checks for redundant exceptions declared in throws clause such as duplicates, unchecked exceptions or subclasses of another declared exception.
檢查是否拋出了多余的異常
<module name="RedundantThrows">
<property name="logLoadErrors" value="true"/>
<property name="suppressLoadErrors" value="true"/>
</module>
-->
<!-- Checks for overly complicated boolean expressions. Currently finds code like if (b == true), b || true, !false, etc.
檢查boolean值是否冗余的地方
Rationale: Complex boolean logic makes code hard to understand and maintain. -->
<module name="SimplifyBooleanExpression"/>
<!-- Checks for overly complicated boolean return statements. For example the following code
檢查是否存在過度復雜的boolean返回值
if (valid())
return false;
else
return true;
could be written as
return !valid();
The Idea for this Check has been shamelessly stolen from the equivalent PMD rule. -->
<module name="SimplifyBooleanReturn"/>
<!-- Checks that a class which has only private constructors is declared as final.只有私有構造器的類必須聲明為final-->
<module name="FinalClass"/>
<!-- Make sure that utility classes (classes that contain only static methods or fields in their API) do not have a public constructor.
確保Utils類(只提供static方法和屬性的類)沒有public構造器。
Rationale: Instantiating utility classes does not make sense. Hence the constructors should either be private or (if you want to allow subclassing) protected. A common mistake is forgetting to hide the default constructor.
If you make the constructor protected you may want to consider the following constructor implementation technique to disallow instantiating subclasses:
public class StringUtils // not final to allow subclassing
{
protected StringUtils() {
throw new UnsupportedOperationException(); // prevents calls from subclass
}
public static int count(char c, String s) {
// ...
}
}
<module name="HideUtilityClassConstructor"/>
-->
<!-- Checks visibility of class members. Only static final members may be public; other class members must be private unless property protectedAllowed or packageAllowed is set.
檢查class成員屬性可見性。只有static final 修飾的成員是可以public的。其他的成員屬性必需是private的,除非屬性protectedAllowed或者packageAllowed設置了true.
Public members are not flagged if the name matches the public member regular expression (contains "^serialVersionUID$" by default). Note: Checkstyle 2 used to include "^f[A-Z][a-zA-Z0-9]*$" in the default pattern to allow CMP for EJB 1.1 with the default settings. With EJB 2.0 it is not longer necessary to have public access for persistent fields, hence the default has been changed.
Rationale: Enforce encapsulation. 強制封裝 -->
<module name="VisibilityModifier"/>
<!-- 每一行只能定義一個變量 -->
<module name="MultipleVariableDeclarations">
</module>
<!-- Checks the style of array type definitions. Some like Java-style: public static void main(String[] args) and some like C-style: public static void main(String args[])
檢查再定義數組時,采用java風格還是c風格,例如:int[] num是java風格,int num[]是c風格。默認是java風格-->
<module name="ArrayTypeStyle">
</module>
<!-- Checks that there are no "magic numbers", where a magic number is a numeric literal that is not defined as a constant. By default, -1, 0, 1, and 2 are not considered to be magic numbers.
<module name="MagicNumber">
</module>
-->
<!-- A check for TODO: comments. Actually it is a generic regular expression matcher on Java comments. To check for other patterns in Java comments, set property format.
檢查是否存在TODO(待處理) TODO是javaIDE自動生成的。一般代碼寫完后要去掉。
-->
<module name="TodoComment"/>
<!-- Checks that long constants are defined with an upper ell. That is ' L' and not 'l'. This is in accordance to the Java Language Specification, Section 3.10.1.
檢查是否在long類型是否定義了大寫的L.字母小寫l和數字1(一)很相似。
looks a lot like 1. -->
<module name="UpperEll"/>
<!-- Checks that switch statement has "default" clause. 檢查switch語句是否有‘default’從句
Rationale: It's usually a good idea to introduce a default case in every switch statement.
Even if the developer is sure that all currently possible cases are covered, this should be expressed in the default branch,
e.g. by using an assertion. This way the code is protected aginst later changes, e.g. introduction of new types in an enumeration type. -->
<module name="MissingSwitchDefault"/>
<!--檢查switch中case后是否加入了跳出語句,例如:return、break、throw、continue -->
<module name="FallThrough"/>
<!-- Checks the number of parameters of a method or constructor. max default 7個. -->
<module name="ParameterNumber">
<property name="max" value="5"/>
</module>
<!-- 每行字符數 -->
<module name="LineLength">
<property name="max" value="200"/>
</module>
<!-- Checks for long methods and constructors. max default 150行. max=300 設置長度300 -->
<module name="MethodLength">
<property name="max" value="300"/>
</module>
<!-- ModifierOrder 檢查修飾符的順序,默認是 public,protected,private,abstract,static,final,transient,volatile,synchronized,native -->
<module name="ModifierOrder">
</module>
<!-- 檢查是否有多余的修飾符,例如:接口中的方法不必使用public、abstract修飾 -->
<module name="RedundantModifier">
</module>
<!--- 字符串比較必須使用 equals() -->
<module name="StringLiteralEquality">
</module>
<!-- if-else嵌套語句個數 最多4層 -->
<module name="NestedIfDepth">
<property name="max" value="3"/>
</module>
<!-- try-catch 嵌套語句個數 最多2層 -->
<module name="NestedTryDepth">
<property name="max" value="2"/>
</module>
<!-- 返回個數 -->
<module name="ReturnCount">
<property name="max" value="5"/>
<property name="format" value="^$"/>
</module>
</module>
</module>