与克隆的Java反射

时间:2017-09-07 02:59:25

标签: java reflection clone

示例我在

之后有数据层
public class DemoData implements Cloneable {

    private String name;
    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone(); //To change body of generated methods, choose Tools | Templates.
    }
}

我想将数据值(DemoData)分配给重复数据(DemoData clone)图层,如下所示

public static void main(String[] args) {
        DemoData demoData = new DemoData();
        demoData.setName("Class Sources");
        testReflectionDemo(demoData);
    }

    private static DemoData testReflectionDemo(DemoData demoData) {
        try {
            DemoData clone = (DemoData) demoData.clone();
            clone.setName(demoData.getName());
            clone.setValue(demoData.getValue());
            return clone;
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

我想将方法​​testReflectionDemo(DemoData demoData)转换为方法testReflectionDemo(T t)反射,如下所示。我不知道如何继续,请帮帮我

public <T> T testReflectionDemo(T t){
        Class<?> aClass = t.getClass();
        for (Method method : aClass.getMethods()) {

        }
        return null;
    }

2 个答案:

答案 0 :(得分:1)

如果testReflectionDemo的参数是javabean,则意味着参数类有几对setXXX和&#39; getXXX ,and the getXXX {{1}的方法setXXX`只有一个参数。如果是这样,下面的代码可以将属性从旧对象复制到新对象。

don't have argument,the

Class<?> aClass = t.getClass(); Object result = aClass.newInstance(); Map<String,MethodHolder> map=new HashMap<>(); for (Method method : aClass.getMethods()) { if(method.getName().startsWith("get") && method.getParameterTypes().length==0){ String property=method.getName().substring(3); MethodHolder hodler = map.get(property); if(hodler ==null){ map.put(property, new MethodHolder(property, method, null)); continue; } hodler.getMethod=method; }else if (method.getName().startsWith("set") && method.getParameterTypes().length==1) { String property=method.getName().substring(3); MethodHolder holder = map.get(property); if(holder ==null){ map.put(property, new MethodHolder(property, null, method)); continue; } holder.setMethod=method; } } List<MethodHolder> collect = map.values().stream().filter(item -> item.setMethod != null && item.getMethod != null).collect(Collectors.toList()); for (MethodHolder holder : collect) { Object property = holder.getMethod.invoke(t); holder.setMethod.invoke(result,property); } return (T)result; 只有一些字段:

MethodHolder

注意以下代码只是浅层复制。

答案 1 :(得分:0)

谢谢大家对我的问题的帮助,我已经删除了克隆方法,我只是应用了reflection.Hi @dabaicai.Your代码帮助了我的想法,我认为将值传递给私有字段会更容易一点点。

public static <T> T clazzClone(T t) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class<?> clazzRoot = t.getClass();

        Object newInstance = clazzRoot.newInstance();
        Field[] fieldsClone = newInstance.getClass().getDeclaredFields();
        for (Field fieldClone : fieldsClone) {
            fieldClone.setAccessible(true);
            fieldClone.set(newInstance, getContent(t, fieldClone.getName()));
        }
        return (T) newInstance;
    }

    private static String getContent(Object aClass, String name) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Field declaredField = aClass.getClass().getDeclaredField(name);
        declaredField.setAccessible(true);
        return (String) declaredField.get(aClass);
    }

我的程序意味着当我需要编辑用户输入数据以输出我想要的结果时,使用通用的过滤功能

 fieldClone.set(newInstance,methodYourEdit(getContent(t, fieldClone.getName())));