Java reflection mechanism-Analysis

Source: Internet
Author: User

1. The so-called reflection mechanism defined by the java reflection mechanism is that the java language has a self-view capability during runtime. With this capability, You can thoroughly understand your situation and prepare for the next step. For example, you can use it to obtain the names of the members in the Java class and display them. 2. The implementation of the reflection mechanism of the key java classes requires the use of four classes: class, Constructor, field, Method; class -- class object, Constructor -- class Constructor object, Field -- class attribute object, and Method -- class Method object. Through these four objects, we can roughly see each group part of a class. 3. java reflection mechanism Example 1) class-Class objects use java. lang. class classes to describe classes and interfaces. [Html] package com. centit. reflect; import java. lang. reflect. method;/*** @ author zjh **/class Person {private String name; private int age; public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public Person (String name, int age) {super (); this. name = name; this. age = age ;}public Person () {super () ;}} public class DumpMethods {public static void main (String [] args) {try {// define a Person Instance Object Person man = new Person ("test", 30); // method for obtaining a Class object [html] Class manClass = man. getClass (); [html] // method for obtaining a Class Object Class womanClass = Class. forName ("com. centit. reflect. person "); [html] // method for getting a Class Object Class boyClass = Person. class; System. out. println (manClass. getName (); System. out. println (womanClass. getName (); System. out. println (boyClass. getName ();} catch (Throwable e) {System. err. println (e) ;}} output result: [html] com. centit. reflect. person com. centit. reflect. person com. centit. reflect. person [html] 2) Constructor -- Constructor object of the class [html] [html] public class DumpMethods {public static void main (String [] args) {try {// define a Person Instance Object Person man = new Person ("test", 30); [html] Class manClass = man. getClass (); // obtain the Constructor manCon [] = manClass. getDeclaredConstructors (); for (int I = 0; I <manCon. length; I ++) {Constructor con = manCon [I]; System. out. println ("name =" + con. getName (); System. out. println ("decl class =========" + con. getDeclaringClass (); Class param [] = con. getParameterTypes (); for (int j = 0; j <param. length; j ++) {System. out. println ("param ------" + param [j]) ;}} catch (Throwable e) {System. err. println (e) ;}} output result: name ====== com. centit. reflect. persondecl class ========= class com. centit. reflect. personparam ------ class java. lang. stringparam ------ intname ======= com. centit. reflect. persondecl class ========= class com. centit. reflect. person3) Field -- attribute object of the class [html] public class DumpMethods {public static void main (String [] args) {try {// define a Person Instance Object Person man = new Person ("test", 30); Class manClass = man. getClass (); Field manField [] = manClass. getDeclaredFields (); for (int I = 0; I <manField. length; I ++) {Field fie = manField [I]; System. out. println ("name =" + fie. getName (); System. out. println ("type =" + fie. getType () ;}} catch (Throwable e) {System. err. println (e) ;}}4) Method -- Method object of the class [html] public class DumpMethods {public static void main (String [] args) {try {// define a Person Instance Object Person man = new Person ("test", 30); Class manClass = man. getClass (); Method manMethod [] = manClass. getDeclaredMethods (); for (int I = 0; I <manMethod. length; I ++) {Method m = manMethod [I]; System. out. println ("name =" + m. getName (); System. out. println ("decl class =" + m. getDeclaringClass (); Class pvec [] = m. getParameterTypes (); for (int j = 0; j <pvec. length; j ++) System. out. println ("param #" + j + "" + pvec [j]); Class evec [] = m. getExceptionTypes (); for (int j = 0; j <evec. length; j ++) System. out. println ("exc #" + j + "" + evec [j]); System. out. println ("return type =" + m. getReturnType (); System. out. println ("-----") ;}} catch (Throwable e) {System. err. println (e) ;}} output result: name = getNamedecl class = class com. centit. reflect. personreturn type = class java. lang. string ----- name = setNamedecl class = class com. centit. reflect. personparam #0 class java. lang. stringreturn type = void ----- name = getAgedecl class = class com. centit. reflect. personreturn type = int ----- name = setAgedecl class = class com. centit. reflect. personparam #0 intreturn type = void -----

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.