使用反射覆盖具有其他Object属性的Object属性

时间:2017-10-16 06:46:09

标签: java reflection

我试图做一些看似有点不正统的事情,但这是因为我使用的框架存在一些限制。

基本上我的情况是这样的: 1:我有一个包含多个字段的对象,都具有默认值。 2:我有一个初始化的其他对象,并且在新对象中具有我想要的所有值。

我试图用反射做这个,所以循环遍历所有公共setMethods,找到似乎与另一个对象匹配的所有getMethods,调用它们并使用调用的setMethod上的值调用另一个setMethod。

这是我到目前为止所提出的:

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");
                                }
                            }
                        }

遗憾的是,这不起作用,它也没有给出错误(除非没有找到吸气剂,但生病了)。 我在互联网上环顾四周,发现这种方法有点相似:

/**
 * 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();
                }
            }
        }
    }
}

所以我尝试了,因为它似乎更好,但最终它给出了相同的无效解决方案。

任何人都可以帮我吗?

0 个答案:

没有答案
相关问题