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 >麻省理工学院