Monday, October 12, 2020

Java Basic Util - CopyUtil

package basic.util;


import java.lang.reflect.Field;

import java.math.BigInteger;

import java.time.LocalDateTime;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


public class CopyUtil {

public static void copy(Object src, Object dest) {

Map<String, Object> srcMap = getSourceValues(src);

populateDestination(dest, srcMap);

}


private static void populateDestination(Object dest, Map<String, Object> srcMap) {

List<Field> fieldList = getAllFields(dest);


for (Field fd : fieldList) {

if (srcMap.get(fd.getName()) == null) continue;

try {

fd.setAccessible(true);

fd.set(dest, srcMap.get(fd.getName()));

} catch (Exception e) {

e.printStackTrace();

}

}

}


private static Map<String, Object> getSourceValues(Object src) {

Map<String, Object> srcMap = new HashMap<String, Object>();

List<Field> fieldList = getAllFields(src);


for (Field fd : fieldList) {

try {

fd.setAccessible(true);

srcMap.put(fd.getName(), fd.get(src));

} catch (Exception e) {

e.printStackTrace();

}

}

return srcMap;

}


private static List<Field> getAllFields(Object src) {

List<Field> fieldList = new ArrayList<>();


Class<?> tempClass = src.getClass();

while (tempClass != null) {

fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));

tempClass = tempClass.getSuperclass();

}

return fieldList;

}

public static void main(String[] args) {

CopyUtil copyUtil = new CopyUtil();

Child child = copyUtil.new Child();


child.setBigInt(new BigInteger("123456"));

child.setDate(LocalDateTime.now());

child.setInteger(21);

child.setString("test");

child.setFnum(11.11f);

child.setDnum(22.22d);

child.setCharacter("parent string");

Child another = copyUtil.new Child();

CopyUtil.copy(child, another);

System.out.println("BigInt:" + another.getBigInt());

System.out.println("Date:" + another.getDate());

System.out.println("Character:" + another.getCharacter());

System.out.println(another.getInteger());

System.out.println(another.getString());

System.out.println(another.getFnum());

System.out.println(another.getDnum());

}


class Parent {

private BigInteger bigInt;


private LocalDateTime date;


private String character;


public BigInteger getBigInt() {

return bigInt;

}


public void setBigInt(BigInteger bigInt) {

this.bigInt = bigInt;

}


public LocalDateTime getDate() {

return date;

}


public void setDate(LocalDateTime date) {

this.date = date;

}


public String getCharacter() {

return character;

}


public void setCharacter(String character) {

this.character = character;

}

}


class Child extends Parent {

private int integer;

private float fnum;

private double dnum;

private String string;


public int getInteger() {

return integer;

}


public void setInteger(int integer) {

this.integer = integer;

}


public String getString() {

return string;

}


public void setString(String string) {

this.string = string;

}


public float getFnum() {

return fnum;

}


public void setFnum(float fnum) {

this.fnum = fnum;

}


public double getDnum() {

return dnum;

}


public void setDnum(double dnum) {

this.dnum = dnum;

}

}

}


No comments:

Post a Comment