Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Every so often I run into a situation where I need to map a set of properties from one object to another object of a different, unrelated class. The set of properties is large enough to make typing out the code by hand tedious. Anti-DRY principle code like this is the result:

if (objectOfType1.getPropertyA() != null){objectOfType2.setSimilarlyNamedPropertyA(someMethod(objectOfType1.getPropertyA()));}
if (objectOfType1.getPropertyB() != null){objectOfType2.setSimilarlyNamedPropertyB(someMethod(objectOfType1.getPropertyB()));}
...skipping some lines...
if (objectOfType1.getPropertyZ() != null){objectOfType2.setSimilarlyNamedPropertyZ(someMethod(objectOfType1.getPropertyZ()));}

Writing and even looking at code like this is a huge pet peeve, but I feel like using an introspection library like BeanUtils to avoid having a couple of methods like this would irritate my coworkers.

I've taken to writing one-off scripts to generate code like this for me. How have other programmers addressed this problem?

Edit: Here's the code translated to use reflection/introspection. Sure it's DRY, but it might make an enemy out of the next programmer on the project, and I can be just as productive generating code like above with a script. That said, if this is more common than I suspect, please fill me in:

    //Is this really a common technique to use when there's a simpler method?
    String[] propertyNamePartsArr = {"A","B", ...skipping some parts..., "Z"};
    List<String> propertyNameParts = new ArrayList<String> (Arrays.asList(propertyNamePartsArr));

    for (String propertyNamePart : propertyNameParts) {
        try {
            Object originalValue = BeanUtils.getProperty(objectOfType1, "property" + propertyNamePart);
            if (originalValue != null) {
                BeanUtils.setProperty(objectOfType2, "similarlyNamedProperty" + propertyNamePart, someMethod(originalValue));
            }
        } catch (IllegalAccessException e) {
            handleIllegalAccessException(e);
        } catch (InvocationTargetException e) {
            handleInvocationTargetException(e);
        } catch (NoSuchMethodException e) {
            handleNoSuchMethodException(e);
        }
    }
share|improve this question
    
Not too familiar with Java, but why not just use reflection? This is the sort of situation where it's very useful. –  Mason Wheeler May 10 '12 at 23:28
    
Are those properties coded by different developers ? are those properties always from objects already defined, or you can change those classes ? –  umlcat May 11 '12 at 1:22
    
Generally, I'm usually not free to change the classes, or at least not much. Usually when I find myself in situations like this I'm getting an object from some third-party API and performing a conversion to another object to use with a different third-party API. Say for example mapping from XML binding classes to a myBatis result map. Any modification to the first would be wiped out next time the XML changes and the class is regenerated. I could change the second but it would still need to have the expected properties with appropriate types. –  user506069 May 11 '12 at 1:55
add comment

1 Answer

You should be using Reflection.

In your particular case you should create a map between methods of Type1 to methods of Type2 (if they always have the same name even better). Then using reflection you'll call getPropertyA in one object of Type1 and call the setter to put the value on the other object of Type2. It's very straight forward and this is a classic case where you need to use things at the metadata level.

share|improve this answer
    
In Java 7 you might be able to use MethidHandles and MethodTypes instead, in theory a more type safe, security manager safe and faster way than Reflection, YMMV of course. –  Martijn Verburg May 11 '12 at 6:29
    
@MartijnVerburg I've heard of it but never really used it so I can't really vouch for it. –  Alex May 11 '12 at 11:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.