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)
ルート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 youshallnotpasspom.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キーワードを使用してはいけません
staticFree ( staticが悪い理由ですか?)エレガントなコードはstaticキーワードを使用してはなりません
allfinalすべてのクラス、すべてのフィールド、すべての引数、すべてのローカル変数がエレガントコードでfinalなければなりません
InstanceOffree [in progress]エレガントなコードは、キーワードのinstanceofを使用してはなりません
comseletancefree無料のエレガントなコードは、クラスの継承を使用してはなりません(あるクラスが別のクラスextends場合)、構成とタイプの継承のみが許可されています
EnumFree [in progress]エレガントなコードはenumを使用してはなりません
SwitchFree [in progress]エレガントなコードはswitchブロック/式を使用してはなりません
✅nomultiplereturnエレガントコードには、任意の方法に1つの(または誰も)返品のみを含める必要があります
GetterFree [in progress]エレガントなコードにはゲッターが含まれていないはずです
setterFreeエレガントなコードにセッターを含めてはなりません
Allpublicエレガントコードは、 publicメソッドのみを使用する必要があります
nopublicmethodnotoverrides [in progress]エレガントなコードのすべてのパブリックメソッドは、インターフェイスから上向きにする必要があります
プラグイン構成オプション:
skipComparisons 、ブール式でnullを使用できます。 if ( some == null ) {
...
} @SuppressWarnings("nullfree")によってコードで抑制できます
@SuppressWarnings("staticfree")によってコードで抑制できます
プラグイン構成オプション:
skipInterfaceMethodParams使用すると、インターフェイスメソッドパラメーターのfinal sを制限するか、最終的なsを制限できます。デフォルトでは、そのような場所のfinal設定する必要はありませんskipLambdaParams 、ラムダパラメーターのfinalを許可します。デフォルトでは、ラムダパラメーターがfinalなる必要がありますskipCatchParams 、 catchパラメーターのfinalを許可します。デフォルトでは、 catchパラメーターがfinalである必要があります@SuppressWarnings("allfinal")によってコードで抑制できます
@SuppressWarnings("allpublic")によってコードで抑制できます
@SuppressWarnings("setterfree")でコードで抑制できます
@SuppressWarnings("nomultiplereturn")によってコードで抑制できます
@SuppressWarnings("inheritancefree")によってコードで抑制できます
offline = true設定なしでYouShallNotPassプラグインを使用する場合は、検査バッジをREADMEファイルに添付できます。
検査は、 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 >mit