lundi 26 août 2019

Diff two classes structure

My task

Force two classes to have the same (or similar) structure

Description

I have an entity and DTO. How do I force that if someone adds / removes / changes a field in entity, a test will fail, so DTO structure matches the entity structure.

Using so called diff tool

Let's imagine a diff tool which has input parameters - two objects and output parameter - Map, where Diff is a structure oldValue, newValue. This tool returns difference between input arguments.

public Map<String, Diff> diff(final Object first, final Object second) {
    // This is implemented.
    return innerDiff(first, second, otherParameters); // 
}

public class Diff {
    private String oldValue;
    private String newValue;
    // getters, setters, constructor.
}

// To achieve this, we used Guava Plain map. It works well!

How do I achieve the same for classes. I want to diff two classes and have their fields as difference.

public Map<String, FieldDiff> diff(Class<?> type1, Class<?> type2) {
    // How?
} 

One idea is to use reflection and iterate though all fields of the class.





Aucun commentaire:

Enregistrer un commentaire