You could do
b.setValue2(a.getValue2().longValue());
But if a.value2 isn't actually an integer (e.g. it's a Double with a fractional component) this will lose data.
Correspondingly
b.setValue1(a.getValue1().doubleValue());
Edit
Ok I think I've got a grasp on your situation. Here's a dirty way to go about what you want to do. Basically you need to have a transform method which will transform a Number into another Number based on a chosen class. That class you get from the Method itself. So it will be something like this:
public static void main(String[] args) throws Exception {
A a = new A();
a.setValue1(1.0);
a.setValue2(5);
B b = new B();
Method[] methods = b.getClass().getMethods();
for ( Method m : methods ) {
if ( m.getName().equals("setValue2") ) {
m.invoke(b, transform(a.getValue2(), m.getParameterTypes()[0]));
}
}
System.out.println(b.getValue2());
}
private static Number transform(Number n, Class<?> toClass) {
if ( toClass == Long.class ) {
return n.longValue();
} else if ( toClass == Double.class ) {
return n.doubleValue();
}
//instead of this you should handle the other cases exhaustively
return null;
}
The reason you would otherwise get an IllegalArgumentException in the above is because with a, value2 is not being set to a Long, it's being set to an Integer. They are disjoint types. If a.value2 was actually set to be a Long instead, you wouldn't have that error.