mercredi 9 août 2023

Dynamically loaded class missing members at runtime

I'm trying to load a class dynamically and iterate through the declared fields within it. I'm finding that occasionally, a field that I have recently added to the dynamically loaded class is missing.

The catch (and yes, I understand this is not really best practice) is that the dynamically loaded class is modified by the program just before it is loaded.

Specifically:

  1. A "template" (for lack of a better word) .csv file is read (this can be updated in between runs of the program)
  2. A POJO class is generated based on that template (this is the class we want to dynamically load, and should be re-written on each run to reflect changes in the template)
  3. A Class object is created based on the full qualified name of the class we just made
  4. We iterate through the declared fields within the class.

I've tried making the function that actually generates the POJO class return a value to ensure that everything is running in order (not that that would ever happen but at this point I'm deeply lost).

I've also tried deleting the POJO entirely between runs of the program, to no avail.

The only potential resolution that I've found is running the whole program twice in a row, which seems to somehow make the changes to the POJO class fully settle.

This seems to indicate that in some way, when I load the generated class based on the qualified name, the previous version of the class (prior to any edits to the template) is loaded. The real question is why? Is it possible to ensure that I am loading the absolute most recent version of a class?





Aucun commentaire:

Enregistrer un commentaire