Copying one class's fields into another class's identical fields
Asked Answered
H

4

15

I have this question. But it will be difficult for me to explain as I don't know exact terms to use. Hope someone will understand. I'll try to discribe to the best i can. I feel like this is much related to parsing

Say there are two classes. And in both classes I have some variables, say strings (just for simplicity, variable type can be any), which have similar names.

Eg:
    class ClassA{
        String x,y,z;
    }

    class ClassB{
        String x,y,z;
    }

Now, what i need is, i need to copy the value of one class's variable values to other classes corresponding variable.

Eg:
    ClassA aa=new ClassA();
    ClassB bb=new ClassB();
    //set bb's variables
    aa.x=bb.x;
    aa.y=bb.y;
    aa.z=bb.z;

like that.

But please note that what i need is not the above method. I hope there will be a way to write a simple method, so that it will identify the relevent variable by the name passed to it. Then it will do the value assignment accordingly.

My imagined method is like this,

void assign(String val){        
    // aa.<val>=val
}

For example if you pass bb.x to assign(...) method, then it will do aa.x=bb.x assignment.

Hope this is clear enough. There must be a better way to explain this. If someone know it please edit the post(+title) to make it more clear (But save my idea)..

Please let me know if there's a way to achieve this.

Thanks!

Homeopathist answered 10/8, 2012 at 11:8 Comment(11)
Do you really need a two class for that? Couldn't be just two instances of the same class? Then you can use just clone() method.Gilbertgilberta
You can try a Map or a hashmap... or do something with reflection. This sort of thing has to be purpose-built so why not share a common use-case with us?Thalassic
@Gilbertgilberta Nope. Can't go for cloning. Got two classes. :(Homeopathist
@Thalassic What did you mean by why not share a common use-case with us? (I saw you changing your answer into a comment :p)Homeopathist
What I mean is "give us a better idea where and how this might be used since you might have the wrong idea for solving the problem to begin with.". ClassA, ClassBB is too vague and really doesn't describe anything; sharing real class names might give insight into what they should be doing.Thalassic
Or you could try serializing/deserializing thru XML, that always work.Thalassic
@Thalassic well i'm question seeks a solution like Jean-RémyRevy & SeanPatrickFloyd suggested below. Don't get confused with class names. It's the best I can do to explain. It will be complicated if i try to give more details.Homeopathist
@Thalassic Simply I need a method that can identify the passed variables name and deal with a variable with the same name in some other independent object (both variables will be of the same type).Homeopathist
Here's an honest idea - i'd try dozer, then XML serialization, then reflection... With reflection, you could write a method to compare members of both classes and assign runtime value of one to the other.Thalassic
Thanks @Thalassic it would be appreciated if you can explain a bit about reflection. I haven't come across that.. (You'd better put it as an answer, as this doesn't like extended discussions :D)Homeopathist
The thing is, I never extensively used reflection... that's why I'd leave it for last :) I'll come up with a brief answer now.Thalassic
M
16

Dozer is fine, see Jean-Remy's answer.

Also, if the variables have getters and setters according to the JavaBeans standard, there are a number of technologies that could help you, e.g. Apache Commons / BeanUtils

Sample code (not tested):

final Map<String, Object> aProps = BeanUtils.describe(a);
final Map<String, Object> bProps = BeanUtils.describe(b);
aProps.keySet().retainAll(bProps.keySet());
for (Entry<String, Object> entry : aProps.entrySet()) {
    BeanUtils.setProperty(b,entry.getKey(), entry.getValue());
}

Update:

If you don't have getters and setters, here's a quick hack that copies field values from one class to another as long as the fields have common names and types. I haven't tested it, but it should be OK as a starting point:

public final class Copier {

    public static void copy(final Object from, final Object to) {
        Map<String, Field> fromFields = analyze(from);
        Map<String, Field> toFields = analyze(to);
        fromFields.keySet().retainAll(toFields.keySet());
        for (Entry<String, Field> fromFieldEntry : fromFields.entrySet()) {
            final String name = fromFieldEntry.getKey();
            final Field sourceField = fromFieldEntry.getValue();
            final Field targetField = toFields.get(name);
            if (targetField.getType().isAssignableFrom(sourceField.getType())) {
                sourceField.setAccessible(true);
                if (Modifier.isFinal(targetField.getModifiers())) continue;
                targetField.setAccessible(true);
                try {
                    targetField.set(to, sourceField.get(from));
                } catch (IllegalAccessException e) {
                    throw new IllegalStateException("Can't access field!");
                }
            }
        }
    }

    private static Map<String, Field> analyze(Object object) {
        if (object == null) throw new NullPointerException();

        Map<String, Field> map = new TreeMap<String, Field>();

        Class<?> current = object.getClass();
        while (current != Object.class) {
            for (Field field : current.getDeclaredFields()) {
                if (!Modifier.isStatic(field.getModifiers())) {
                    if (!map.containsKey(field.getName())) {
                        map.put(field.getName(), field);
                    }
                }
            }
            current = current.getSuperclass();
        }
        return map;
    }
}

Call Syntax:

Copier.copy(sourceObject, targetObject);
Mammet answered 10/8, 2012 at 11:20 Comment(9)
Thanks @SeanPatrickFloyd . Yeah, if i had getters and setters this will come in handy. Well, i'll consider that. But i'd like to experiment with that dozer thing now..Homeopathist
@Homeopathist I've added a solution for fields onlyMammet
@Thalassic yeah, but note: it's 10 minutes worth of coding. Don't use this in productionMammet
I meant it more as proof of concept of what i suggested below. But hey, if it works... :)Thalassic
works great! it is missing a "current = current.getSuperclass();" at the end of the "while (current != Object.class)" loop. Without this, it goes into an infinite loop.Olmos
@Olmos First thing I hit was the endless loop, thanks for your fix! Kudos to Sean Patrick Floyd for the algorithm.Dichotomous
I like this, but can you make one that returns a whole new object instead of asking you to pass one into the second parameter?Litta
@Litta that's a lot harder to do in a type safe language. it would require creating the new class on the fly. It can be done with tools like CGLib or ByteBuddy, but it's far from trivial. And it wouldn't do you any good, either, as your client code wouldn't know the class. Better to just return a map in that case.Mammet
Update (May 2023): BeanUtils.copyProperties(sourceObject, targetObject) should work fine.Eta
M
8

Did you ever heared about Dozer ? : http://dozer.sourceforge.net/

Dozer

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.

Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.

Dozer allow you to map Java Beans :

  • using their names (implicit mapping), i.e mapping ClassA.x to ClassB.x
  • providing hability to map diffrent structures (explicit mapping) with different names with (quite simple) xml or annoation configuration .

Here a XML example on the library site :

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">
          
  <mapping> 
    <class-a>org.dozer.vo.TestObject</class-a>
    <class-b>org.dozer.vo.TestObjectPrime</class-b>   
    <field>
      <a>one</a>
      <b>onePrime</b>
    </field>
  </mapping>  

      <!-- SNIP ... -->

</mappings>

This will map object org.dozer.vo.TestObject into TestObjectPrime, mapping all variable that are identicals together (like in your case) and variables TestObjectFoo.oneFoo into TestObjectFooPrime.oneFooPrime.

Great, isn't it ?

Maximalist answered 10/8, 2012 at 11:11 Comment(1)
appreciate your answer. yeah, i haven't ever heard about dozer. Seems great. I'll try that one and give a feedback. Thanks!Homeopathist
D
5

Look here. Just use BeanUtils.copyProperties(newObject, oldObject);

Dollie answered 21/3, 2017 at 6:50 Comment(0)
T
3

new answer.

I'd suggest looking into Dover as it seems pretty straightforward.

The second option is serializing classes into XML and deserializing into your target class only on members that match.

The third option I mentioned in the comment was using reflection - http://java.sun.com/developer/technicalArticles/ALT/Reflection/

This technique allows for a nice design pattern called Introspection - Java introspection and reflection which in turn allows you to discover members of a certain class...

Now, having said that, one would simply "discover" members of ClassA, fill a ArrayList with their names, discover members of ClassB, fill another ArrayList with their names, and copy values of the intersecting set. At least that's my idea on it.

Thalassic answered 10/8, 2012 at 11:12 Comment(1)
This should be a comment, at bestMammet

© 2022 - 2024 — McMap. All rights reserved.