2017-09-07 32 views
0

例Java反射我有数据层之后与克隆

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. 
    } 
} 

欲如下

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

我要分配的数据值(DemoData),以一个重复的数据(DemoData克隆)层转换方法testReflectionDemo(demoData demoData)至方法testReflectionDemo(T T)反射,如图below.I不知道如何继续,请大家帮我

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

     } 
     return null; 
    } 
+2

什么clone.setName(demoData.getName的'点() ); clone.setValue(demoData.getValue());'?你知道克隆是如何工作的吗? – shmosel

+0

因为语句正常运行,我认为它可以工作,在实际的程序我已经包括以下功能 clone.setName(的getContent(demoData.getName())); –

+0

那么如果它运行?这并不是正确或必要的。 – shmosel

回答

1

如果testReflectionDemo的参数是一个javabean,则表示该参数的类有几个对的方法setXXX和'getXXX ,and the getXXX don't have argument,the setXXX`只有一个参数。如果是这样,下面的代码可以将属性从旧对象到新对象。

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只是有一些领域:

public static class MethodHolder{ 
    private String property; 
    private Method getMethod; 
    private Method setMethod; 

    public MethodHolder() { 
    } 

    public MethodHolder(String property, Method getMethod, Method setMethod) { 
     this.property = property; 
     this.getMethod = getMethod; 
     this.setMethod = setMethod; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (!(o instanceof MethodHolder)) return false; 
     MethodHolder that = (MethodHolder) o; 
     return Objects.equals(property, that.property); 
    } 

    @Override 
    public int hashCode() { 
     return Objects.hash(property); 
    } 
} 

的下面的代码只是让浅拷贝留意。

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())));