vendredi 13 novembre 2020

How to pass an object to a method using reflection in java

I am working on some code which takes a string input dynamically , so i chose reflection as i didnt find any other better approach for it ,

My current code:

Class xyzClass = Class.forName("com.example.Xyz");

Object abcObj =  abcService.generateAbcObj("some string input");
Method method = xyzClass.getDeclaredMethod("add", abcObj); // i want to pass this abcObj to this declared method add but getDeclaredMethod takes class as a parameter

Can anybody suggest way to do in reflection as i want it in a dynamic way or any other way to achieve it in a better sense ?





Aucun commentaire:

Enregistrer un commentaire