YouShallnotPass ist der statische Analysator für Ihren eleganten Code.
Sehen Sie einfach, was es in einem scheinbar üblichen Code finden kann:
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 ;
}
}Die Verstöße gegen die Analyse von YouHhallnotpass :
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)
Fügen Sie das Plugin zum 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
}
} Wobei xyz eine tatsächliche Version von Gradle -Plugins ist
Rufen Sie es an:
./gradlew youshallnotpass Fügen Sie das Plugin zum pom.xml hinzu
< 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 >Rufen Sie es an:
mvn youshallnotpass:youshallnotpass Wobei xyz eine tatsächliche Version von Maven Central ist
✅ nullFree (warum null ist schlecht?) Eleganter Code darf die null -Schlüsselwörter nicht verwenden
✅ staticFree (warum static ist schlecht?) Eleganter Code darf die static Schlüsselwörter nicht verwenden
✅ Allfinal Jede Klasse, jedes Feld, jedes Argument, jede lokale Variable muss im eleganten Code final sein
InstanceOffree [in progress] Eleganter Code darf die instanceof nicht verwenden
✅ Erbschaftsfree eleganter Code darf die Klassenerbschaft nicht verwenden (wenn eine Klasse eine andere extends ), wurde nur Komposition und Typ Erbe zulässig
Enumfree [in progress] Eleganter Code darf die enum nicht verwenden
SwitchFree [in progress] Eleganter Code darf die switch -Blöcke/Ausdrücke nicht verwenden
✅ Nomultiplereturn Eleganter Code darf nur eine (oder niemanden) in einer beliebigen Methode enthalten
GetterFree [in progress] Eleganter Code darf keine Getter enthalten
✅ Setzerfreier eleganter Code darf keine Setter enthalten
✅ Allpublic Elegant Code darf nur public Methoden anwenden
NopublicMethodnotoverrides [in progress] Jede öffentliche Methode im eleganten Code muss von einer Schnittstelle überschrieben werden
Plugin -Konfigurationsoptionen:
skipComparisons ermöglicht die Verwendung null in booleschen Ausdrücken: if ( some == null ) {
...
} Kann im Code von @SuppressWarnings("nullfree") unterdrückt werden.
Kann im Code von @SuppressWarnings("staticfree") unterdrückt werden.
Plugin -Konfigurationsoptionen:
skipInterfaceMethodParams ermöglicht das Einschränkungs- oder Nicht -Schnittstellenmethode -Parameter final S, standardmäßig ist es nicht erforderlich, final für solche Orte einzustellenskipLambdaParams ermöglicht das Skip final in Lambda -Parametern, standardmäßig muss der Lambda -Parameter final seinskipCatchParams ermöglicht das Skip final in catch -Parametern, standardmäßig muss der Parameter catch final sein Kann im Code von @SuppressWarnings("allfinal") unterdrückt werden.
Kann im Code von @SuppressWarnings("allpublic") unterdrückt werden.
Kann im Code von @SuppressWarnings("setterfree") unterdrückt werden.
Kann im Code von @SuppressWarnings("nomultiplereturn") unterdrückt werden.
Kann im Code von @SuppressWarnings("inheritancefree")
Wenn Sie YouShallnotpass -Plugin ohne offline = true -Einstellungen verwenden, können Sie die Inspektionsausweise an Ihre ReadMe -Datei anhängen:
 Jede Inspektion kann mit threshold konfiguriert werden:
Im gradle
youshallnotpass {
.. .
staticfree {
threshold = 19
}
.. .
} In maven
< configuration >
< staticfree >
< threshold >19</ threshold >
</ staticfree >
</ configuration > Jede Inspektion kann durch disabled Einstellungen deaktiviert werden:
Im gradle
youshallnotpass {
.. .
staticfree {
disabled = true
}
.. .
} In maven
< configuration >
< staticfree >
< disabled >true</ disabled >
</ staticfree >
</ configuration >Alle Inspektionen sind standardmäßig aktiviert.
Es gibt globale exclude , die zum Definieren von Ausschlussmuster verwendet werden können: im gradle
youshallnotpass {
exclude = [ " glob:**/test/**/*Test.java " ]
} In maven
< configuration >
< exclude >glob:**/test/**/*Test.java</ exclude >
</ configuration >Es gibt eine Inspektion lokaler Konfigurationsoption, die eine höhere Priorität hat als die globale Konfiguration ausschließt:
Im gradle
nullfree {
exclude = [ " glob:**/*SomeBadFile.java " ]
} In maven
< nullfree >
< exclude >glob:**/*SomeBadFile.java</ exclude >
</ nullfree >MIT