Java.lang.class<t>--Reflection mechanism

Source: Internet
Author: User

1 What is the reflection mechanism?

The reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the method of the object is called the reflection mechanism of the Java language.

2 What the reflection mechanism can do

The reflection mechanism mainly provides the following functions:

    • Determine the class to which any object belongs at run time;

    • Constructs an object of any class at run time;

    • Determine the member variables and methods of any class at run time;

    • A method of invoking any object at run time;

    • Generates a dynamic agent.

3 Correlation API for reflection mechanism Get the full package name and class name from an object
 Package edu.pri.lime.test;  Public class Testreflect {    publicstaticvoid  main (string[] args) {          New  testreflect (); //         Get the full package name and class name         System.out.println (Reflect.getclass (). GetName ())     from an object;}}
Instantiating a Class class object
 Packageedu.pri.lime.test; Public classTestreflect { Public Static voidMain (string[] args)throwsclassnotfoundexception{Class<?> Clazza = Class.forName ("Edu.pri.lime.test.TestReflect"); Class<?> CLAZZB =Newtestreflect (). GetClass (); Class<?> CLAZZC = Testreflect.class;        System.out.println (Clazza.getname ());        System.out.println (Clazzb.getname ());    System.out.println (Clazzc.getname ()); }}
Gets the parent class of an object with the implemented interface
 Packageedu.pri.lime.test;Importjava.io.Serializable; Public classTestreflectImplementsserializable{Private Static Final LongSerialversionuid = -3407476067923921222l;  Public Static voidMain (string[] args)throwsclassnotfoundexception{Class<?> clazz = Class.forName ("Edu.pri.lime.test.TestReflect");//Get parent classClass<?> Clazzsup =Clazz.getsuperclass ();        System.out.println (Clazzsup.getname ()); //Get all Interfacesclass<?> clazzinte[] =clazz.getinterfaces ();  for(class<?>In:clazzinte)        {System.out.println (In.getname ()); }    }}
Get all constructors in a class
 Packageedu.pri.lime.test;Importjava.io.Serializable;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Modifier; Public classTestreflectImplementsserializable{Private Static Final LongSerialversionuid = -3407476067923921222l; Private intID; PrivateString name;  PublicTestreflect () {Super(); //TODO auto-generated Constructor stub    }     PublicTestreflect (intID) {Super();  This. ID =ID; }     Publictestreflect (String name) {Super();  This. Name =name; }     PublicTestreflect (intID, String name) {        Super();  This. ID =ID;  This. Name =name; }     Public Static voidMain (string[] args)throwsclassnotfoundexception{Class<?> clazz = Class.forName ("Edu.pri.lime.test.TestReflect");//Get all ConstructorsConstructor<?>[] Constructors =clazz.getconstructors ();  for(constructor<?>con:constructors) {//get constructor all parameter typesclass<?>[] Paramtypes =con.getparametertypes (); System.out.print (Modifier.tostring (Con.getmodifiers ())+ " "); System.out.print (Con.getname ()+ "(");  for(inti = 0;i < paramtypes.length;i++){                if(I < Paramtypes.length-1) {System.out.print (Paramtypes[i].getname (). substring (Paramtypes[i].getname (). LastIndexOf (".") + 1) + ","); }Else{System.out.print (Paramtypes[i].getname (). substring (Paramtypes[i].getname (). LastIndexOf (".") + 1)); }} System.out.println (");"); }    }}
Instantiating an object of a class through a reflection mechanism

Java.lang.class<t>--Reflection mechanism

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.