how can resolve dodgy:unchecked/unconfirmed cast in sonar?

别来无恙 提交于 2019-12-24 06:27:57

问题


iam getting the exception through sonar in this following code.How can I resolve this.Suggest me.

    @Override 
     public boolean validate(BaseInfo infoObject) { 
     boolean isValid = true; 
     AckTransferPaymentInfo ackTransferPaymentInfo = (AckTransferPaymentInfo) infoObject; 

Dodgy - Unchecked/unconfirmed cast
Unchecked/unconfirmed cast from com.vocalink.acsw.common.validation.info.BaseInfo to com.vocalink.acsw.common.validation.info.AckTransferPaymentInfo in com.vocalink.acsw.validation.rule.T170Rule.validate(BaseInfo)

AckTransferPaymentElement payment = ackTransferPaymentInfo.getTransferPayment();  
if(CreditDebitIndicator.CRDT.equals(ackTransferPaymentInfo.getCreditDebitIndicator()) 
&& ackTransferPaymentInfo.getOriginalPaymentAccount().getAccountName() != null 

回答1:


You can check that the type of infoObject is correct and handle it appropriately when it's not:

if (!(infoObject instanceof AckTransferPaymentInfo)) {
    throw new AssertionError("Unexpected type: " + infoObject);
}
AckTransferPaymentInfo ackTransferPaymentInfo = (AckTransferPaymentInfo) infoObject;

You should verify this does what you want when infoObject is null.



来源:https://stackoverflow.com/questions/12067510/how-can-resolve-dodgyunchecked-unconfirmed-cast-in-sonar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!