日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

FindBugs和JSR-305

發布時間:2023/12/3 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FindBugs和JSR-305 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
假設那組開發人員在大型項目的各個部分上并行工作–一些開發人員在進行服務實現,而其他開發人員在使用該服務的代碼。 考慮到API的假設,兩個小組都同意服務API,并開始單獨工作。

您認為這個故事會有幸福的結局嗎? 好吧,…–也許是:) –有一些工具可以幫助實現它:) –其中之一是FindBugs ,它受JSR-305(用于軟件缺陷檢測的注釋)支持。

讓我們看一下服務API合同:

package com.blogspot.vardlokkur.services;import java.util.List;import javax.annotation.CheckForNull; import javax.annotation.Nonnull;import com.blogspot.vardlokkur.entities.domain.Employer;/*** Defines the API contract for the employer service.** @author Warlock* @since 1.0*/ public interface EmployerService {/*** @param identifier the employer's identifier* @return the employer having specified {@code identifier}, {@code null} if not found*/@CheckForNull Employer withId(@Nonnull Long identifier);/*** @param specification defines which employers should be returned* @return the list of employers matching specification*/@Nonnull List thatAre(@Nonnull Specification specification);}

如您所見,在服務方法簽名中添加了諸如@ Nonnull或@ CheckForNull之類的注釋。 使用它們的目的是定義方法參數的要求(例如, 標識符參數不能為null ),以及方法返回的值的期望值(例如,服務方法的結果可以為null ,應在代碼中檢查一下) )。

所以呢? –您可能會問–我應該自己檢查代碼還是讓同事相信他們會使用這些注釋定義的準則? 當然不是:) –不信任任何人,請使用可驗證API假設的工具,例如FindBugs 。

假設我們有以下服務API用法:

package com.blogspot.vardlokkur.test;import org.junit.Before; import org.junit.Test;import com.blogspot.vardlokkur.services.EmployerService; import com.blogspot.vardlokkur.services.impl.DefaultEmployerService;/*** Employer service test.** @author Warlock* @since 1.0*/ public class EmployerServiceTest {private EmployerService employers;@Beforepublic void before() {employers = new DefaultEmployerService();}@Testpublic void test01() {Long identifier = null;employers.withId(identifier);}@Testpublic void test02() {employers.withId(Long.valueOf(1L)).getBusinessName();}@Testpublic void test03() {employers.thatAre(null);} }

讓我們嘗試根據服務API假設來??驗證代碼:

FindBugs將分析您的代碼,并切換到顯示潛在問題的FindBugs透視圖:

為null參數傳遞了null
可能的空指針取消引用

類似地,例如,編寫服務代碼的人可以對照定義的API假設來??驗證其工作。 如果您為服務實現的早期版本運行FindBugs :

package com.blogspot.vardlokkur.services.impl;import java.util.List;import com.blogspot.vardlokkur.entities.domain.Employer; import com.blogspot.vardlokkur.services.EmployerService; import com.blogspot.vardlokkur.services.Specification;/*** Default implementation of {@link EmployerService}.** @author Warlock* @since 1.0*/ public class DefaultEmployerService implements EmployerService {/*** {@inheritDoc}*/public Employer withId(Long identifier) {return null;}/*** {@inheritDoc}*/public List thatAre(Specification specification) {return null;}}

將發現以下錯誤:

如您所見,FindBugs和他的盟友-JSR-305沒有什么可以隱藏的;)

甜點的幾個鏈接:

  • JSR-305:軟件缺陷檢測的批注
  • JSR 305:一顆子彈還是根本沒有?

參考: JCG合作伙伴提供的 FindBugs和JSR-305 ? Micha? 術士思想博客上的Ja?tak。


翻譯自: https://www.javacodegeeks.com/2012/03/findbugs-and-jsr-305.html

總結

以上是生活随笔為你收集整理的FindBugs和JSR-305的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。