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

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -