java - Reflection finding method with Generic Type withing Parameterized Class -
i have class following signature:
public class multiplesorting<t extends enum<?>> { private class<t> criteriatype; private t selectedcriteria; public multiplesorting(class<t> criteriatype) { super(); this.criteriatype = criteriatype; } public void setselectedcriteria(t selectedcriteria) { this.selectedcriteria = selectedcriteria; } public t getselectedcriteria() { return selectedcriteria; } public class<t> getcriteriatype(){ return criteriatype; } public void setcriteriatype(class<t> criteriatype){ this.criteriatype = criteriatype; } }
i instantiating new multiplesorting(articlesortfield.class);
. here articlesortfield
enum
. method within multiplesorting
when trying method setselectedcriteria
through reflection as:
method setselectedcriteriamethod = getclass().getdeclaredmethod("setselectedcriteria",getcriteriatype());
the setselectedcriteriamethod
returning null
. after debugging found getcriteriatype()
returning class articledortfield
argument type of setselectedcriteria
method enum
, why reflection returning null
.
also if change signature of multiplesorting
public class multiplesorting<t>
t
of setselectedcriteria
becoming object
.
- how can fix issue?
- is happening due type erasure? thought
t
replacedarticledortfield
. couldn't understand why behaving this. how type erasure works? replace generic type concrete implementation?
any pointer helpful me.
a possible fix issue might "setselectedcriteria" method
calling getclass().getdeclaredmethod(...)
desired type, super type, , walking class hierarchy until found matching method. it's not beautiful solution might trick.
and yes, caused type erasure. once code compiled, there no <t>
object
, there no <t extends enum<?>>
enum
. type safety checked compiler, not enforced jvm.
Comments
Post a Comment