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 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
✅ StaticFree (ทำไม static ไม่ดี?) รหัสที่สง่างามต้องไม่ใช้คำหลัก static
✅ Allfinal ทุกชั้นเรียนทุกสาขาอาร์กิวเมนต์ทุกตัวแปรท้องถิ่นทุกตัวจะต้องเป็น final ในรหัสที่สง่างาม
อินสแตนซ์ฟรี [in progress] รหัสที่สง่างามต้องไม่ใช้คำหลักของ instanceof คำหลัก
✅การสืบทอดรหัสที่สง่า งาม จะต้องไม่ใช้การสืบทอดคลาส (เมื่อคลาสหนึ่ง extends อีกชั้นหนึ่ง) จะอนุญาตให้ใช้องค์ประกอบและประเภทมรดกเท่านั้น
enumfree [in progress] รหัสที่สง่างามจะต้องไม่ใช้ enum s
SwitchFree [in progress] รหัสที่สง่างามต้องไม่ใช้ switch /นิพจน์สวิตช์
✅ nomultiplereturn รหัสที่สง่างามจะต้องมีเพียงหนึ่งเดียว (หรือไม่มีใคร) กลับมาในวิธีใด ๆ
Getterfree [in progress] รหัสที่สง่า
✅ setterfree รหัสสง่างามต้องไม่มีตัวตั้งค่าใด ๆ
✅ allpublic elegant code ต้องใช้เฉพาะวิธี public
nopublicmethodnotoverrides [in progress] ทุกวิธีสาธารณะในรหัสที่สง่างามจะต้องถูกแทนที่จากอินเทอร์เฟซ
ตัวเลือกการกำหนดค่าปลั๊กอิน:
skipComparisons อนุญาตให้ใช้ null ในการแสดงออกบูลีน: if ( some == null ) {
...
} สามารถระงับได้ในรหัสโดย @SuppressWarnings("nullfree")
สามารถระงับได้ในรหัสโดย @SuppressWarnings("staticfree")
ตัวเลือกการกำหนดค่าปลั๊กอิน:
skipInterfaceMethodParams อนุญาตให้ จำกัด หรือไม่วิธีการอินเทอร์เฟซพารามิเตอร์ final s โดยค่าเริ่มต้นไม่จำเป็นต้องตั้ง final สำหรับสถานที่ดังกล่าวskipLambdaParams อนุญาตให้ข้ามรอบ final ในพารามิเตอร์แลมบ์ดาโดยพารามิเตอร์ Lambda เริ่มต้นจะต้องเป็น finalskipCatchParams อนุญาตให้ข้าม final ในพารามิเตอร์ catch โดยพารามิเตอร์ catch ต้นจะต้องเป็น final สามารถระงับได้ในรหัสโดย @SuppressWarnings("allfinal")
สามารถระงับได้ในรหัสโดย @SuppressWarnings("allpublic")
สามารถระงับได้ในรหัสโดย @SuppressWarnings("setterfree")
สามารถระงับได้ในรหัสโดย @SuppressWarnings("nomultiplereturn")
สามารถระงับได้ในรหัสโดย @SuppressWarnings("inheritancefree")
หากคุณใช้ปลั๊กอิน YouShallNotPass โดยไม่ต้อง offline = true คุณสามารถแนบป้ายตรวจสอบเข้ากับไฟล์ 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 >มีการตรวจสอบตัวเลือกการกำหนดค่าแบบยกเว้นในพื้นที่ซึ่งมีลำดับความสำคัญสูงกว่าการกำหนดค่า Global Exclude:
ใน gradle
nullfree {
exclude = [ " glob:**/*SomeBadFile.java " ]
} ใน maven
< nullfree >
< exclude >glob:**/*SomeBadFile.java</ exclude >
</ nullfree >มิกซ์