lundi 16 octobre 2017

Overwrite Object properties with properties of other Object using reflection

Im trying to do something that might seem a bit unorthodox, however this is due to some limitations in a framework that im using.

Basically my case is this: 1: I have an object that has several fields, all with default values. 2: I have an other object that is initialized and has all values that I want to have, in the new object.

Im trying to do this with reflection, so looping over all public setMethods, finding all getMethods that seem to be matching from the other object, invoking them and invoke the other setMethod with the value over the invoked setMethod.

This is what I came up with so far:

java.lang.reflect.Method[] publicMethods1 = newlabels.getClass().getMethods();
                        for(int i=0; i<publicMethods1.length; i++){
                            if (publicMethods1[i].getName().startsWith("set")){
                                String setname = publicMethods1[i].getName();
                                String getname = "get"+setname.substring(3, setname.length());
                                try {
                                    java.lang.reflect.Method getMethod = labels.getClass().getMethod(getname, null);
                                    publicMethods1[i].invoke(newlabels, getMethod.invoke(labels, null));
                                } catch (NoSuchMethodException e1) {
                                    //System.out.println("Couldnot find a method with name: "+getname);
                                } catch (SecurityException e1) {
                                    //System.out.println("Security exception occured");
                                } catch (IllegalAccessException e1) {
                                    //System.out.println("IllegalAccessException");
                                } catch (IllegalArgumentException e1) {
                                    //System.out.println("IllegalArgumentException");
                                } catch (InvocationTargetException e1) {
                                    //System.out.println("InvocationTargetException");
                                }
                            }
                        }

This unfortunately isn't working, its not giving errors either (unless a getter wasnt found, but ill take that). I looked around on the internet and found somewhat similar in this method:

/**
 * Only works for methods with equally named getters and setters
 * 
 * 1. Get all declared methods of obj1 and find all setters
 * 2. Get all declared methods of obj2 and find all getters
 * 3. Find which setter belongs to which getter
 * 4. Set the value of obj1.setter with obj2.getter 
 * 
 * @param obj1
 * @param obj2
 */
public static void runAllSettersWithGetters(Object obj1, Object obj2) {
    ArrayList<Method> setters = findSetters(obj1);
    ArrayList<Method> getters = findGetters(obj2);

    for(int s=0; s<setters.size(); s++){
        String setmethodname = setters.get(s).getName();
        String whattoset = setmethodname.substring(3);
        for(int g=0; g<getters.size(); g++){
            boolean isboolean = false;
            boolean match = false;
            if(getters.get(g).getReturnType().equals(Boolean.TYPE)){
                isboolean = true;
            }

            String getmethodname = getters.get(g).getName();
            String whattoget = getmethodname.substring(3);
            if(whattoset.equalsIgnoreCase(whattoget)){
                match = true;
            }else{
                //might start with is instead of get
                whattoget = getmethodname.substring(2);
                if(whattoset.equalsIgnoreCase(whattoget)){
                    match = true;
                }
            }

            if(match){
                try {
                    setters.get(s).invoke(obj1, getters.get(g).invoke(obj2));
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

So I tried it, because it seems better, however in the end it gives the same not working solution.

Can anyone help me with this?





Aucun commentaire:

Enregistrer un commentaire