StringUtil

// 去除空格
public static Object objectTrim(Object obj) {
        Class<? extends Object> ownerClass = obj.getClass();
        Method[] methodArray = obj.getClass().getDeclaredMethods();

        try {
            for(int i = 0; i < methodArray.length; ++i) {
                String temp = null;
                String tempMethod = null;
                if ("get".equals(methodArray[i].getName().substring(0, 3))) {
                    temp = null;
                    tempMethod = methodArray[i].getName().substring(3);
                    if (methodArray[i].invoke(obj) instanceof String) {
                        temp = (String)methodArray[i].invoke(obj);
                    }

                    if (temp != null) {
                        temp = temp.trim();
                        ownerClass.getMethod("set".concat(tempMethod), String.class).invoke(obj, "".equals(temp) ? null : temp);
                    }
                }
            }
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return obj;
    }

 

posted @ 2019-03-08 14:41  亲爱的阿道君  阅读(179)  评论(0编辑  收藏  举报