vendredi 16 juin 2023

Change object field value by field name

ENV:

  • Spring Boot 3
  • Java 17

CONTEXT:

I have multiple model classes, some of which have fields that should be changed at runtime. For example, a model has a field String name which should be encrypted and decrypted at runtime on demand. I thought about the following solution:

  • I will create a marker annotation that indicates which fields should be encryptable/decryptable.
  • At runtime, there is a method that accepts objects of different classes and analyzes them via reflection. This part already works, and I can find the annotated fields.

Questions:

  1. Is this a good pattern? I thought it would be elegant to just annotate the related fields, and the rest happens decoupled from the object.
  2. Is there a way to read and write a field just by knowing the field name? Or do I have to create a separate instance for each object by reflection and create a copy?




Aucun commentaire:

Enregistrer un commentaire