org.snaker.designer.utils.BeanUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.snaker.designer.utils.BeanUtil.java

Source

/* Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.designer.utils;

import java.lang.reflect.Field;

import org.apache.commons.beanutils.PropertyUtils;

/**
 * bean
 * @author yuqs
 * @version 1.0
 */
public class BeanUtil {
    /**
     * ??
     * @param bean 
     * @param propertyName ??
     * @return 
     */
    public static Object getPropertyValue(Object bean, String propertyName) throws Exception {
        try {
            return PropertyUtils.getProperty(bean, propertyName);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * bean???,?????.
     * Bean,??org,org.manager.id??
     * @param bean 
     * @param propertyName ??
     * @return ??true,?false
     */
    public static boolean hasPropertyName(Object bean, String propertyName) {
        String[] propertyNames = StringUtils.strToStrArray(propertyName, ".");
        Class<?> clzz = bean.getClass();
        Field field = null;

        for (int i = 0; i < propertyNames.length; i++) {

            // ???
            field = hasClassField(clzz, propertyNames[i]);
            if (field == null)
                return false;

            clzz = field.getType();
        }

        return true;
    }

    public static Class<?> getProperyClass(Object bean, String propertyName) {
        try {
            return PropertyUtils.getPropertyType(bean, propertyName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * (Object)clazz???(fieldName),
     * @param clzz
     * @param fieldName
     * @return
     */
    private static Field hasClassField(Class<?> clazz, String fieldName) {
        try {
            return clazz.getDeclaredField(fieldName);
        } catch (Exception e) {
            if (clazz.equals(Object.class))
                return null;
            return hasClassField(clazz.getSuperclass(), fieldName); // ,
        }
    }

    /**
     * ??
     * @param bean 
     * @param propertyName ??
     * @return ,String
     */
    public static String getPropertyValueToStr(Object bean, String propertyName) throws Exception {
        Object val = getPropertyValue(bean, propertyName);

        if (val == null)
            return null;

        return val.toString();
    }

    /**
     * 
     * @param bean 
     * @param propertyName ??
     * @param value 
     */
    public static void setPropertyValue(Object bean, String propertyName, Object value) throws Exception {
        try {
            PropertyUtils.setProperty(bean, propertyName, value);
        } catch (Exception e) {
            throw e;
        }
    }
}