• 反射工具


    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);
    }
    
    }
  • 相关阅读:
    游戏热更思路
    Shader实现新手指引挖空圆形和矩形
    C#利用栈实现字符串运算解析
    Unity UGUI和特效(含粒子系统和3D Object)之间层级问题
    Unity中的值传递与引用传递
    Unity3D中的线程与协程
    进程、线程、协程、CPU
    Photon Server与Unity3D客户端的交互
    Photon Server的Unity3D客户端配置
    Photon Server的服务器端配置
  • 原文地址:https://www.cnblogs.com/dayuss/p/11508837.html
Copyright © 2020-2023  润新知