java - Why does reflection return two methods, when there is only one implementation? -
suppose have code:
public interface address { public int getno(); } public interface user<t extends address> { public string getusername(); public t getaddress(); } public class addressimpl implements address { private int no; public int getno() { return no; } public void setno(int no) { this.no = no; } } public class userimpl implements user<addressimpl> { private string username; private addressimpl addressimpl; public string getusername() { return username; } public void setusername(string username) { this.username = username; } public addressimpl getaddress() { return addressimpl; } public void setaddress(addressimpl addressimpl) { this.addressimpl = addressimpl; } } running code:
int getaddressmethodcount = 0; (method method : userimpl.class.getmethods()) { if (method.getname().startswith("getaddress")) { getaddressmethodcount++; } } would yield 2 getaddressmethodcount variable; why so?
it's way covariant return types implemented. javap -private show more conveniently reflection.
the subclass have synthetic bridge method handles forwarding more specific method. far jvm concerned methods have name, sequence of raw typed parameters , raw type return. can overload on return type in bytecode.
a system.err.println(mehtod.getreturntype()); should give different results 2 methods.
Comments
Post a Comment