Given a map of name-value pairs, how would you blindly set the correct values on a contain object that makes more sense to hold those values? For example, suppose you have a map of location / address data. A simple "Location" would have a address, city, state, postal, etc, modifiers and accessors. How would you load the "Location" object from the map values using reflection? What would you do with the additional data that is not directly accessible on the "Location" object?
Code aside, I would follow the following steps:
- Look up the setters on the location object using the getMethods method
- Match the map keys up with the results of the getMethods call. To do this, you will have to assume the map keys are the field names and "set" + "field name" is the setter on that field. You will also have to adjust the casing of the field name. For example, something simple would be fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1).
- If the "Location" object contains a "set" + "field name", set the value for it. To do this, you can get the method object directly for that setter, location.getClass().getMethod(setterName, classParm). Then invoke the method to set the value you want for that field, method.invoke(location, valueArg).
- If the "Location" object does not contain a setter for the given field name, I would extend the "Location" object to have an "extended data" map for storing extra location values.
Checking for a method name on an object:
Invoking method via reflection on object:String classField = getClassFieldName(nextField);
Method[] methods = Location.class.getMethods();
for(int i=0; i
Location location = new Location();
Class[] classParm = {String.class};
String setterName = "set" + getClassFieldName(nextKey);
Method method = location.getClass().getMethod(setterName, classParm);
String[] valueArg = {nextValue};
method.invoke(location, valueArg);