Java generic return type (with similar input parameters) -
this questions close this, there major difference.
may requirements:
(1) want generate java function generic return value.
(2) list of input parameters same. (unlike in link above).
(3) function shall know, type of return parameter expected.
my tries:
public <t> t getproperty(string name) { t value; try { if (t instanceof string) {value = (t) getstringproperty(name);} if (t instanceof long) {value = (t) getlongproperty(name);} if (t instanceof boolean) {value = (t) getbooleanproperty(name);} } catch (exception e) { logger.error("error @ getproperty", e); } return value; }
this not working, since no instance of t
generated. trys t value = new t();
failed.
any quick fixes? or approach not recommended?
what want not possible due type erasure of generics @ runtime. in other words <t>
erased object
in case.
you can pass class-object describe return type:
public <t> t getproperty(class<t> returntype,string name){ if (returntype == string.class) return (t)stringproperty(name); [...] }
Comments
Post a Comment