how can resolve dodgy:unchecked/unconfirmed cast i

2019-07-18 09:29发布

问题:

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.