jeudi 11 août 2016

Java reflective assignment of field to null resulting in an IllegaArgumentException

I've got a deep recursive, reflective method that's purpose is to null out any values in the payload that shouldn't be returned to the caller. This set of code

if (function.apply(attribute)) {
      field.setAccessible(true);
      if (!field.getType().isPrimitive()) {
        log.info("field to be set to null = " + field.getType().getName());
        field.set(object, null);
      }

is called repeatedly. When it hits a particular field, I get the error

Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field edu.psu.swe.aed.extensions.data.Citizenship.citizenshipCountry to edu.psu.swe.aed.extensions.PersonBioExtension

Some of the output from the logging statement to show that it's working up to that point looks like this

16:26:35,906 INFO  [edu.psu.swe.scim.server.utility.AttributeUtil] (default task-35) field to be set to null = java.lang.String
16:26:35,906 INFO  [edu.psu.swe.scim.server.utility.AttributeUtil] (default task-35) field to be set to null = edu.psu.iam.services.soap.endpoint.DocumentType
16:26:35,906 INFO  [edu.psu.swe.scim.server.utility.AttributeUtil] (default task-35) field to be set to null = java.util.List
16:26:35,906 INFO  [edu.psu.swe.scim.server.utility.AttributeUtil] (default task-35) field to be set to null = java.util.List
16:26:35,906 INFO  [edu.psu.swe.scim.server.utility.AttributeUtil] (default task-35) field to be set to null = java.lang.String

All I can guess is that I've somehow mangled the call stack or the memory model, but I have no idea how to trouble shoot that. I've completely wiped everything in my target directory to make sure I didn't have a sticky class file, but no luck.

Does anyone have any suggestions as to why this would happen, or advice on how to continue the troubleshooting?

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire