新手原创,不喜轻喷,欢迎交流。
最近项目交付了,想着项目中遇到的一个问题,在传递返回的包装类给app端的时候有的字段位是null,如果这个字段app端需要其中的值,如果获取的值是null的时候会导致app的闪退,为了解决这个问题,我不得不把所有需要传递给app端且有可能为空的字段一个一个赋“”或者0等,为了简便,我把返回的包装类的数据类型只保留String和Integer.写出了以下方法:
@SuppressWarnings("unused")
public class ParameterInit {
public static <E> void inChange2(Object o) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, SecurityException {
Class<? extends Object> c = o.getClass(); //获取要返回的类的class
Method[] methods = c.getMethods(); //获取其中的方法(自定义的方法都是public)
for (Method m : methods) {
String methodName = m.getName(); //获取方法名
if(methodName.startsWith("set")) { //判断方法前缀(整个操作只有set/get有关)
String getMethod = methodName.substring(3); //截取除set外的其余部分
Method method = c.getMethod("get"+getMethod ); //拼成get就变成了这个属性的get方法
Object invoke = method.invoke(o); //因为get方法是没有参数的,所以直接传obj进去运行方法
if( invoke == null ) { //判断当前方法的返回值
Type[] genericParameterTypes = m.getGenericParameterTypes(); //获取set方法的参数类型
if( genericParameterTypes[0] == String.class) { //匹配参数类型m.invoke(o, ""); //如果是String类型就赋值“”
}else if( genericParameterTypes[0] == Integer.class) {
m.invoke(o, 0); //如果是Integer类型就赋值0
}
}
}
}
System.out.println(o.toString()); //测试用,打印看是否所有字段都不为null
}
}
注:以上操作的参数Object是经过springframework的BeanUtils.copyProperties赋值过后的类
做完这些之后高兴的和领导说了一下我做了一个工具类,然后领导说就这个问题把copyProperties研究一下,做一个类似的吧
下面就说说对copyProperties的研究心得,做为一个新手,写的不好大家不要喷,谢谢
先说一下spring源在下载,用git下的方式是
git clone https://github.com/spring-projects/spring-framework.git
spring源码是用gradle写的,所以需要用gradle编译才能放到eclipse中,gradle安装这个大家百度吧
然后用gradle编译的命令是 gradle cleanidea eclipse
以下是copyProperties的源码
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, null, (String[]) null);
}
可以看到,这里面只是一个方法的调用
private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);
for (PropertyDescriptor targetPd : targetPds) {