一、核心代码
public static void autoValue(Object instance , String values) {
String[] result = values.split("\\|");
for (int i = 0 ; i < result.length ; i++) {
Object currentObj=instance;
Object currentObjBefore=instance;
String[] attVal = result[i].split(":");
try {
if (attVal[0].contains(".")) {
String temp[] = attVal[0].split("\\.");
for (int o = 0 ; o < temp.length -1; o++) {
Field field = currentObj.getClass().getDeclaredField (temp[o]);
Method getMethod = currentObj.getClass().getDeclaredMethod("get" + StringUtilAutoValue.attrib(temp[o]));
Method setMethod = currentObj.getClass().getDeclaredMethod("set" + StringUtilAutoValue.attrib(temp[o]),field.getType());
if (getMethod.invoke(currentObj) == null) {
currentObj = field.getType().getDeclaredConstructor().newInstance();
setMethod.invoke(currentObjBefore , currentObj);
currentObjBefore=currentObj;
}else{
currentObj=getMethod.invoke(currentObj);
}
}
Field field = currentObj.getClass().getDeclaredField(temp[temp.length-1]);
System.out.println(field);
Method method = currentObj.getClass().getDeclaredMethod("set" + StringUtilAutoValue.attrib(temp[temp.length-1]) , field.getType());
System.out.println(method);
Object attributeValue = BeanUtilAutoValue.convertAttributeValue(field.getType().getName() , attVal[1]);
method.invoke(currentObj , attributeValue);
}else{
Field field = currentObj.getClass().getDeclaredField(attVal[0]);
Method method = currentObj.getClass().getDeclaredMethod("set" + StringUtilAutoValue.attrib(attVal[0]) , field.getType());
Object attributeValue = BeanUtilAutoValue.convertAttributeValue(field.getType().getName() , attVal[1]);
method.invoke(currentObj , attributeValue);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、完整代码
package reflect.andjavaclass;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AttribAutoValue {
public static void main(String[] args) {
String values = "name:张三|age:28|property:20000.209020|sex:男|birthday:1999-09-19|home.room.size:100平分|home.address:潮汕";
PersonAutoValue person = (PersonAutoValue) ClassInstanceFactoryAutoValue.getInstance(PersonAutoValue.class , values);
System.out.println(person.toString());
}
}
class ClassInstanceFactoryAutoValue {
private ClassInstanceFactoryAutoValue() {
}
public static <T> T getInstance(Class<T> clazz , String values) {
try {
T t = (T) clazz.getDeclaredConstructor().newInstance();
BeanUtilAutoValue.autoValue(t , values);
return t;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
class StringUtilAutoValue {
public static String attrib(String value) {
if (value == null || "".equals(value)) {
return value;
} else if (value.length() == 1) {
return value.toUpperCase();
} else {
return value.substring(0 , 1).toUpperCase() + value.substring(1);
}
}
}
class BeanUtilAutoValue {
public static void autoValue(Object instance , String values) {
String[] result = values.split("\\|");
for (int i = 0 ; i < result.length ; i++) {
Object currentObj=instance;
Object currentObjBefore=instance;
String[] attVal = result[i].split(":");
try {
if (attVal[0].contains(".")) {
String temp[] = attVal[0].split("\\.");
for (int o = 0 ; o < temp.length -1; o++) {
Field field = currentObj.getClass().getDeclaredField (temp[o]);
Method getMethod = currentObj.getClass().getDeclaredMethod("get" + StringUtilAutoValue.attrib(temp[o]));
Method setMethod = currentObj.getClass().getDeclaredMethod("set" + StringUtilAutoValue.attrib(temp[o]),field.getType());
if (getMethod.invoke(currentObj) == null) {
currentObj = field.getType().getDeclaredConstructor().newInstance();
setMethod.invoke(currentObjBefore , currentObj);
currentObjBefore=currentObj;
}else{
currentObj=getMethod.invoke(currentObj);
}
}
Field field = currentObj.getClass().getDeclaredField(temp[temp.length-1]);
System.out.println(field);
Method method = currentObj.getClass().getDeclaredMethod("set" + StringUtilAutoValue.attrib(temp[temp.length-1]) , field.getType());
System.out.println(method);
Object attributeValue = BeanUtilAutoValue.convertAttributeValue(field.getType().getName() , attVal[1]);
method.invoke(currentObj , attributeValue);
}else{
Field field = currentObj.getClass().getDeclaredField(attVal[0]);
Method method = currentObj.getClass().getDeclaredMethod("set" + StringUtilAutoValue.attrib(attVal[0]) , field.getType());
Object attributeValue = BeanUtilAutoValue.convertAttributeValue(field.getType().getName() , attVal[1]);
method.invoke(currentObj , attributeValue);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static Object convertAttributeValue(String attType , String value) {
if ("double".equals(attType) || "java.lang.Double".equals(attType)) {
return Double.parseDouble(value);
} else if ("int".equals(attType) || "java.lang.Integer".equals(attType)) {
return Integer.parseInt(value);
} else if ("java.util.Date".equals(attType)) {
SimpleDateFormat time;
if (value.matches("\\d{4}-\\d{2}-\\d{2}")) {
time = new SimpleDateFormat("yyyy-MM-dd");
} else if (value.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else {
return new Date();
}
try {
return time.parse(value);
} catch (ParseException e) {
e.printStackTrace();
return new Date();
}
} else {
return value;
}
}
}
class PersonAutoValue {
private String name;
private int age;
private String sex;
private double property;
private Date birthday;
private HomeAutoValue home;
public HomeAutoValue getHome() {
return home;
}
@Override
public String toString() {
return "PersonAutoValue{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", property=" + property +
", birthday=" + birthday +
", home=" + home +
'}';
}
public void setHome(HomeAutoValue home) {
this.home = home;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getProperty() {
return property;
}
public void setProperty(double property) {
this.property = property;
}
public PersonAutoValue() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getAge() {
return age;
}
}
class RoomAutoValue {
private String size;
public RoomAutoValue() {
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
@Override
public String toString() {
return "RoomAutoValue{" +
"size='" + size + '\'' +
'}';
}
}
class HomeAutoValue {
private String address;
private RoomAutoValue room;
@Override
public String toString() {
return "HomeAutoValue{" +
"address='" + address + '\'' +
", room=" + room +
'}';
}
public RoomAutoValue getRoom() {
return room;
}
public void setRoom(RoomAutoValue room) {
this.room = room;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public HomeAutoValue() {
}
}