java - How to get the parameter names of an object's constructors (reflection)? -


this question has answer here:

say somehow got object reference other class:

object myobj = anobject; 

now can class of object:

class objclass = myobj.getclass(); 

now, can constructors of class:

constructor[] constructors = objclass.getconstructors(); 

now, can loop every constructor:

if (constructors.length > 0) {     (int = 0; < constructors.length; i++)     {         system.out.println(constructors[i]);     } } 

this giving me summary of constructor, example constructor public test(string paramname) shown public test(java.lang.string)

instead of giving me class type however, want name of parameter.. in case "paramname". how that? tried following without success:

if (constructors.length > 0)     {         (int icon = 0; icon < constructors.length; icon++)         {             class[] params = constructors[icon].getparametertypes();             if (params.length > 0)             {                 (int ipar = 0; ipar < params.length; ipar++)                 {                     field fields[] = params[ipar].getdeclaredfields();                     (int ifields = 0; ifields < fields.length; ifields++)                     {                         string fieldname = fields[i].getname();                         system.out.println(fieldname);                     }                                                        }             }         }     } 

unfortunately, not giving me expected result. tell me how should or doing wrong? thanks!

this information lost after compilation , can't retrieved @ runtime.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -