Method call and filed settings for Java reflection

Source: Internet
Author: User

Reflection is an advanced feature of Java.

Reflection is a key feature of Java as a dynamic (or quasi-dynamic) language. This mechanism allows the program to obtain the internal information of any class with a known name through reflection APIs at runtime, including its modifiers (such as public and static), superclass (such as object), implements interfaces (such as serializable), also includes all information about fields and methods, and can change fields content or call methods at runtime.

Reflection is a path for the application to check itself and the runtime environment. Replacement of your own information-metadata.

 

At the entry level, Let's first look at how to call the method through reflection and set the value of the member variable:

1. Set variable values through reflection

 

Import Java. lang. reflect. field; <br/>/** <br/> * Modify Field reflection <br/> * @ author yblin <br/> */<br/> public class reffield {<br/> Public String color; <br/> Public double number; <br/> Public static void main (string ARGs []) {<br/> try {<br/> Class C = reffield. class; <br/> Field colorfield; <br/> colorfield = C. getfield ("color"); <br/> Field numberfield = C. getfield ("Number"); <br/> reffield OBJ = new reffield (); <br/> colorfield. set (OBJ, "blue"); <br/> system. out. println ("color =" + colorfield. get (OBJ); <br/> numberfield. set (OBJ, 2.1); <br/> system. out. println ("number =" + numberfield. get (OBJ); <br/>} catch (securityexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (nosuchfieldexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (illegalargumentexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (illegalaccessexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/>

 

2. Call the reflection call Method

Import java. Lang. Reflect. invocationtargetexception; <br/> Import java. Lang. Reflect. method; <br/>/** <br/> * reflection method call. <Br/> * @ author yblin <br/> */<br/> public class refmethod {<br/> private string color; <br/> Public void printcolor () {<br/> system. out. println ("color is:" + color); <br/>}< br/> Public refmethod (string color) {<br/> This. color = color; <br/>}< br/> Public void sayhello (string name, integer age) {<br/> system. out. println ("Hello World," + name + "! "+" The age is: "+ age); <br/>}< br/> Public static void main (string ARGs []) {<br/> class CLS = refmethod. class; <br/> refmethod ref = new refmethod ("blue"); <br/> try {<br/> method sayhello = CLS <br/>. getmethod ("sayhello", new class [] {string. class, integer. class}); // find the sayhello method with the string and integer parameters. <Br/> method printcolor = Cls. getmethod ("printcolor", null); <br/> try {<br/> sayhello. invoke (ref, new object [] {"ant", 3}); // call the method using the invoke of the mechod object <br/> printcolor. invoke (ref, null); <br/>} catch (illegalargumentexception e) {// invalid parameter <br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (illegalaccessexception e) {// No access <br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (invocationtargetexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/>} catch (securityexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (nosuchmethodexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/>

 

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.