Java reflection and generics - parameterized type info missing "extends" -
i reconstitute signature of method using reflection in java 8. i've run issues generic methods. in particular, find nothing recover information cases of "extends" or "super" in template type declaration.
here's specific test case, based on known source code:
signature of method source code:
public static <t extends assertdelegatetarget> t assertthat(t assertion)
first of all, there elegant way reconstitute <t extends assertdelegatetarget>
part without having parse strings?
here's main info able reflection:
method.tostring()
:public static org.assertj.core.api.assertdelegatetarget org.assertj.core.api.assertions.assertthat(org.assertj.core.api.assertdelegatetarget)
method.togenericstring()
:public static <t> t org.assertj.core.api.assertions.assertthat(t)
what happened "extends assertdelegatetarget"?
method.getreturntype.tostring()
:interface org.assertj.core.api.assertdelegatetarget
method.getgenericparametertypes()[0].gettypename()
:t
based on above reflection info, assume relationship between t , assertdelegatetarget, how know whether it's "extends" or "super"?
you have method
object already. starting here, type parameters declared on method the gettypeparameters
method, returns typevariable<method>[]
. array of type parameter declared on method. in case has 1 type parameter, returned array has 1 element.
the typevariable
interface has method retrieve name t
, getname
, , method retrieve bounds, getbounds
. getbounds
method returns type[]
. can sure bounds here upper bounds, because declarations of type variables aren't allowed use super
, extends
.
in case, type
variable class
object assertdelegatetarget
.
now can re-construct "<t extends assertdelegatetarget>"
string above information. use "extends" if type[]
non-empty. if there multiple bounds, place comma in between them, e.g. t extends bound1, bound2
.
Comments
Post a Comment