Java Reflection mechanism (not completed)

Source: Internet
Author: User

1. Background

1) Reflection that is, reflection is a key property of Java as a dynamic (or quasi-dynamic) language.

2) Reflection mechanism refers to the program at runtime to obtain all the internal information of any class

2. Overview of Implementation functions

1) All information about a class can be obtained by reflection as long as the full name of the class is given.

2) reflection can get the class object to which any object belongs while the program is running.

3) At run time, you can get all the property objects in the class and manipulate them (including private properties).

4) At run time you can get all the methods in the parent class to the class, and call.

5) The core of the current mainstream application framework, such as STRUTS2,HIBERNATE,SPRING,SPRINGMVC, is implemented using the reflection mechanism of java.

The mechanism and implementation of class object

1. Class Object Overview

1) class is actually the type of classes

2) string types are strings, and the type of shaping is integer,string and the integer type is class

2. Introduction to common methods of class objects

Newinstance () instantiating an object

3. Three ways to instantiate an object

JavaBean

public class book {private int  id;private String name;public String type;/** *  @return  the id  */public int getid ()  {return id;} /** *  @param  id the id to set */public void setid (int  ID)  {this.id = id;} /** *  @return  the name */public string getname ()  {return name;} /** *  @param  name the name to set */public void setname ( String name)  {this.name = name;} /** *  @return  the type */public string gettype ()  {return type;} /** *  @param  type the type to set */public void settype ( String type)  {this.type = type;}} 
Package Main;import Bean. Book;public class Test1 {public static void main (string[] args) {//TODO auto-generated method Stubclass demo1=null; Class Demo2=null; Class demo3=null;//The first way to instantiate an object Try{demo1=class.forname ("Bean. Book ");} catch (Exception e) {e.printstacktrace ();} System.out.println (DEMO1);//Instantiate the object in the second way book Bo=new Book (); object Ob=bo; System.out.println ("Second" +ob.getclass ());//The Third way to instantiate the object Demo3=book.class; System.out.println ("The third" +demo3);//try {book bo1= (book) demo3.newinstance (); System.out.println ("Instantiated class object:" +bo1);} catch (Instantiationexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Illegalaccessexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

The mechanism and implementation of Field object

Package main;import java.lang.reflect.field;import bean. book;public class fieldtest {//This method is used to get the attributes in the class using the passed Class object Public void show (class  C1) {field[] fi=c1.getdeclaredfields ();  //{can get the private property to} getdeclaredfields () gets all the properties in the class for (Field  FF:FI) {System.out.println (Ff.getname ()); System.out.println (Ff.gettype ());} System.out.println ("~~~~~~~~~~~~~~~~~"); Field[] fi1=c1.getfields ();//can only get the public property for (FIELD FF1:FI1) {System.out.println (Ff1.getname ()); System.out.println (Ff1.gettype ());} System.out.println ("~~~~~~~~~~~~~~~~~");} The method is used to pass the entity class object passed over   get the property and the value of the property Public void show (Object ob) {class c1=ob.getclass (); Field[] fi=c1.getdeclaredfields (); for (Field ff:fi) {try {ff.setaccessible (true); System.out.println (Ff.getname () + ":" +ff.get (OB));  catch  (illegalargumentexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (Illegalaccessexception e)  {// todo auto-generated catch blocke.printstacktrace ();}}} Public static void main (String[] args)  {// todo auto-generated method  stubbook bo=new book (), Bo.setid (1), Bo.setname ("Love of the Allure"), Bo.settype ("literature"); Fieldtest ft=new fieldtest (); Ft.show (Book.class); Book oob=new book (); Oob.setid (2); Oob.setname ("The Summer of that year"); Oob.settype ("Literature"); Fieldtest ft1=new fieldtest (); Ft1.show (OOB);}}

Getdeclaredannotations () Get all annotations for a method

Package Main;import Java.lang.reflect.method;import Java.lang.reflect.modifier;import Bean. Book;public class Methodtest {//This method is used to get all method names, return value types, and parameter information for the object public void Show (Object ob) {class C2=ob.getclass (); Method[] Mt=c2.getdeclaredmethods (); for (method Mm:mt) {System.out.println ("Method name:" +mm.getname ()); System.out.println ("Method modifier" +modifier.tostring (Mm.getmodifiers ())); System.out.println ("Method return value type" +mm.getreturntype ()); System.out.println ("Method parameter list"); Class[] Pretype=mm.getparametertypes (); for (Class C21:pretype) {System.out.println (C21.getname ());}}} public static void Main (string[] args) {//TODO auto-generated method Stubbook bk=new Book (); Bk.setid (3); Bk.setname ("Me and sister Feng The day of the Life "); Bk.settype (" Thriller "); Methodtest mt=new methodtest (); Mt.show (BK);}}

Mechanism and implementation of Method object
Package main;import java.lang.reflect.invocationtargetexception;import java.lang.reflect.method ; Import java.lang.reflect.modifier;import bean. book;public class methodtest {//This method is used to get all method names, return value types, and parameter information for an object Public void show (object  ob) {class c2=ob.getclass (); Method[] mt=c2.getdeclaredmethods (); for (METHOD MM:MT) {System.out.println ("Method name:" +mm.getname ()); System.out.println ("Method modifier" +modifier.tostring (Mm.getmodifiers ())); System.out.println ("Method return value type" +mm.getreturntype ()); System.out.println ("Method parameter list"); Class[] pretype=mm.getparametertypes (); for (Class c21:pretype) {System.out.println (C21.getname ());}}} This method is used to use the passed entity object   Get its method   and call  public void showuse (OBJECT OB) {class c1= Ob.getclass (); Try {method me=c1.getmethod ("GetName",  null); Try {me.invoke (ob, new  object[0]);}  catch  (ILLEGALACCESSEXCEPTION E1)  {// todo auto-generated catch blocke1.printstacktrace ();} Method me1=c1.getmethod ("SetName",  string.class); Try {me1.invoke (ob,  "Hee Travels"); class[] c4={string.class,int.class}; Method me2=c1.getmethod ("Test",  C4), object[] obb={"haha", 12};me2.invoke (Ob, obb);}  catch  (illegalaccessexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}   catch  (illegalargumentexception e)  {// todo auto-generated catch  blocke.printstacktrace ();}  catch  (invocationtargetexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (nosuchmethodexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (securityexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}} Public static void main (String[] args)  {// Todo auto-generated method stubbook bk=new book (); Bk.setid (3); Bk.setName ("The day I Live with Fengjie") ; Bk.settype ("Thriller");//methodtest mt=new methodtest ();//mt.show (BK); Methodtest mte=new methodtest (); Mte.showuse (BK); System.out.println (Bk.getname ());}}




Java Reflection mechanism (not completed)

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.