InvokeExact on the object, whose type is dynamically loaded by classloader

前端 未结 2 1464
萌比男神i
萌比男神i 2021-01-20 16:56

I have spend whole day on this problem. My problem is how to make an MethodHandle.invokeExact invocation on an instance, whose class type is dynamically loaded at program ru

2条回答
  •  面向向阳花
    2021-01-20 17:45

    To make sense, you'll have to type the method ( otherwise the cast is pointless):

    public  void doSomething() {
    
    BaseTemplate obj = ...
    Class newType = Class('AddSample');
    T t = newType.cast(obj);
    

    Without the method typing, you can't tie the dynamic class to the type to be cast to.

提交回复
热议问题