当对象的字段非常多, 字段类型又是对象, 对象里面还是对象, 构造一个这样的对象简直痛苦, 又臭又长的代码写一大堆, 写完了逻辑混乱看不懂, 相信这个工具会起到作用
使用
- 将要操作的对象放入工具, 创建实体
- 使用set方法, 用于设置对象字段的内容, 与对象内部的set方法不同的是, 他支持链式串联
- 使用get方法获取对应的对象字段的内容, 与set是相对应的, 同样支持链式串联
- 调用getObject方法获取修改完成的对象, 或者也可以用创建实体时丢进去的变量
- 如果对象的字段是个ArrayList, 则可以使用[i]的方式set或者get数据, 当然支持链式串联
package com.dome.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* 用于便捷构造对象
*/
public class BuildObject<T> {
private final T object;
public BuildObject(T object) {
this.object = object;
}
public T getObject() {
return object;
}
public void set(String names, Object value) {
if (names != null && names.length() > 0 && value != null) {
String[] split = names.split("\\.");
int lsat = split.length - 1;
Object obj = object;
for (int i = 0; i < split.length; i++) {
String name = split[i];
if (i == lsat) {
setObject(obj, name, value);
}else {
if (name.contains("[")) {
String[] split1 = name.split("\\[");
name = split1[0];
int index = Integer.parseInt(split1[1].replaceAll("]", ""));
Object object = getObject(obj, name);
if (object == null) {
obj = generateObject(obj, name);
} else {
obj = object;
}
obj = ((ArrayList)obj).get(index);
}else {
Object object = getObject(obj, name);
if (object == null) {
obj = generateObject(obj, name);
} else {
obj = object;
}
}
}
}
}
}
public void add(String names, Object value) {
if (names != null && names.length() > 0 && value != null) {
Object o = get(names);
if (o != null) {
((ArrayList) o).add(value);
}else {
ArrayList<Object> objects = new ArrayList<>();
objects.add(value);
set(names, objects);
}
}
}
public Object get(String names) {
return get(names, Object.class);
}
public <V> V get(String names, Class<V> entityClass) {
if (names != null && names.length() > 0 ) {
String[] split = names.split("\\.");
int lsat = split.length - 1;
Object obj = object;
for (int i = 0; i < split.length; i++) {
String name = split[i];
if (i == lsat) {
Object object = getObject(obj, name);
if (object == null) {
return null;
}else {
return (V) object;
}
}else {
if (name.contains("[")) {
String[] split1 = name.split("\\[");
name = split1[0];
Integer index = Integer.valueOf(split1[1].replaceAll("]", ""));
Object object = getObject(obj, name);
if (object == null) {
obj = generateObject(obj, name);
} else {
obj = object;
}
obj = ((ArrayList)obj).get(index);
}else {
Object object = getObject(obj, name);
if (object == null) {
obj = generateObject(obj, name);
} else {
obj = object;
}
}
}
}
}
return null;
}
private void setObject(Object source, String name, Object value) {
String methodName = bigHump(name, "set");
try {
Field field = getFieldByName(source, name);
Class<?> cl = Class.forName(field.getType().getName());
Method method = source.getClass().getDeclaredMethod(methodName, cl);
method.invoke(source, value);
} catch (Exception e) {
e.printStackTrace();
}
}
private Object getObject(Object source, String name) {
Class<?> clazz = source.getClass();//获取对象的类型
Object value = null;
try {
//获取 clazz 类型中的 propertyName 的属性描述器
name = bigHump(name, "get");
Method method = clazz.getDeclaredMethod(name);
value = method.invoke(source);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
private Object generateObject(Object source, String name) {
Object o = null;
try {
Field field = getFieldByName(source, name);
Class<?> cl = Class.forName(field.getType().getName());
o = cl.newInstance();
setObject(source, name, o);
} catch (Exception e) {
e.printStackTrace();
}
return o;
}
private Field getFieldByName(Object source, String name) {
try {
// 获取字段
Field[] fields = source.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.getName().equals(name)) {
return field;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String bigHump(String name, String prefix) {
return prefix + name.substring(0, 1).toUpperCase() + name.substring(1);
}
private String bigHump(String name) {
return bigHump(name, "");
}
}