⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beanutil.java

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA
字号:
package com.cloudwebsoft.framework.util;

import java.sql.ResultSet;
import java.util.Map;
import java.lang.reflect.Method;
import java.lang.reflect.Field;

public class BeanUtil {

    public static Object getProperty(Object bean, String propertyName) {
        if (bean == null || propertyName == null)return null;

        boolean isMap = bean instanceof Map;
        if (isMap) {
            Object value = ((Map) bean).get(propertyName);
            if (value != null)return value;
        }

        if (bean instanceof ResultSet) {
            ResultSet rs = (ResultSet) bean;
            try {
                return rs.getObject(propertyName);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        return getBeanProperty(bean, propertyName);
    }

    public static Object getBeanProperty(Object bean, String propertyName) {
        Object obj = getBeanPropertyMethodOrField(bean, propertyName);
        if (obj==null)
            return null;
        Object[] params = null;
        Object propertyValue = null;
        try {
            if (obj instanceof Method) {
                propertyValue = ((Method) obj).invoke(bean, params);
            } else {
                propertyValue = ((Field) obj).get(bean);
            }
        } catch (Exception e) {
            LogUtil.getLog(BeanUtil.class).error("getBeanProperty:" +
                                                 e.getMessage());
        }

        return propertyValue;
    }

    public static Object getBeanPropertyMethodOrField(Object bean, String propertyName) {
        if (bean == null || propertyName == null)
            return null;

        Class beanClass = bean.getClass();

        boolean hit = false;
        Object ret = null;

        String propName = Character.toUpperCase(propertyName.charAt(0))
                          + propertyName.substring(1);
        String methodName = "get" + propName;

        Method method = null;
        try {
            Class[] args = null;
            method = beanClass.getMethod(methodName, args);
            ret = method;
            hit = true;
        } catch (Exception e) {
            method = null;
            LogUtil.getLog(BeanUtil.class).error("getBeanPropertyMethodOrField:" + e.getMessage());
        }

        if (!hit) {
            try {
                methodName = "is" + propName;
                Class[] args = null;
                method = beanClass.getMethod(methodName, args);
                ret = method;
                hit = true;
            } catch (Exception e) {
                method = null;
                LogUtil.getLog(BeanUtil.class).error("getBeanPropertyMethodOrField:" + e.getMessage());
            }
        }

        Field field = null;
        if (!hit) {
            try {
                field = beanClass.getField(propertyName);
                ret = field;
                hit = true;
            } catch (Exception e) {
                field = null;
                LogUtil.getLog(BeanUtil.class).error("getBeanPropertyMethodOrField:" + e.getMessage());
            }
        }
        if (ret == null) {
            LogUtil.getLog(BeanUtil.class).error("getBeanPropertyMethodOrField:" + propertyName + " is not found in " + bean.getClass());
        }

        return ret;

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -