
易率支持的錯誤是野餐的野餐範圍擴展,可容納Google的錯誤。它旨在提高代碼質量,集中於維護性,一致性和避免常見陷阱。
易行的是Java的靜態分析工具,它在編譯時會捕獲常見的編程錯誤。
要了解有關易行的錯誤(支持)的更多信息,您如何開始在實踐中使用錯誤的錯誤以及我們如何在野餐時使用它,請觀看會議對話在實踐中易於使用錯誤的錯誤自動化錯誤。還要考慮查看博客文章野餐愛錯誤容易出現:產生高質量和一致的Java代碼。
入門•開發易於支持的錯誤•如何工作•貢獻
該庫是在容易發生的基礎上構建的。要使用它,請閱讀下面的Maven或Gradle的安裝指南。該庫要求您的構建使用JDK 17或更高版本執行,但是支持構建的構建針對較舊版本的Java的構建。
首先,遵循Prone Prone的安裝指南。
接下來,編輯您的pom.xml文件,以在maven-compiler-plugin的annotationProcessorPaths中添加一個或多個錯誤的支持模塊:
< build >
< pluginManagement >
< plugins >
< plugin >
< groupId >org.apache.maven.plugins</ groupId >
< artifactId >maven-compiler-plugin</ artifactId >
<!-- Prefer using the latest release. -->
< version >3.12.0</ version >
< configuration >
< annotationProcessorPaths >
<!-- Error Prone itself. -->
< path >
< groupId >com.google.errorprone</ groupId >
< artifactId >error_prone_core</ artifactId >
< version >${error-prone.version}</ version >
</ path >
<!-- Error Prone Support's additional bug checkers. -->
< path >
< groupId >tech.picnic.error-prone-support</ groupId >
< artifactId >error-prone-contrib</ artifactId >
< version >${error-prone-support.version}</ version >
</ path >
<!-- Error Prone Support's Refaster rules. -->
< path >
< groupId >tech.picnic.error-prone-support</ groupId >
< artifactId >refaster-runner</ artifactId >
< version >${error-prone-support.version}</ version >
</ path >
</ annotationProcessorPaths >
< compilerArgs >
< arg >
-Xplugin:ErrorProne
<!-- Add other Error Prone flags here. See
https://errorprone.info/docs/flags. -->
</ arg >
< arg >-XDcompilePolicy=simple</ arg >
</ compilerArgs >
<!-- Enable this if you'd like to fail your build upon warnings. -->
<!-- <failOnWarning>true</failOnWarning> -->
</ configuration >
</ plugin >
</ plugins >
</ pluginManagement >
</ build >首先,遵循gradle-errorprone-plugin的安裝指南。
接下來,編輯您的build.gradle文件以添加一個或多個錯誤的易載支持模塊:
dependencies {
// Error Prone itself.
errorprone( " com.google.errorprone:error_prone_core: ${ errorProneVersion } " )
// Error Prone Support's additional bug checkers.
errorprone( " tech.picnic.error-prone-support:error-prone-contrib: ${ errorProneSupportVersion } " )
// Error Prone Support's Refaster rules.
errorprone( " tech.picnic.error-prone-support:refaster-runner: ${ errorProneSupportVersion } " )
}
tasks . withType( JavaCompile ) . configureEach {
options . errorprone . disableWarningsInGeneratedCode = true
// Add other Error Prone flags here. See:
// - https://github.com/tbroyer/gradle-errorprone-plugin#configuration
// - https://errorprone.info/docs/flags
}考慮以下示例代碼:
import com . google . common . collect . ImmutableSet ;
import java . math . BigDecimal ;
public class Example {
static BigDecimal getNumber () {
return BigDecimal . valueOf ( 0 );
}
public ImmutableSet < Integer > getSet () {
ImmutableSet < Integer > set = ImmutableSet . of ( 1 );
return ImmutableSet . copyOf ( set );
}
}如果安裝成功,則使用Maven構建上述代碼應發出兩個編譯器警告:
$ mvn clean install
...
[INFO] Example.java:[9,34] [Refaster Rule] BigDecimalRules.BigDecimalZero: Refactoring opportunity
(see https://error-prone.picnic.tech/refasterrules/BigDecimalRules#BigDecimalZero)
Did you mean ' return BigDecimal.ZERO; ' ?
...
[WARNING] Example.java:[13,35] [IdentityConversion] This method invocation appears redundant ; remove it or suppress this warning and add a comment explaining its purpose
(see https://error-prone.picnic.tech/bugpatterns/IdentityConversion)
Did you mean ' return set; ' or ' @SuppressWarnings("IdentityConversion") public ImmutableSet<Integer> getSet() { ' ?
...這裡有兩件事:
BugChecker 。BigDecimal.valueOf(0)和new BigDecimal(0)為BigDecimal.ZERO 。請務必查看所有錯誤檢查和重新制定規則。
這是一個Maven項目,因此運行mvn clean install可以執行完整的清潔構建,並將庫安裝到本地Maven存儲庫中。
一旦進行了更改,構建可能會因靜態代碼分析發出警告或錯誤而失敗。下面列出的標誌和命令允許您抑製或(在大量的情況下)自動修復此類情況。確保仔細檢查可用選項,因為這可以節省大量的開發時間!
相關的Maven構建參數:
-Dverification.warn盡可能由各種插件和Java編譯器發出警告和錯誤。-Dverification.skip禁用各種非必需的插件,並以最小的檢查(即無伸縮,易於錯誤檢查等)編譯代碼。-Dversion.error-prone=some-version使用易於使用的指定版本運行構建。這是有用的,例如測試易於使用的錯誤快照時。-Perror-prone-fork使用野餐的錯誤叉,在Jitpack上託管。該叉子通常在最新的錯誤發行版本中包含一些更改。-Pself-check運行該項目定義的檢查對其進行的檢查。在發布Google/易於錯誤的3301之前,該標誌當前必須與-Perror-prone-fork結合使用。其他高度相關的命令:
mvn fmt:format使用google-java-format格式化代碼。./run-full-build.sh兩次構建項目,第二次通過驗證了與野餐的錯誤叉的兼容性,並符合該項目中定義的任何規則。 (考慮到PR檢查也執行此驗證,請考慮在打開拉動請求之前運行此操作。)./apply-error-prone-suggestions.sh SH在此項目中應用錯誤和錯誤支持代碼建議。在運行此命令之前,請確保已安裝了該項目( mvn clean install ),並確保當前的工作目錄不包含未分段或未交易的更改。./run-branch-mutation-tests.sh使用pitest對相對於上游默認分支進行修改的代碼運行突變測試。可以通過打開相應的target/pit-reports/index.html文件來審查結果。可以使用./run-mutation-tests.sh對當前工作目錄中的所有代碼進行突變測試。有關更多信息,請查看PIT MAVEN插件。在Intellij Idea中運行項目測試時,您可能會看到以下錯誤:
java: exporting a package from system module jdk.compiler is not allowed with --release
如果發生這種情況,請轉到設置 - >構建,執行,部署 - >編譯器 - > Java編譯器,並取消選擇選項使用“ - 釋放”選項以進行交叉編譯(Java 9及以後) 。有關詳細信息,請參見Idea-288052。
該項目提供了其他BugChecker實現。
是否需要報告或修復錯誤,建議或添加新功能或改進文檔?太棒了!請閱讀我們的貢獻指南。
如果您想報告安全漏洞,請通過私人渠道進行操作;請參閱我們的安全政策以獲取詳細信息。