vendredi 20 septembre 2019

Java Reflection call to method with Generic parameters

How to call a method through a Reflection, when it has a generic parameter, like bellow snippet -

@Test
    public void testOptional() throws NoSuchMethodException, InvocationTargetException,
            IllegalAccessException
    {
        AtomicReference<ClassA> atomicReference = new AtomicReference<>(new ClassA());
        ClassB classB = new ClassB();

        Method method = MyTest.class.getDeclaredMethod("doSomething", AtomicReference.class, ClassB.class);
        method.setAccessible(true);
        method.invoke(atomicReference, classB);
    }

    private void doSomething(AtomicReference<ClassA> classA, ClassB classB){

        System.out.println("Hi do not poke me, I am working!");
    }

It gives me -

java.lang.IllegalArgumentException: object is not an instance of declaring class





Aucun commentaire:

Enregistrer un commentaire