com.buession.beans.AbstractBeanConverter Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to you 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.
*
* =========================================================================================================
*
* This software consists of voluntary contributions made by many individuals on behalf of the
* Apache Software Foundation. For more information on the Apache Software Foundation, please see
* .
*
* +-------------------------------------------------------------------------------------------------------+
* | License: http://www.apache.org/licenses/LICENSE-2.0.txt |
* | Author: Yong.Teng |
* | Copyright @ 2013-2023 Buession.com Inc. |
* +-------------------------------------------------------------------------------------------------------+
*/
package com.buession.beans;
import com.buession.beans.converters.*;
import com.buession.core.utils.Assert;
import com.buession.core.utils.EnumUtils;
import com.buession.core.utils.FieldUtils;
import com.buession.core.utils.StringUtils;
import com.buession.lang.Primitive;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.beans.BeanMap;
import java.io.File;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Bean 转换器抽象类
*
* @author Yong.Teng
* @since 2.3.1
*/
public abstract class AbstractBeanConverter implements BeanConverter {
private final static Map BEAN_COPIERS = new ConcurrentHashMap<>(32);
private final static Map, BeanPropertyConverter>> converters = new ConcurrentHashMap<>(24);
/**
* 构造函数
*/
public AbstractBeanConverter() {
initDefaultBeanPropertyConverters();
}
/**
* 注册 bean 属性转换器
*
* @param type
* 类型
* @param propertyConverter
* bean 属性转换器
*/
public void registerConverter(Class> type, BeanPropertyConverter> propertyConverter) {
converters.put(type, propertyConverter);
}
@Override
public T convert(final S source, final T target) {
Assert.isNull(target, "No destination bean specified.");
if(source == null){
return null;
}
final boolean isSourceMap = source instanceof Map;
final boolean isTargetMap = target instanceof Map;
if(isSourceMap && isTargetMap){
return mapToMap(source, target);
}
if(isSourceMap && isTargetMap == false){
return mapToBean(source, target);
}
if(isSourceMap == false && isTargetMap){
return beanToMap(source, target);
}
return beanToBean(source, target);
}
@SuppressWarnings({"rawtypes", "unchecked"})
protected static Object convertValue(final Object value, final Class> targetType) {
if(value == null){
return null;
}else if(targetType.isEnum()){
return EnumUtils.getEnumIgnoreCase((Class) targetType, value.toString());
}else{
final Class> targetWrapperType = Primitive.primitiveToWrapper(targetType);
if(targetWrapperType == value.getClass()){
return value;
}else{
final BeanPropertyConverter> propertyConverter = converters.get(targetWrapperType);
return propertyConverter == null ? value : propertyConverter.convert(value);
}
}
}
@SuppressWarnings({"unchecked"})
protected T mapToMap(final S source, final T target) {
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy