All about how to use reflection in Java (reflection)

Source: Internet
Author: User

Source: http://www.java-tips.org/java-se-tips/java.lang.reflect/how-to-use-reflection-in-java.html

Reflection is a powerful method for analyzing classes at runtime. For example, when a new class is dynamically added to your application at runtime, in this case, you can use the reflection mechanism to obtain the structure information of this class. We use a special class to implement reflection: class .. Class objects hold all information about the class and can use the gette method to extract the information we need. The following example extracts and outputs the structure information of the string class on the console, and displays the name, attribute, and method of the constructor.

Import Java. lang. reflect. constructor; import Java. lang. reflect. field; import Java. lang. reflect. method; public class reflectionexample {public static void main (string [] ARGs) {try {// creates an object of type class which contains the information of creates a class object containing string class information. // The class string class Cl = Class. forname ("Java. lang. string "); // getdeclaredfields () returns all the constructors of the class. // The getdeclaredfields () method returns information about all constructor cnst [] = Cl. getconstructors (); // getfields () returns all the declared fields of the class. // The getfields () method returns all attribute information defined in the class field attributes [] = Cl. getdeclaredfields (); // getmethods () returns all the declared methods of the class. // The getmethods () method returns all the method information defined in the class. Method MTD [] = Cl. getmethods (); system. out. println ("Name of the constructors of the string class"); For (INT I = 0; I <cnst. length; I ++) {system. out. println (cnst [I]. getname ();} system. out. println ("Name of the declared fields"); For (INT I = 0; I <random. length; I ++) {system. out. println (parts [I]. getname ();} system. out. println ("Name of the methods"); For (INT I = 0; I <MTD. length; I ++) {system. out. println (MTD [I]. getname () ;}} catch (classnotfoundexception e) {e. printstacktrace ();}}

Result:

Name of the constructors of the string class/string class constructor name
Java. Lang. String

......

Name of the declared fields/attribute name
Value
Offset
Count
Hash
Serialversionuid

......

Name of the methods/method name
Hashcode
Equals
Compareto
Compareto
Indexof
Indexof
Indexof
Indexof

......

 

Source 2: http://www.java-tips.org/java-se-tips/java.lang.reflect/invoke-method-using-reflection.html

Reflection can be used to dynamically execute a method at runtime (that is, the method name is provided or determined only at runtime). below is the sample code for Using Reflection to implement method call:

Import Java. lang. reflect. method; public class runmthdref {public int add (int A, int B) {return a + B;} public int sub (int A, int B) {return a-B ;} public int MUL (int A, int B) {return a * B;} public int Div (int A, int B) {return a/B ;} public static void main (string [] ARGs) {try {INTEGER [] input = {New INTEGER (2), new INTEGER (6)}; Class Cl = Class. forname ("runmthdref"); // enter the full name of the class, for example, Java. lang. string class [] par = new class [2]; par [0] = integer. type; par [1] = integer. type; Method mthd = Cl. getmethod ("add", par); integer output = (integer) mthd. invoke (New runmthdref (), input); system. out. println (output. intvalue ();} catch (exception e) {e. printstacktrace ();}}}

 

Output: 8

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.