vendredi 26 avril 2019

Init Generic Class Using Reflection Java

I am currently automating a process of a data handler. What I mean with automating is that the DataHandlers are initiated automatically via reflection based on a specific configuration.

But I am on a point where I do not know how I should solve this problem. It is about this peace of code:

Class<?> clazz = classLoader.loadClass(d);
Object values = clazz.getMethod("values").invoke(null);
RSA.addDataHandler(clazz,new DataHandlerSetAdapter<>(values));

I am loading the class via the classLoader and invoking a static method called "values" which returned a bunch of, you have suggested right, values!

Now the problem is with new DataHandlerSetAdapter<>(values)) which has an error in <> because the Object does not know which class to initiate.

Cannot infer arguments (unable to resolve constructor)

Would this code be in C# I would use typeof() but in JAVA there is nothing like this maybe instanceof but I cannot use this at this point. Btw .values() is returning an Array of the same type class (enum).

How can I solve this problem?

//edit

with the method .getReturnType() on the Method I would get the return type but I cannot apply it to the DataHandlerSetAdapter<>





Aucun commentaire:

Enregistrer un commentaire