1

I have some demo code as follwing.

  Class<?> myClass = cl.loadClass("com.hp.ac.scriptengine.test." + generateClassName);
  Object my_obj = myClass.newInstance();
  MethodType mt; 
  MethodHandle mh;
  MethodHandles.Lookup lookup = MethodHandles.lookup();            
  mt = MethodType.methodType(void.class, int.class);
  mh = lookup.findVirtual(my_obj.getClass(), "ToDoit", mt);
  mh.invokeExact(my_obj,1);

here '"com.hp.ac.scriptengine.test." + generateClassName' is a generated class. I got the message as follows.

java.lang.invoke.WrongMethodTypeException: (I)V cannot be called as (Ljava/lang/Object;I)V
                 at com.hp.ac.scriptengine.test.compliebyCommandline.main(compliebyCommandline.java:138)

Here line 138 is mh.invokeExact(my_obj,1);'

I tried that demo code(such as ... mh.invokeExact("daddy",'d','n')...) in Java 7 API document. It works fine. Such call(mh.invokeExact("daddy",'d','n'))just invoke (CC)Ljava/lang/String other than (Ljava/lang/String;CC)Ljava/lang/String. But why, in my code, mh.invokeExact(my_obj,1) invoke (Ljava/lang/Object;I)V other than (I)V ?

Igor
  • 33,276
  • 14
  • 79
  • 112
Steven Guan
  • 43
  • 1
  • 5
  • 1
    For one thing, you need to cast my_obj to your generated class type (or a subtype) -- the lack of a cast is why invokeExact uses Ljava/lang/Object; as the first argument type. But I'm not sure why the method handle claims to be (I)V despite being virtual. – Jeffrey Bosboom Jun 01 '14 at 13:54

1 Answers1

-1

I think the problem is with int.class. Try Integeer.class or Integer.TYPE instead.

alexsmail
  • 5,661
  • 7
  • 37
  • 57