com.baomidou.framework.common.util.BeanUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-wind Show documentation
Show all versions of spring-wind Show documentation
spring-wind extension of spring framework.
The newest version!
/**
* Copyright (c) 2011-2014, L.cm ([email protected]).
*
* 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 com.baomidou.framework.common.util;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.util.Assert;
/**
*
* 基于CGlib,扩展BeanUtils,对于复杂类型的CGlib更有优势
*
*
* @author L.cm
* @date 2016-04-15
*/
public final class BeanUtil extends BeanUtils {
protected BeanUtil() {
/* 保护 */
}
/**
* 实例化对象
*
* @param clazz
* 类
* @return 对象
*/
@SuppressWarnings("unchecked")
public static T newInstance(Class> clazz) {
try {
return (T) clazz.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* 实例化对象
*
* @param clazzStr
* 类名
* @return {T}
*/
public static T newInstance(String clazzStr) {
try {
Class> clazz = Class.forName(clazzStr);
return newInstance(clazz);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
/**
* copy 对象属性到另一个对象,默认不使用Convert
*
* @param src
* @param clazz
* 类名
* @return {T}
*/
public static T clone(Object src, Class clazz) {
BeanCopier copier = BeanCopier.create(src.getClass(), clazz, false);
T to = newInstance(clazz);
copier.copy(src, to, null);
return to;
}
/**
* 拷贝对象
*
* @param source
* 源对象
* @param target
* 需要赋值的对象
*/
public static void copy(Object source, Object target) throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class> actualEditable = target.getClass();
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
for (PropertyDescriptor targetPd : targetPds) {
if (targetPd.getWriteMethod() != null) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null && sourcePd.getReadMethod() != null) {
try {
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
/*
* no copy null properties
*/
Object value = readMethod.invoke(source);
if (value != null) {
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
} catch (Throwable ex) {
throw new FatalBeanException("Could not copy properties from source to target", ex);
}
}
}
}
}
/**
* 将对象装成map形式 注意:生成的是unmodifiableMap
*
* @param src
* 源对象
* @return {Map}
*/
@SuppressWarnings("unchecked")
public static Map toMap(Object src) {
return BeanMap.create(src);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy