Object mapping in Java

CrazYmonkeY159

Expert Member
Joined
Sep 13, 2007
Messages
2,142
Reaction score
0
Location
CPT/PE
Hi guys

So I have a quick question. I have two classes in Java, they are almost identical (except one or two different methods) both are named differently for semantic reasons. but both have the same setter and getter methods and both have the same instance variables.

That being said. Is there any clever way for me to translate one object to another without going through all the setters or getters?

I'm asking because 1) I want to learn how to do this 2) I want to see if there is a simpler way than just doing it the long way. To clarify

lets say I have two classes:

[table="width: 500"]
[tr]
[td]
Class A:
int name
int surname
String getSurname()
void setSurname()
String getName()
void setName()
//--other methods specific to class A​
[/td]
[td]
Class B:
int name
int surname
String getSurname()
void setSurname()
String getName()
void setName()
//--other methods specific to class B​
[/td]
[/tr]
[/table]​

If anyone can point me in the right direction that would be great. If the solution exists then i've been googling the wrong search terms
 
For one thing, those getters and setters should be part of an interface. So, you have A and B, which each implement interface X, which provides getName(), getSurname(), etc.

Or you could use an abstract class for this, not sure what your inheritance hierarchy isnt. I wouldnt unless you really need to.

Anyway, you then have two options. One is to write a class with a method that takes in two objects of type X, and copies all variables from X1 to X2. Or, A and B could each implement methods which do the same.

But, to answer your question, you will still need to use
setName(a.getName());
setSurname(a.getSurname());

As far as I can tell. Java has some interesting reflection capabilities, but the problem is, I'm not sure how you would do the mapping. Someone who has more experience with Java reflection could tell you.
 
Yeah I went down the reflection road however, I have descovered BeanUtils from the springframework. I have not tested it out but it seems to work. I am also told that manually using the getter and setter methods is the fastest way to go so im reconsidering. Thanks anyways.
 
you could do this:

Create a class that contains the shared info:

public class SomeInfo {

private String name;
private String surname;

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the surname
*/
public String getSurname() {
return surname;
}

/**
* @param surname the surname to set
*/
public void setSurname(String surname) {
this.surname = surname;
}

}

Then with A and B:

public class A {

private SomeInfo myInfo;

public void doSomething() {

}

/**
* @return the myInfo
*/
public SomeInfo getMyInfo() {
return myInfo;
}

/**
* @param myInfo the myInfo to set
*/
public void setMyInfo(SomeInfo myInfo) {
this.myInfo = myInfo;
}

}


public class B {

private SomeInfo myInfo;

public void doSomethingElse() {

}

/**
* @return the myInfo
*/
public SomeInfo getMyInfo() {
return myInfo;
}

/**
* @param myInfo the myInfo to set
*/
public void setMyInfo(SomeInfo myInfo) {
this.myInfo = myInfo;
}

}

One way not to duplicate unnecessary code.

2 cents worth.. :-)
 
adidas, these two classes are developed and maintained by two seperate teams within the company i'm in so that is not really doable. my team maintains the one class, and another team maintains the other.
 
adidas, these two classes are developed and maintained by two seperate teams within the company i'm in so that is not really doable. my team maintains the one class, and another team maintains the other.

Okay then!

I'd map it the old fashioned way. Its boring, but it works, and if anything changes (ie variable names), your build will break immediately and you will know about it.
 
So you want to copy the internal state of one object to another? Repeating what others said, you really should have a common base class if there are so many similarities. Even more so if both teams work in the same company. Talk about waste of resources developing almost exactly the same code multiple times.

Code:
public class XY
{
  private int x;
  private int y;

  public void setX(int x) { this.x = x; }
  public void setY(int y) { this.y = y; }

  public int getX() { return x; }
  public int getY() { return y; }

  public void copyFrom(XY xy)
  {
    this.x = xy.x;
    this.y = xy.y;
  }
}

public class A extends XY
{
  private int a;

  public void setA(int a) { this.a = a; }

  public int getA() { return a; }

  public void copyFrom(XY xy)
  {
    super.copyFrom(xy);

    if (xy instanceof A)
    {
      this.a = ((A)xy).a;
    }
  }
}

public class B extends XY
{
  private int b;

  public void setB(int b) { this.b = b; }

  public int getB() { return b; }

  public void copyFrom(XY xy)
  {
    super.copyFrom(xy);
    if (xy instanceof B)
    {
      this.b = ((B)xy).b;
    }
  }
}

A a = new A();
a.x = 1;
a.y = 2;
a.a = 3;

B b = new B();
b.copyFrom(a);

In this way you only have one definition of copying the common internal state. Sub-classes then are responsible for copying their own internal state. There is no need to use getters and setters everytime you need to duplicate state. Obviously none of this helps if you cannot use a common base. In which case, you just have to copy all the internal state manually using the getters and setters. Alternatively and highly not recommended, use reflection. If you need more information on that, probably shouldn't be doing it.
 
adidas, these two classes are developed and maintained by two seperate teams within the company i'm in so that is not really doable. my team maintains the one class, and another team maintains the other.

unless your team and the other is at war with each other... why not work together on a mutual library containing these classes?
 
What you want to do will tightly couple two classes together. This is a bad idea when both classes are maintained by the same team in the same source repository but when across two teams is a recipe for misery.

Either use the setters and getters or use a shared state class in a common library that has strict change controls.
 
unless your team and the other is at war with each other... why not work together on a mutual library containing these classes?

well our company is huge... huge! And im just a lowly software dev intern. Also the classes/library maintained by the other team is actually public information. The stuff which im working on is all internal. basically my stuff depends on their stuff but I cant inherit from the other teams' classes for other reasons. I think ill go with the lazy way for now.
 
Top
Sign up to the MyBroadband newsletter
X