zoukankan      html  css  js  c++  java
  • 通过反射对任意class类中方法赋值的方式

    import org.apache.commons.lang3.StringUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;

    import java.lang.reflect.Method;


    @Component
    public class SetPlatformUtils {
    private static final CoreServiceType c = CoreServiceType.getInstance();

    public void setVal(Object obj, String methodName, String platform){
    //获取obj代表的实体类class对象
    Class clazz = obj.getClass();
    Method method = null;
    try {
    //获取class对象中的指定的方法
    method = clazz.getMethod(methodName, String.class);
    //给方法赋值
    if (StringUtils.isBlank(platform)){
    method.invoke(obj,c.CODE.get("1"));
    }else{
    method.invoke(obj, c.CODE.get(platform));
    }
    }
    catch (Exception e) {
    logger.error("存储二级分类的反射工具类出错,obj:{},methodName:{},platform:{},",obj,methodName,platform);
    }
    }
    }

  • 相关阅读:
    ebs R12 支持IE11
    reloc: Permission denied
    3.23考试小记
    3.21考试小记
    3.20考试小记
    3.17考试小记
    3.15考试小记
    3.13考试小记
    3.12考试小记
    3.10考试小记
  • 原文地址:https://www.cnblogs.com/bin-zhao/p/11609374.html
Copyright © 2011-2022 走看看