zoukankan      html  css  js  c++  java
  • 反射工具

    package com.ljyq.central.common.util;
    
    import com.google.common.collect.Maps;
    import com.ljyq.central.common.annotation.ExportFiledComment;
    import org.apache.commons.lang3.StringUtils;
    import org.reflections.ReflectionUtils;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    import java.util.Date;
    import java.util.LinkedHashMap;
    import java.util.Objects;
    import java.util.Set;
    
    
    public final class ReflectionPlusUtils extends ReflectionUtils {
    
    private final static String GETTERS_PREFIX = "get";
    
    /**
    * 反射调用对象字段的Getters方法,结果定制输出成字符串
    * <p>
    * 只适用于个别业务场景
    *
    * @param object
    * @param fieldName
    * @return 结果定制化
    */
    public static String invokeFieldGettersMethodToCustomizeString(Object object, String fieldName) {
    final Object methodResult = invokeFieldGettersMethod(object, fieldName);
    if (Objects.isNull(methodResult)) {
    return StringUtils.EMPTY;
    }
    if (methodResult.getClass().isEnum()) {
    return invokeFieldGettersMethod(methodResult, "comment").toString();
    } else if (methodResult instanceof Date) {
    return DateUtils.formatDateByStyle((Date) methodResult);
    }
    return methodResult.toString();
    }
    
    /**
    * 获取字段属性说明
    *
    * @param clazz
    * @return
    */
    public static LinkedHashMap<String, String> exportFiledComment(Class<?> clazz) {
    Set<Field> fields = getAllFields(clazz, withAnnotation(ExportFiledComment.class));
    LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>(
    Maps.newHashMapWithExpectedSize(fields.size()));
    fields.forEach(field -> {
    String filedComment = field.getAnnotation(ExportFiledComment.class).value();
    if (StringUtils.isEmpty(filedComment)) {
    filedComment = field.getName();
    }
    linkedHashMap.put(field.getName(), filedComment);
    });
    return linkedHashMap;
    }
    
    /**
    * 获取字段属性说明
    *
    * @param clazz
    * @param keyPrefix
    * 字段前缀
    * @param valuePrefix
    * 值的前缀
    * @return
    */
    public static LinkedHashMap<String, String> exportFiledComment(Class<?> clazz, String keyPrefix,
    String valuePrefix) {
    Set<Field> fields = getAllFields(clazz, withAnnotation(ExportFiledComment.class));
    LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>(
    Maps.newHashMapWithExpectedSize(fields.size()));
    fields.forEach(field -> {
    String filedComment = field.getAnnotation(ExportFiledComment.class).value();
    if (StringUtils.isEmpty(filedComment)) {
    filedComment = field.getName();
    }
    linkedHashMap.put(keyPrefix + field.getName(), valuePrefix + filedComment);
    });
    return linkedHashMap;
    }
    
    ///////////////////////////////////////////////////////////////////////////
    // 下面为通用方法
    ///////////////////////////////////////////////////////////////////////////
    
    public static Set<Method> getMethods(Class<?> clazz, final Class<? extends Annotation> annotation) {
    return getMethods(clazz, withAnnotation(annotation));
    }
    
    /**
    * 反射调用对象字段的Getters方法
    *
    * @param object
    * : 该对象
    * @param fieldName
    * : 该对象字段
    * @return 该对象字段Getters方法结果
    */
    public static Object invokeFieldGettersMethod(Object object, String fieldName) {
    if (Objects.isNull(object)) {
    return null;
    }
    final Method gettersMethod = getFieldGettersMethod(object.getClass(), fieldName);
    if (Objects.isNull(gettersMethod)) {
    return null;
    }
    try {
    return gettersMethod.invoke(object);
    } catch (IllegalAccessException | InvocationTargetException e) {
    LogUtils.getLogger().error(e);
    return null;
    }
    }
    
    /**
    * 反射得到对象字段Getters方法
    *
    * @param clazz
    * : 对象
    * @param fieldName
    * : 对象字段
    * @return <code>getFieldName</code>方法
    */
    public static Method getFieldGettersMethod(Class<?> clazz, String fieldName) {
    if (Objects.isNull(clazz) || StringUtils.isEmpty(fieldName)) {
    return null;
    }
    final Set<Method> fieldNameGettersMethods = getMethods(clazz, withModifier(Modifier.PUBLIC),
    withName(buildGetters(fieldName)), withParametersCount(0));
    return fieldNameGettersMethods.parallelStream().findFirst().orElse(null);
    }
    
    /**
    * 构建字段Getters方法名称
    *
    * @param fieldName
    * 字段
    * @return <code>getFieldName</code>
    */
    private static String buildGetters(String fieldName) {
    return GETTERS_PREFIX + StringPrivateUtils.firstCharToUpperCase(fieldName);
    }
    
    }
  • 相关阅读:
    Python Day 30 网络编程 (串讲 FTP作业)
    Python Day 29 网络编程 ( socket中的一些常见方法,登录验证,socketserver模块)
    Python Day 28 网络编程 (socket远程命令执行, tcp黏包现象,以及struck模块的使用 )
    Python Day 27 网络编程 (socket udp)
    Python Day 26 网络编程 ( 网络编程基础 socket tcp)
    Python Day 25 蜥蜴串讲
    Python Day 24 面向对象进阶(双下方法 __new__ __del__ item系列 异常处理)
    Python Day 23 面向对象进阶(内置方法:反射,isinstance和issubclass,__str__和__repr__和其他双下方法)
    Python Day 21 面向对象 (面向对象的三大特性(二)继承,多态,封装,几个装饰器函数)
    aaencode:用颜文字来加密吧
  • 原文地址:https://www.cnblogs.com/dayuss/p/11508837.html
Copyright © 2011-2022 走看看