Domanda

I am trying to use mirroring to call a method s1 of my class MyClass, with the parameter s2. Java is complaining that String.TYPE does not exist. I checked the API and it is right: I cannot do the same thing as when I call Integer.TYPE. But how can I solve the problem then? I need partype of type String, or else the method throws an exception.

public void trying(MyClass method, String s1, String s2){
   try {
       Class cls = Class.forName("MyClass");
       Class partype[] = new Class[1];
       partype[0] = String.TYPE;
       Method meth = cls.getMethod(s1, partype);
       meth.invoke(methobj, s2);
   }
   catch (Throwable e) {
        System.err.println(e);
   }
}
È stato utile?

Soluzione

It's not a type, it's a class:

partype[0] = String.class;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top