简体   繁体   English

反射中方法的参数数量未知

[英]Unknown number of arguments of method in reflection

For example I have a class PartE such: 例如,我有一个类PartE如:

public class PartE implements Part
{
  protected String method = "getIndex" ;

  protected String getIndex ( double dummy,int dummyInt,String dummyString )
  {
    return "E" ;
  }
}

In this code I should invoke a method with unknown number of arguments. 在这段代码中,我应该调用一个参数数量未知的方法。 In this case it is getIndex(double,int,String) . 在这种情况下,它是getIndex(double,int,String)

It can be in this way: 它可以这样:

getIndex()
getIndex(int)
getIndex(boolean,int)
getIndex(int,double,boolean)

So whatever combination of arguments, I have done this but it seems that I am doing error in params[i].newInstance() because it returns not proper thing for instantiation. 所以无论参数的组合如何,我都已经这样做了,但似乎我在params[i].newInstance()做错误,因为它返回的不是实例化的东西。 That's why I am taking InstantiationException . 这就是我接受InstantiationException的原因。 I got stuck what can I do? 我被困了怎么办?

Class c1 = null;

try {
    c1 = Class.forName( "PartE" );
} catch (ClassNotFoundException ex) {}

Object obj = null;
obj = c1.newInstance();

Method [] methods = c1.getDeclaredMethods();

for(Method m:methods)
  {
      m.setAccessible(true);

      Class<?>[] params = m.getParameterTypes();
      Object[] paramObjects = new Object[params.length];

      for (int i = 0; i < params.length; i++) 
      {
          paramObjects[i] = params[i].newInstance();
      }

      try {
         System.out.println(m.invoke(obj, paramObjects));  
      } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {}

You need to handle the possibility that the class you need doesn't support a no-params constructor, since newInstance requires one but neither Integer nor Double has one. 你需要处理你需要的类不支持no-params构造函数的可能性,因为newInstance需要一个但是IntegerDouble都没有。 That means you'll need to add branching within your loop over the parameter types, to at least handle Integer and Double . 这意味着您需要在循环中添加参数类型的分支,以至少处理IntegerDouble Presumably the assignment tells you what values you should use (or that it doesn't matter what values you use) for those arguments, since you can't pass null (you'll get an unboxing error if you use null since the method expects int and double , not Integer and Double ) and they don't have a no-params constructor. 大概这个赋值告诉你应该使用哪些值(或者你使用的值无关紧要),因为你不能传递null (如果你使用null你会得到一个解包错误,因为方法需要intdouble ,不是IntegerDouble )并且它们没有no-params构造函数。

This answer is intentionally given as prose rather than code because the point of the assignment, presumably, is to learn to write that code. 这个答案是作为散文而不是代码故意给出的,因为分配的重点可能是学会编写代码。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用反射方法时参数数量未知 - Unknown number of arguments when invoking a reflection method 获取传递给具有未知数量参数的方法的参数数量 - Get the number of arguments passed to a method with an unknown number of arguments 在Java中创建具有未知数量和参数类型的方法 - Create a method with unknown number and type of arguments in java 如何实现未知数arguments的方法? - How to implement a method with an unknown number of arguments? Java反射API并调用具有可变数量参数的方法 - Java reflection API and invoking a method with variable number of arguments Java反射 - 参数数量错误; 预期0,得1 - Java Reflection - Wrong number of arguments; expected 0, got 1 反射newinstance构造函数java错误的参数数量 - reflection newinstance constructor java wrong number of arguments 如何在 Kotlin 中使用可变参数(参数数量)和反射 - How to work with varargs (number of arguments) and reflection in Kotlin 反射 - 获取具有未知数量的构造函数参数的未知类的新实例 - Reflection - Getting New Instance of unknown classes with unknown number of constructor parameters 如何使用反射来调用具有原始参数的方法? - How to use reflection to invoke a method with primitive arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM