mercredi 15 décembre 2021

How to get a hash of a Java class' structure?

Imagine I have the following two classes:

public class A {
    private String field1;
    private B field2;
}

public class B {
    private String field3;
}

What I want to do is use Java Reflection to get all the fields and all the subfields from class A and create a hash out of it. In this example, I would get, say, a list of Strings saying ["field1", "field2", "field3"], and I would hash the entire list.

In case it helps, the reason I am doing this is because I have a MongoDB collection for A. However, every once in a while, I add new fields to A or B, and I need to refresh the entire collection with the new fields when that happens. For this, I need to keep track, in a separate collection, of whether I have already refreshed A's collection yet or not for its current structure. The easiest way would be to just hash its fields and subfields names.





Aucun commentaire:

Enregistrer un commentaire