vendredi 16 février 2018

How to invoke a method in java with .class (non-object) parameters using reflection

I recently had to use reflection to access some private methods and fields in a superclass. This is how it looked before I started using reflection:

            processSecondPassesOfType(PkDrivenByDefaultMapsIdSecondPass.class);
        processSecondPassesOfType(SetSimpleValueTypeSecondPass.class);
        processSecondPassesOfType(CopyIdentifierComponentSecondPass.class);
        processFkSecondPassInOrder();
        processSecondPassesOfType(CreateKeySecondPass.class);
        processSecondPassesOfType(SecondaryTableSecondPass.class);

Now I have to invoke the private method that I have acquired through reflection:

        Class<?> clazz = Class.forName("org.hibernate.cfg.Configuration");
        inSecondPass = true;
        Method m1 = clazz.getDeclaredMethod("processSecondPassesOfType", Class.class);
        m1.setAccessible(true);
        m1.invoke(PkDrivenByDefaultMapsIdSecondPass.class);

When I try to run this, I get the following error:

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

So how do I invoke a method with non-object parameters using reflection?





Aucun commentaire:

Enregistrer un commentaire