Java tutorial
/** * Copyright (c) 2011-2014, L.cm (596392912@qq.com). * * 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; /** * <p> * CGlibBeanUtils??CGlib * </p> * * @author L.cm * @date 2016-04-15 */ public final class BeanUtil extends BeanUtils { protected BeanUtil() { /* ? */ } /** * * * @param clazz * * @return */ @SuppressWarnings("unchecked") public static <T> 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> 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> T clone(Object src, Class<T> 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<K, V>} */ @SuppressWarnings("unchecked") public static <K, V> Map<K, V> toMap(Object src) { return BeanMap.create(src); } }