com.junly.common.util.ReflectUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.junly.common.util.ReflectUtils.java

Source

/*
 * ?(C) lijun2016-2020
 * Copyright 2016-2020 Zhejiang lijun Technology Co., Ltd.
 *  
 * This software is the confidential and proprietary information of
 * Zhejiang lijun Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Zhejiang lijun
 */
package com.junly.common.util;

import java.lang.reflect.Field;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/** <p class="detail">
 * Json? ??
 * ????  code?detail
 * </p>
 * @ClassName: ReflectUtils 
 * @version V1.0  
 * @date 2016612 
 * @author panwuhai
 * Copyright2016lijun.com,Inc.Allrightsreserved
 */
public class ReflectUtils {

    private static final Log logger = LogFactory.getLog(ReflectUtils.class);

    /**
     * <p class="detail">
     * ?
     * </p>
     * @author wan.Dong
     * @date 20161112 
     * @param obj   
     * @param name   ??
     * @return
     */
    public static Object getProperty(Object obj, String name) {
        if (obj != null) {
            Class<?> clazz = obj.getClass();
            while (clazz != null) {
                Field field = null;
                try {
                    field = clazz.getDeclaredField(name);
                } catch (Exception e) {
                    clazz = clazz.getSuperclass();
                    continue;
                }
                try {
                    field.setAccessible(true);
                    return field.get(obj);
                } catch (Exception e) {
                    return null;
                } finally {
                    field.setAccessible(false);
                }
            }
        }
        return null;
    }

    /**
     * <p class="detail">
     * 
     * </p>
     * @author wan.Dong
     * @date 20161112 
     * @param obj 
     * @param name    ??
     * @param value  (?)??false
     * @return
     */
    public static boolean setProperty(Object obj, String name, Object value) {
        if (obj != null) {
            Class<?> clazz = obj.getClass();
            while (clazz != null) {
                Field field = null;
                try {
                    field = clazz.getDeclaredField(name);
                } catch (Exception e) {
                    clazz = clazz.getSuperclass();
                    continue;
                }
                try {
                    Class<?> type = field.getType();
                    if (type.isPrimitive() == true && value != null) {
                        if (value instanceof String) {
                            if (type.equals(int.class) == true) {
                                value = Integer.parseInt((String) value);
                            } else if (type.equals(double.class) == true) {
                                value = Double.parseDouble((String) value);
                            } else if (type.equals(boolean.class) == true) {
                                value = Boolean.parseBoolean((String) value);
                            } else if (type.equals(long.class) == true) {
                                value = Long.parseLong((String) value);
                            } else if (type.equals(byte.class) == true) {
                                value = Byte.parseByte((String) value);
                            } else if (type.equals(char.class) == true) {
                                value = Character.valueOf(((String) value).charAt(0));
                            } else if (type.equals(float.class) == true) {
                                value = Float.parseFloat((String) value);
                            } else if (type.equals(short.class) == true) {
                                value = Short.parseShort((String) value);
                            }
                        }
                        field.setAccessible(true);
                        field.set(obj, value);
                        field.setAccessible(false);
                    }
                    if (value == null || type.equals(value.getClass()) == true) {
                        field.setAccessible(true);
                        field.set(obj, value);
                        field.setAccessible(false);
                    }
                    return true;
                } catch (Exception e) {
                    return false;
                }
            }
        }
        return false;
    }

    /**
     * <p class="detail">
     * ??
     * </p>
     * @author wudy
     * @date 20161117 
     * @return
     */
    public static String getCurrentClassMethodName() {
        return Thread.currentThread().getStackTrace()[2].getClassName() + "."
                + Thread.currentThread().getStackTrace()[2].getMethodName();
    }

}