Example usage for org.apache.commons.lang From getClass

List of usage examples for org.apache.commons.lang From getClass

Introduction

In this page you can find the example usage for org.apache.commons.lang From getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.apache.niolex.commons.bean.BeanUtil.java

/**
 * Merge the non null properties from the source bean to the target bean.
 *
 * @param to the target bean/*w  w  w .j  av a 2 s  .  c o m*/
 * @param from the source bean
 * @param mergeDefault whether do we merge default numeric primitives
 * @return the target bean
 */
public static final <To, From> To merge(To to, From from, boolean mergeDefault) {
    try {
        Map<String, Method> writeMap = prepareWriteMethodMap(to.getClass());
        BeanInfo fromInfo = Introspector.getBeanInfo(from.getClass());
        // Iterate over all the attributes of from, do copy here.
        for (PropertyDescriptor descriptor : fromInfo.getPropertyDescriptors()) {
            Method readMethod = descriptor.getReadMethod();
            if (readMethod == null) {
                continue;
            }
            Method writeMethod = writeMap.get(descriptor.getName());
            if (writeMethod == null) {
                continue;
            }
            Object value = readMethod.invoke(from);
            if (value == null) {
                continue;
            }
            if (!mergeDefault && isNumericPrimitiveDefaultValue(readMethod.getReturnType(), value)) {
                continue;
            }
            // Only copy value if it's assignable, auto boxing is OK.
            if (ClassUtils.isAssignable(value.getClass(), writeMethod.getParameterTypes()[0], true)) {
                writeMethod.invoke(to, value);
            }
        }
    } catch (Exception e) {
        throw new IllegalArgumentException("Failed to merge propeties.", e);
    }
    return to;
}