属性名不一致的对象深度克隆

来源:互联网 发布:毕向东java教程35天版 编辑:程序博客网 时间:2024/05/21 06:39

参考文档:

http://outofmemory.cn/code-snippet/2749/java-value-object-between-copy-tool-use-mirror-tongyong
http://www.cnblogs.com/bjzhanghao/archive/2004/07/30/28607.html

被克隆的类:MovieSourceTemp.java

public class MovieSourceTemp {    private Integer source_id;                private String source_sign;              private String source_name;               private Integer source_type;             private String appcode;                  public Integer getSource_id() {        return source_id;    }    public void setSource_id(Integer source_id) {        this.source_id = source_id;    }    public String getSource_sign() {        return source_sign;    }    public void setSource_sign(String source_sign) {        this.source_sign = source_sign;    }    public String getSource_name() {        return source_name;    }    public void setSource_name(String source_name) {        this.source_name = source_name;    }    public Integer getSource_type() {        return source_type;    }    public void setSource_type(Integer source_type) {        this.source_type = source_type;    }    public String getAppcode() {        return appcode;    }    public void setAppcode(String appcode) {        this.appcode = appcode;    }    public MovieSource toSource() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException{        Class<?> movieSourceTempClass =  MovieSourceTemp.class;        Class<?> movieSourceClass = MovieSource.class;        Field[] fields = movieSourceClass.getDeclaredFields();        MovieSource movieSource = new MovieSource();        for (Field field : fields) {            Column column = field.getAnnotation(Column.class);            if (column==null){                continue;            }            Field fieldMovieSourceTemp = null;            try {                fieldMovieSourceTemp = movieSourceTempClass.getDeclaredField(column.name());            } catch (NoSuchFieldException e) {                //e.printStackTrace();                continue;            }            if (fieldMovieSourceTemp!=null){                //movieSourceTempClass拿出来                String movieSourceTempMethodName =fieldMovieSourceTemp.getName().substring(0, 1).toUpperCase()+fieldMovieSourceTemp.getName().substring(1);                String getMethodName = "get"+movieSourceTempMethodName;                Method getMethod =  movieSourceTempClass.getMethod(getMethodName);                //setmovieSourceClass这里面去                String movieSourceMethodName = field.getName().substring(0, 1).toUpperCase()+field.getName().substring(1);                String setMethodName = "set"+movieSourceMethodName;                Method setMethod =  movieSourceClass.getMethod(setMethodName,field.getType());                Object value = getMethod.invoke(this);                setMethod.invoke(movieSource,value);            }        }        return movieSource;    }
//需要克隆的类:MovieSource.java

@Entity@Table(name = "movie_source")@Level(Level.TYPE.PRIVATE)@org.hibernate.annotations.Entity(dynamicUpdate=true,dynamicInsert=true)public class MovieSource extends AbstractItem implements ItemBase {   /**    *     */   private static final long serialVersionUID = 1L;      public MovieSource (){}      @Column( name = "source_id" , nullable=false ,length = 10)   @Id   private Integer sourceId;        @Column( name = "source_sign" ,length = 50 )   private String sourceSign;       @Column( name = "source_name" ,length = 50 )   private String sourceName;       @Column( name = "source_type" ,length = 2 )   private Integer sourceType;      @Column( name = "appcode" ,length = 50 )   private String appcode;      public Integer getSourceId() {      return sourceId;   }   public void setSourceId(Integer sourceId) {      this.sourceId = sourceId;   }   public String getSourceSign() {      return sourceSign;   }   public void setSourceSign(String sourceSign) {      this.sourceSign = sourceSign;   }   public String getSourceName() {      return sourceName;   }   public void setSourceName(String sourceName) {      this.sourceName = sourceName;   }   public Integer getSourceType() {      return sourceType;   }   public void setSourceType(Integer sourceType) {      this.sourceType = sourceType;   }   public String getAppcode() {      return appcode;   }   public void setAppcode(String appcode) {      this.appcode = appcode;   }   }

0 0