Youshallnotpass是您優雅代碼的靜態分析儀。
只需看到它在看似通常的代碼中可以找到什麼:
package com . example ;
import java . util . Collection ;
import java . util . StringTokenizer ;
public class Words {
public static final String DELIM = " ,." ;
private Collection < String > words ;
public Words () {
this . words = null ;
}
public Words ( Collection < String > words ) {
this . words = words ;
}
boolean containsIn ( String text ) {
if ( words == null ) return false ;
StringTokenizer tokenizer = new StringTokenizer ( text , DELIM );
while ( tokenizer . hasMoreTokens ()) {
String nextWord = tokenizer . nextToken ();
if ( words . contains ( nextWord )) return true ;
}
return false ;
}
}違反YouShallnotPass分析的行為:
nullfree
com.example.Words(Words.java:12) > null
com.example.Words.containsIn(Words.java:20) > null
staticfree
com.example.A.main(A.java:6) > static
com.example.Words(Words.java:7) > static
allfinal
com.example.A.main(A.java:6) > String[] args
com.example.A(A.java:5) > A
com.example.Words(Words.java:9) > words
com.example.Words.containsIn(Words.java:22) > StringTokenizer tokenizer = new StringTokenizer(text, DELIM)
com.example.Words.containsIn(Words.java:24) > String nextWord = tokenizer.nextToken()
com.example.Words(Words.java:15) > Collection<String> words
com.example.Words.containsIn(Words.java:19) > String text
com.example.Words(Words.java:6) > Words
allpublic
com.example.Words.containsIn(Words.java:19)
nomultiplereturn
com.example.Words.containsIn(Words.java:19)
將插件添加到root build.gradle
plugins {
id ' dev.youshallnotpass ' version ' x.y.z '
}
// then configure it, if you need:
youshallnotpass {
offline = true // default false
nullfree {
disabled = true // default false
threshold = 3 // default 0
skipComparisons = true // default false
}
staticfree {
disabled = true // default false
threshold = 2 // default 0
}
allfinal {
disabled = true // default false
threshold = 1 // default 0
skipInterfaceMethodParams = false // default true
skipLambdaParams = true // default false
skipCatchParams = true // default false
}
allpublic {
disabled = true // default false
threshold = 4 // default 0
}
setterfree {
disabled = true // default false
threshold = 5 // default 0
}
nomultiplereturn {
disabled = true // default false
threshold = 6 // default 0
}
inheritancefree {
disabled = true // default false
threshold = 7 // default 0
}
}其中xyz是Gradle插件的實際版本
調用它:
./gradlew youshallnotpass將插件添加到pom.xml
< plugin >
< groupId >dev.youshallnotpass</ groupId >
< artifactId >youshallnotpass-maven-plugin</ artifactId >
< version >x.y.z</ version >
<!-- then configure it, if you need: -->
< configuration >
< offline >true</ offline > <!-- default false -->
< nullfree >
< disabled >true</ disabled > <!-- default false -->
< threshold >3</ threshold > <!-- default 0 -->
< skipComparisons >true</ skipComparisons > <!-- default false -->
</ nullfree >
< staticfree >
< disabled >true</ disabled > <!-- default false -->
< threshold >2</ threshold > <!-- default 0 -->
</ staticfree >
< allfinal >
< disabled >true</ disabled > <!-- default false -->
< threshold >1</ threshold > <!-- default 0 -->
< skipInterfaceMethodParams >false</ skipInterfaceMethodParams > <!-- default true -->
< skipLambdaParams >true</ skipLambdaParams > <!-- default false -->
< skipCatchParams >true</ skipCatchParams > <!-- default false -->
</ allfinal >
< allpublic >
< disabled >true</ disabled > <!-- default false -->
< threshold >4</ threshold > <!-- default 0 -->
</ allpublic >
< setterfree >
< disabled >true</ disabled > <!-- default false -->
< threshold >5</ threshold > <!-- default 0 -->
</ setterfree >
< nomultiplereturn >
< disabled >true</ disabled > <!-- default false -->
< threshold >6</ threshold > <!-- default 0 -->
</ nomultiplereturn >
< inheritancefree >
< disabled >true</ disabled > <!-- default false -->
< threshold >7</ threshold > <!-- default 0 -->
</ inheritancefree >
</ configuration >
</ plugin >調用它:
mvn youshallnotpass:youshallnotpass其中xyz是Maven Central的實際版本
✅nullfree (為什麼null不好?)優雅代碼不得使用null關鍵字
✅靜態Free (為什麼static不好?)優雅代碼不得使用static關鍵字
✅每個類,每個字段,每個參數,每個本地變量都必須在優雅代碼中final確定
InstanceOffree [in progress]優雅代碼不得使用關鍵字的instanceof
✅無繼承的優雅代碼不得使用類繼承(當一個類extends另一個時),只允許構圖和類型的繼承
枚舉[in progress]優雅代碼不得使用enum
switchfree [in progress]優雅代碼不得使用switch塊/表達式
✅Nomultiplereturn優雅代碼必須僅包含一個(或沒有一個)以任何方法返回
getterfree [in progress]優雅代碼不得包含任何getters
✅無固定的優雅代碼不得包含任何設置器
✅所有公共優雅代碼必須僅使用public方法
NopublicMetHodNotoverrides [in progress]
插件配置選項:
skipComparisons允許在布爾表達中使用null : if ( some == null ) {
...
}可以通過@SuppressWarnings("nullfree")
可以通過@SuppressWarnings("staticfree")
插件配置選項:
skipInterfaceMethodParams允許限製或不限制接口方法參數final s,默認情況下,不需要為此位置設置finalskipLambdaParams允許在lambda參數中跳過final ,默認情況下,lambda參數需要是finalskipCatchParams允許跳過catch參數的final ,默認情況下需要catch參數為final可以通過@SuppressWarnings("allfinal")
可以通過@SuppressWarnings("allpublic")
可以通過@SuppressWarnings("setterfree")
@SuppressWarnings("nomultiplereturn")
可以通過@SuppressWarnings("inheritancefree")
如果您使用youshallnotpass插件而不offline = true設置,則可以將檢查徽章附加到讀書文件:
任何檢查都可以使用threshold配置:
在gradle
youshallnotpass {
.. .
staticfree {
threshold = 19
}
.. .
}在maven
< configuration >
< staticfree >
< threshold >19</ threshold >
</ staticfree >
</ configuration >可以通過disabled設置來禁用任何檢查:
在gradle
youshallnotpass {
.. .
staticfree {
disabled = true
}
.. .
}在maven
< configuration >
< staticfree >
< disabled >true</ disabled >
</ staticfree >
</ configuration >默認情況下啟用了所有檢查。
有全局exclude設置,可用於定義排除模式:在gradle中
youshallnotpass {
exclude = [ " glob:**/test/**/*Test.java " ]
}在maven
< configuration >
< exclude >glob:**/test/**/*Test.java</ exclude >
</ configuration >有檢查本地排除配置選項,其優先級高於全局排除配置:
在gradle
nullfree {
exclude = [ " glob:**/*SomeBadFile.java " ]
}在maven
< nullfree >
< exclude >glob:**/*SomeBadFile.java</ exclude >
</ nullfree >麻省理工學院