java - Get instanced object by String -
is possible object instanced in code string @ runtime?
somthing that:
public string xyz = "aaaa_bbb"; getobject("xyz").some function of string (e.g.: .split("_"))
thanks
here's example
if it's class field, can name this.
import java.lang.reflect.method; public class test { public string stringinstance = "first;second"; public void foo() { try { object instance = getclass().getdeclaredfield("stringinstance").get(this); method m = instance.getclass().getmethod("split", string.class); object returnvalue = m.invoke(instance, ";"); if(returnvalue instanceof string[]) { for(string s : (string[])returnvalue ) { system.out.println(s); } } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } public static void main(string a[]){ new test().foo(); } }
if it's local method variable trying invoke on, might able @ variable in current method call stack thread.currentthread().getstacktrace()
maybe.
Comments
Post a Comment