reflection - Java Indirect Access to Variables by Name at Runtime? -
this question has multiple parts, , make sense in last part:
is possible programmatically list programmer-defined variables in java class @ runtime..?
i wrote utility procedure in vb.net lists forms in project name, can pick 1 run listbox (just development purposes). elsewhere on stackoverflow, able pin following bit of code together, , example pinned more illustrative purposes...
system.reflection.assembly.getexecutingassembly().definedtypes(n).basetype.fullnamecan similar done in java list variable names..? being myinteger, mystring, or myobject, example. it'd function behind "locals" window in visual studio (but i'm using eclipse java).
somewhere in past 20 years, in programming language, came across function called indirect(string varname). used access primitive variable name. , 'varname' parameter variable; didn't have literal. here's example showing use:
int n1, n2, n3; // n1, n2, & n3 0 (int = 1; < 4; i++){ indirect("n" + i.tostring) = 100 } // n1, n2, & n3 100does java have equivalent indirect(), or there way duplicate same functionality..?
to glue together, , reason entire forum post, have following procedure i've written debugging purposes, drop classes output variable values in csv format during development...
public string listvars(){ object input[] = {var1, var2, var3, var4, var5}; // list of vars string output = ""; // output catcher int varcount = input.length; // length of array (int = 0; < varcount; i++){ // loop through array output += "\"" + input[i] + "\""; // surround quotes, append output if (i < varcount-1){ // if item not last... output += ","; // append comma } } return output; // return completed output }what automate input object array code automatically enumerates variables, don't have copy & paste them function. , i'm assuming in doing, need referred indirectly once names determined, values can accessed in code.
ps - filtering variables scope nice, such 'private", 'super', <classname>, 'public', etc, know may stretch bit far. =-)
- listing class fields/methods @ runtime
yes, can that, reflection. see tutorial here:
for (field f : getclass().getdeclaredfields()) { system.out.println("found " + f); } - "indirect" access
yes, using above mechanism find field wish altar can set value
field f = ... //get somewhere f.set(this, "new value"); what want achieve sounds close java bean concept. gist of keep getx()/setx() naming convention getter , setter methods, each such pair representing property called "x" , can utilize bean-oriented libraries, such apache commons beanutils all heavy lifting (that told above) you, so:
employee employee = ...; string firstname = (string)propertyutils.getsimpleproperty(employee, "firstname"); string lastname = (string)propertyutils.getsimpleproperty(employee, "lastname"); ... manipulate values ... propertyutils.setsimpleproperty(employee, "firstname", firstname); propertyutils.setsimpleproperty(employee, "lastname", lastname);
Comments
Post a Comment