美文网首页
通过注解不同类,不同属性之间可以进行copy

通过注解不同类,不同属性之间可以进行copy

作者: 程序猿_小刚 | 来源:发表于2018-05-28 15:06 被阅读0次

// 设置注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author mazg
 *
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PropertyName {
    String name() default ""; 
    int order() default 0; 
    Class<?> type() default String.class;
}

// 解析器 , 不同类,不同属性之间可以进行copy

package com.taodangpu.config.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.util.ReflectionUtils;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.reflect.TypeToken;

/**
 * @author mazg
 *
 */

public class PropertyNameParser {
    /**
     * 
     * @param t 目标类
     * @param v 参数类
     * @return
     */
    public static <T,V> T getBean(T t,V v){
        Field[] fields = t.getClass().getDeclaredFields();
        for (Field field : fields) {
            PropertyName annotation = field.getAnnotation(PropertyName.class);
            if (annotation != null){
                try {
                    if (Optional.fromNullable(ReflectionUtils.findField(t.getClass(), field.getName())).isPresent()
                        && Optional.fromNullable(ReflectionUtils.findField(v.getClass(), annotation.name())).isPresent()){
                        String property = BeanUtils.getProperty(v, annotation.name());
                        if (!Strings.isNullOrEmpty(property)){
                            BeanUtils.setProperty(t, field.getName(), convert(TypeToken.of(annotation.type()),property));
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }

        }
        return t;
    }
    
    /**
     * 
     * @param t 目标类
     * @param v 参数类
     * @return
     */
    public static <T,V> T getBean(T t,Object[] v){
        Field[] fields = t.getClass().getDeclaredFields();
        for (Field field : fields) {
            PropertyName annotation = field.getAnnotation(PropertyName.class);
            if (annotation != null){
                try {
                    if (Optional.fromNullable(ReflectionUtils.findField(t.getClass(), field.getName())).isPresent()){
                        if (Optional.fromNullable(v[annotation.order()]).isPresent()){
                            String property = v[annotation.order()].toString();
                            if (!Strings.isNullOrEmpty(property)){
                                BeanUtils.setProperty(t, field.getName(), convert(TypeToken.of(annotation.type()),property));
                            }                           
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } 
            }

        }
        return t;
    }
    
    /**
     * 
     * @param t 目标类
     * @param v 参数类
     * @return
     */
    public static <T,V> T getReverseBean(T t,V v){
        Field[] fields = v.getClass().getDeclaredFields();
        for (Field field : fields) {
            PropertyName annotation = field.getAnnotation(PropertyName.class);
            if (annotation != null){
                try {
                    if (Optional.fromNullable(ReflectionUtils.findField(t.getClass(), annotation.name())).isPresent()
                            && Optional.fromNullable(ReflectionUtils.findField(v.getClass(), field.getName())).isPresent()){
                        String property = BeanUtils.getProperty(v, field.getName());
                        if (Strings.isNullOrEmpty(annotation.name())){
                            BeanUtils.setProperty(t,field.getName(), convert(TypeToken.of(annotation.type()),property));
                        }else if (!Strings.isNullOrEmpty(property)){
                            BeanUtils.setProperty(t, annotation.name(), convert(TypeToken.of(annotation.type()),property));             
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        }
        return t;
    }
    
    /**
     * 
     * @param t 类型
     * @param value 值
     * @return
     */
    private static <T> Object convert(TypeToken<T> t,String value){
        if (t.isAssignableFrom(LocalDateTime.class) ){
            return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        }else if(t.isAssignableFrom(LocalDate.class)){
            return LocalDate.parse(value, DateTimeFormatter.ISO_DATE);
        }else if (t.isAssignableFrom(BigDecimal.class)){
            return BigDecimal.valueOf(Double.valueOf(value)).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        }else if (t.isAssignableFrom(Long.class)){
            return Long.parseLong(value);
        }else if (t.isAssignableFrom(Boolean.class)){
            return Boolean.parseBoolean(value);
        }else if (t.isAssignableFrom(BigInteger.class)){
            return BigInteger.valueOf(Long.valueOf(value));
        }else if (t.isAssignableFrom(Integer.class)){
            return Integer.valueOf(value);
        }
        return value;
    }   
}

相关文章

网友评论

      本文标题:通过注解不同类,不同属性之间可以进行copy

      本文链接:https://www.haomeiwen.com/subject/xargjftx.html