Android updates are too fast. Because of its open source, it has created a number of technical companies and a bunch of solutions, which are distinctive. This is lucky for a developer who wants to gain insight into the system, but it is fatal from a product perspective. We develop applications. To ensure program compatibility, we have bought many devices for compatibility testing, but it is often unsatisfactory. I am not going to solve this problem here, but I am nagging. This article only introduces a simple technique for compatibility.
Background: A method at the framework layer adds a parameter or missing a few parameters in the new version. To ensure program compatibility, this problem must be solved. Otherwise, there will be many more versions! What should I do? There are some ways to use reflection technology in Java! Only a simple demo program is written here. For details about what reflection technology is and how to use reflection, please refer to this blog post on Java reflection-rollen Holt-blog
It's very simple.
public class Demo {public void setABC(int id,String name){}public void setDEF(int id,String name,String pwd){System.out.println("id="+id+",name="+name+";pwd="+pwd);}}
Package Org. winplus. java; import Java. lang. reflect. invocationtargetexception; import Java. lang. reflect. method; public class test {public static void main (string [] ARGs) {}/ *** get all methods in the class */Private Static void getallmethods () {class clz = demo. class; method [] method = clz. getdeclaredmethods (); For (method M: Method) {class rettype = m. getreturntype (); Class [] paramtypes = m. getparametertypes (); For (Int J = 0; j <paramtypes. length; j ++) {If (j> 0) system. out. print (","); system. out. print (paramtypes [J]. getname () ;}}/ *** get the specified method and call the Method */Private Static void exemethod () {class clz = demo. class; class [] clzs = new class [3]; clzs [0] = int. class; clzs [1] = string. class; clzs [2] = string. class; try {method MTH = clz. getdeclaredmethod ("setdef", clzs); MTH. invoke (new demo (), 12, "Tang", "Haha"); system. out. println (MTH. getname ();} catch (securityexception e) {e. printstacktrace ();} catch (nosuchmethodexception e) {e. printstacktrace ();} catch (illegalargumentexception e) {e. printstacktrace ();} catch (illegalaccessexception e) {e. printstacktrace ();} catch (invocationtargetexception e) {e. printstacktrace ();}}}
Original article, reproduced please indicate the source: http://blog.csdn.net/tangcheng_ OK