FranXh
FranXh

Reputation: 4771

Mirroring a method with string parameter

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);
   }
}

Upvotes: 1

Views: 133

Answers (1)

MByD
MByD

Reputation: 137382

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

partype[0] = String.class;

Upvotes: 8

Related Questions