[Android Application Development]-(21) Android uses reflection to solve program compatibility problems

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.