// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.FileInputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.Map.Entry;

public class ObjMap {
    public static void map(Object o1, Object o2, Properties p) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        for(Entry e : p.entrySet()) {
            Method put = (new PropertyDescriptor((String) e.getKey(), o2.getClass())).getWriteMethod();
            Method get = (new PropertyDescriptor((String) e.getValue(), o1.getClass())).getReadMethod();
            put.invoke(o2, get.invoke(o1));
        }
    }
    public static void main(String[] args) throws Exception {
        Properties p = new Properties();
        p.load(new FileInputStream("C:\\map.properties"));
        Foo o1 = new Foo();
        o1.setA(123);
        o1.setB(456);
        Bar o2 = new Bar();
        map(o1, o2, p);
        System.out.println(o2.getC() + " " + o2.getD());
    }
}

class Foo {
    private int a;
    private int b;
    public int getA() {
        return a;
    }
    public void setA(int a) {
        this.a = a;
    }
    public int getB() {
        return b;
    }
    public void setB(int b) {
        this.b = b;
    }
}

class Bar {
    private int c;
    private int d;
    public int getC() {
        return c;
    }
    public void setC(int c) {
        this.c = c;
    }
    public int getD() {
        return d;
    }
    public void setD(int d) {
        this.d = d;
    }
}
