0

There is a method I am writing to validate a response.

    validateAccount(myRequest.getCustody(), errors);


    private void validateAccount(Custody custody, Errors errors) {
        if (null != custody.getCustodyNumber() && StringUtils.isNotBlank(custody.getCustodyNumber().trim())
                && null == custody.getPensionCustody()) {
            errors.rejectValue(PAYLOAD_PENSION_CUSTODY, ERR04, ERR04_PENSION_CUSTODY);
        }
        if (null != custody.getPensionCustody() && custody.getPensionCustody()
                && StringUtils.isBlank(custody.getCommonAccountNumber())) {
            errors.rejectValue(PAYLOAD_COMMON_ACCOUNT_NUMBER, ERR003, ERR003_COMMON_ACCOUNT_NUMBER);
        }
    }

So can we make the validateAccount() method STATIC in this case as object is passed to it being updated there itself in the method. What should be the solution for it for thread safety. Sonar says it should be STATIC but if the object passed in parameter is being updated will it be safe? I am aware that parameter passed should not be updated inside the method, but what is the best solution in this case?

user2173372
  • 483
  • 7
  • 17
  • 1
    Yes. And as long as you are updating the passed in parameter then it's thread local, and safe. – Elliott Frisch Mar 10 '16 at 04:22
  • Possible duplicate of [How to ensure thread safety of utility static method?](http://stackoverflow.com/questions/13910976/how-to-ensure-thread-safety-of-utility-static-method) – Saif Mar 10 '16 at 04:30
  • @ElliottFrisch It is neither thread-local nor safe. You need to ensure external synchronization on the `Custody` object. It would be thread-local and safe to *reassign* the parameter, but to use it to update the object isn't. – user207421 Mar 10 '16 at 04:37
  • @EJP Care to expand on why you think it is not thread-safe? Looks OK to me. I don't see any instance variables of the object being touched. – kiwiron Mar 10 '16 at 06:23

0 Answers0