リフレクション プロセスは、入力パラメータの呼び出しと、一般的なテンプレートのデモによって制御されます。

#反射中的method的invoke入参控制
#invoke(obj,obj[]);
#动态方法入参为不确定入参,参数可有,可无
private Method []methods;
private Object obj;
static{
    Class class=Class.forName("待获取的类路径");
    methods=class.getMethods();
    obj=class.newInstance();
}

public void methodByClass(Map context){

    context.get("method");//获取将要处理的方法名及入参如:getWorkDate()/replace(a,b,c);
    
    Object objs={}
    method =null;
    Object result=null;
    String methodName=context.get("method").split("//(")[0];
    //入参必须
  
    for(Method m:methods){
        if(m.equals(methodName)){
        method=m;   
        break;//杜绝资源浪费
        }
    }
    int num=method.getParametTyps().length;
    if(num==0){    
        result=method.invoke(obj)   
    
     }else{
        String []methodParms=ontext.get("method").split("//(")[1].split("//)").split(",");
        objs=new Object[num];
        for(int i=0;i<num;i++){
        objs[i]=context.get(methodParms[i]);
        
        
        }
        result=method.invoke(obj,objs);
    
    }


}

おすすめ

転載: blog.csdn.net/qq_37511875/article/details/108776057