Java reflection mechanism reflect analyzes Object, reflectobject

Source: Internet
Author: User

Java reflection mechanism reflect analyzes Object, reflectobject

Let's look at an instance.

</Pre> <pre name = "code" class = "java"> package reflect; import java. lang. reflect. constructor; import java. lang. reflect. field; import java. lang. reflect. method; import java. lang. reflect. modifier; class Behavior {private long distance; private long amount; public long getDistance () {return distance;} public void setDistance (long distance) {this. distance = distance;} public long getAmount () {return amount;} public Void setAmount (long amount) {this. amount = amount;} public String walk () {return "taken" + distance + "km";} public String eat () {return "eat" + amount + "bowl of rice" ;}} class Person extends Behavior {private String username; private int age; static char sex; protected final double high = 0; public String [] holobby; public Behavior be; public String getUsername () {return username;} public void setUsername (String username) {This. username = username;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public char getSex () {return sex;} public static void setSex (char sex) {Person. sex = sex;} public double getHigh () {return high;} public String [] getholobby () {return holobby;} public void setholobby (String [] holobby) {this. holobby = holobby;} public Behavior getBe () {return be;} public void setBe (Behavior be) {This. be = be;} public Person () {} public Person (String username, int age, String [] Hober, Behavior be) {super (); this. username = username; this. age = age; this. holobby = holobby; this. be = be;} public String fun2 (int a, int B, int c, String d, double e, float f, char g) {return a + B + c + d + e + f + g;} public class ObjectAnalyzer {public void printFields (Field [] fields) {for (int I = 0; I <fields. lengt H; I ++) {Field f = fields [I]; int m = f. getModifiers (); String name = f. getName (); Class <?> Clazz = f. getType (); System. out. println (Modifier. toString (m) + "" + clazz. getName () + "" + name) ;}} public void printConstructor (Constructor <?> [] C) {for (int I = 0; I <c. length; I ++) {Constructor <?> Con = c [I]; String name = con. getName (); int m = con. getModifiers (); Class <?> [] Clazz = con. getParameterTypes (); System. out. print (Modifier. toString (m) + "" + name + "("); for (int j = 0; j <clazz. length; j ++) {System. out. print (clazz [j]. getName () + "");} System. out. println (")") ;}} public void printMethod (Method [] methods) {for (int I = 0; I <methods. length; I ++) {Method m = methods [I]; int modi = m. getModifiers (); String name = m. getName (); Class <?> ReturnType = m. getReturnType (); Class <?> [] ParamType = m. getParameterTypes (); System. out. print (Modifier. toString (modi) + "" + returnType. getName () + "" + name + "("); for (int j = 0; j <paramType. length; j ++) {System. out. print (paramType [j]. getName () + "");} System. out. println (")") ;}} public void analyzer (Object obj) {Class <?> Clazz = obj. getClass (); Field [] fields = clazz. getDeclaredFields (); Constructor <?> [] C = clazz. getDeclaredConstructors (); Method [] method = clazz. getDeclaredMethods (); // Method [] method = clazz. getMethods (); System. out. println ("===== Analysis of attribute variables ===="); System. out. println ("Fields length:" + fields. length + "variable as follows"); printFields (fields); System. out. println ("===== Constructor (c); System. out. println ("===== method Analysis ===="); printMethod (method);} public static void main (String [] args) {// TODO Auto-generated method stubBehavior be = new Behavior (); Person p = new Person ("chiwei", 26, new String [] {"1 ", "2"}, be); new ObjectAnalyzer (). analyzer (p );}}

The running result is as follows:

===== Analysis of attribute variables ====
Fields length: 6 variables:
Private java. lang. String username
Private int age
Static char sex
Protected final double high
Public [Ljava. lang. String; holobby
Public reflect. Behavior be
===== Constructor analysis =====
Public reflect. Person ()
Public reflect. Person (java. lang. String int [Ljava. lang. String; reflect. Behavior)
===== Method Analysis =====
Public void setUsername (java. lang. String)
Public int getAge ()
Public void setAge (int)
Public char getSex ()
Public static void setSex (char)
Public double getHigh ()
Public [Ljava. lang. String; getholobby ()
Public void setholobby ([Ljava. lang. String ;)
Public reflect. Behavior getBe ()
Public void setBe (reflect. Behavior)
Public java. lang. String fun2 (int java. lang. String double float char)
Public java. lang. String getUsername ()







Java Reflection (JAVA Reflection) Mechanism

I started to score again. Is it interesting?

Java reflection mechanism

During Java runtime, can I know the attribute methods of a class and call and modify them? Can an object know its class and call its method? The answer is yes. This kind of dynamic mechanism for obtaining information and calling methods is called "reflection" in Java ).
The Java reflection mechanism mainly provides the following functions:
Determine the class to which any object belongs at runtime;
Construct any class object at runtime;
Judge the member variables and methods of any class at runtime;
Call the methods of any object at runtime.
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.
Generally speaking, when talking about dynamic languages, the developer community roughly agrees with the definition: "When a program is running, the program structure or variable type can be changed. This language is called dynamic language ".
In JDK, the following classes are used to implement the Java reflection mechanism. These classes are located in the java. lang. reflect package:
Class: represents a Class;
Field Class: represents the member variables of the class (member variables are also called the attributes of the class );
Method class: indicates the Method of the class;
Constructor class: Constructor class;
Array class: provides static methods for dynamically creating arrays and accessing Array elements;

For more information, see references. I think this document is good.
Reference: 2.16.hi.baidu.com/#/detail/24992875

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.