将不同的变量传递给反射getMethod并调用所述方法

问题描述

我目前有一个笨拙的方法,它将采用包含方法名称和此类参数的字符串数组

String []wf = {"sobel(10,1)","sobel2(2,2)","sobelMax(sobel,100,10)"};

这告诉它要调用什么方法以及要传递的参数。但是,如您所见,并非所有方法都使用相同数量的参数,也不是所有参数类型都相同,因此有些浮点型的类型为int,有些为PImage类型。

当前,此参数传递给此函数,该函数修剪字符串并提取有用的信息。然后通过开关盒传递参数长度,以最好地处理下一部分。

void workflow(String[] a){
String[] s = a;

for(int i=0;i<s.length;i++){
  String s1 = s[i];
  
  int [] pIndex = strIndex1(s1,"(",")");
  String function = s1.substring(0,pIndex[0]);
  
  String[]parameters = splitTokens(s[i].substring(pIndex[0]+1,pIndex[1]),",");
  print("p",function,"(");
  for(int j=0;j<parameters.length;j++){
     print(parameters[j]);
     if(j<parameters.length-1)print(",");
  }
  println(")");
  
  switch(parameters.length){
    
    case 1: func1(function,int(parameters[0]));
            println("c1");
            break;
    case 2: if(float(parameters[0])>-1000000&&float(parameters[0])<100000000){
              func2(function,float(parameters[0]),int(parameters[1]));
              println("c2");
            }else {
               Field field = null;
              
               try{
               field = this.getClass().getField(parameters[0]);
               func2(function,field,int(parameters[1]));
               }catch (NullPointerException e){
                println("np c2");
               }catch (NoSuchFieldException e) {
                 println("nsf c3");
               }
            }
            break;
    case 3: 
            if(float(parameters[0])>-1000000&&float(parameters[0])<100000000){
              func3(function,float(parameters[1]),int(parameters[2]));
              println("c2");
            }else {
               Field field = null;
              
               try{
               field = this.getClass().getField(parameters[0]);
               //Object.value = field.
               PImage p = (PImage)field.get(this);
               func3(function,p,int(parameters[2]));
               }catch (NullPointerException e){
                println("np c3");
               }catch (NoSuchFieldException e) {
                 println("nsf c3");
               }catch (IllegalAccessException e) {
                 println("ia c3");
               }
               
            }
    
    println("c3");
    break;
    //func3(function,parameters);
    //case 3: func4(function,parameters);
    
  }
}
  

};

最后,switch语句将方法和参数相应地路由到一个函数,该函数将能够匹配类中的正确方法。

void func1(String function,int p){
println(function,p);
Method method = null;
  try {
    method = this.getClass().getMethod(function,int  .class);
    println(method);
    method.invoke(this,p);
    //println("result",result);
  } catch (SecurityException e) {
    println(function,"se f1");
  }catch (NoSuchMethodException e) {  
    println(function,"nsm f1");
  }
  catch (IllegalAccessException e) {  
    println(function,"ia f1");
  }
  catch (InvocationTargetException e) {  
    println(function,"it f1");
  }

};

有没有一种方法可以压缩这段代码,并且可以取消switch case语句,因为它看起来非常笨拙,并且仅在更多参数时才增加大小。

非常感谢

解决方法

尝试将switch替换为

Class<?>[] parameterClasses = new Class<?>[parameters.length];
Object[] parsedParameters = new Object[parameters.length];
for (int j = 0; j < parameters.length; j++) {
    parameterClasses[j] = getParameterClass(parameters[j]);
    parsedParameters[j] = parseParameter(parameters[j]);
}
try {
    Method method = this.getClass().getMethod(function,parameterClasses);
    method.invoke(this,parsedParameters);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
    throw new RuntimeException(e);
}

其中

Object parseParameter(String parameter) {
    try {
        return Integer.parseInt(parameter);
    } catch(NumberFormatException e) {
        try {
            return Float.parseFloat(parameter);
        } catch(NumberFormatException e1) {
            try {
                Field field = this.getClass().getField(parameter);
                return field.get(this);
            } catch (NoSuchFieldException | IllegalAccessException e2) {
                throw new RuntimeException(e2);
            }
        }
    }
}

Class<?> getParameterClass(String parameter) {
    try {
        Integer.parseInt(parameter);
        return int.class;
    } catch(NumberFormatException e) {
        try {
            Float.parseFloat(parameter);
            return float.class;
        } catch(NumberFormatException e1) {
            return PImage.class;
        }
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...