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

eclipse, exception, findbugs, java, sonarqube

Solution

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.

Problem

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 ```

Original source