vendredi 16 décembre 2016

Set property of property of property using reflection

Assume we have a class structure:

class Street {
    String name;
    List<Building> buildings;
}

class Building {
    String number;
    int floors;
    List<Apartment> apartments;
}

class Apartment {
    String owner;
}

I know nothing about the structure above in runtime. All I have is a Street object and a String containing data about what properties I have to assign.

For example String "buildings.0/floors#5" means I have to assign the floors property of the first Building in the list to 5. And String "buildings.0/apartments.1/owner#Luke" means I have to assign to "Luke" the owner property of the second Apartment in the list of apartments of the first Building.

I know how to assign a property using reflection. But what if we have to assign property of the property which is a class itself? Is it possible to implement the logic like this?





Aucun commentaire:

Enregistrer un commentaire