2017-09-07 34 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())));