javascript
findbugs-dea_FindBugs和JSR-305
findbugs-dea
假設(shè)該組開(kāi)發(fā)人員在大型項(xiàng)目的各個(gè)部分上并行工作-一些開(kāi)發(fā)人員在進(jìn)行服務(wù)實(shí)現(xiàn),而其他開(kāi)發(fā)人員在使用該服務(wù)的代碼。 考慮到API的假設(shè),兩個(gè)小組都同意服務(wù)API,并開(kāi)始單獨(dú)工作。您認(rèn)為這個(gè)故事會(huì)有幸福的結(jié)局嗎? 好吧,…–也許是:) –有一些工具可以幫助實(shí)現(xiàn)它:) –其中之一是FindBugs ,它受JSR-305(用于軟件缺陷檢測(cè)的注釋)支持。
讓我們看一下服務(wù)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);}如您所見(jiàn),在服務(wù)方法簽名中添加了諸如@ Nonnull或@ CheckForNull之類的注釋。 使用它們的目的是定義方法參數(shù)的要求(例如, 標(biāo)識(shí)符參數(shù)不能為null ),以及對(duì)方法返回的值的期望(例如,服務(wù)方法的結(jié)果可以為null ,應(yīng)在代碼中檢查一下) )。
所以呢? –您可能會(huì)問(wèn)–我應(yīng)該自己檢查代碼還是讓同事相信他們會(huì)使用這些注釋定義的準(zhǔn)則? 當(dāng)然不是:) –不信任任何人,請(qǐng)使用可驗(yàn)證API假設(shè)的工具,例如FindBugs 。
假設(shè)我們有以下服務(wù)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);} }讓我們嘗試根據(jù)服務(wù)API假設(shè)來(lái)??驗(yàn)證代碼:
FindBugs將分析您的代碼,并切換到顯示潛在問(wèn)題的FindBugs透視圖:
| Null為nonnull參數(shù)傳遞 |
| 可能的空指針取消引用 |
類似地,例如,編寫(xiě)服務(wù)代碼的人可以對(duì)照定義的API假設(shè)來(lái)??驗(yàn)證其工作。 如果為服務(wù)實(shí)現(xiàn)的早期版本運(yùn)行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;}}將發(fā)現(xiàn)以下錯(cuò)誤:
如您所見(jiàn),FindBugs和他的盟友-JSR-305沒(méi)有什么可以隱藏的;)
甜點(diǎn)的幾個(gè)鏈接:
- JSR-305:用于軟件缺陷檢測(cè)的注釋
- JSR 305:一顆子彈還是根本沒(méi)有?
參考: JCG合作伙伴提供的 FindBugs和JSR-305 ? Micha? 術(shù)士思想博客上的Ja?tak。
翻譯自: https://www.javacodegeeks.com/2012/03/findbugs-and-jsr-305.html
findbugs-dea
總結(jié)
以上是生活随笔為你收集整理的findbugs-dea_FindBugs和JSR-305的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 中医备案制何时实行了(中医备案制何时实行
- 下一篇: 带有ActiveMQ和Maven的JMS