Java Reflection Mechanism Learning: initial reflection mechanism

Source: Internet
Author: User

I small white one, want to share with everyone I learn Java notes and experience.

Reflection mechanism:
Refers to classes that can be loaded, known, and used completely unknown during compilation at run time.
Program in the running state, you can dynamically load a class with only name, for any of the loaded class, can know all the properties and methods of the class, for any one object, can call any of its methods and properties;

Class clazz = Class.forName (Com.danchel.reflect.User);
After the class has been loaded, in heap memory, an object of class type is generated (a class has only a single class object), and this object contains the structure information of the complete class.
We can see the structure of the class through this object. This object is like a mirror that sees the structure of the class through this mirror, so what we call the Image: Reflection.

Class is the source of reflection.
For any class that you want to dynamically load and run, you can only get the corresponding class object first.

How do objects of class classes get?
1, the use of getclass ();
2, the use of Class.forName ();
3. Use the. class syntax.

objects are used to represent and encapsulate some data. Once a class is loaded, the JVM creates a class object that should be classes, and the entire structure information for the class is placed in the corresponding class object.
This class object is like a mirror, and through this mirror I can see all the information of the corresponding class.
The core of the reflection mechanism is the class object here.

Note: The same class will only be loaded once (the value of Hashcode is the same).
Class Clazz1=class.forname (Com.danchel.reflect.User);
System.out.println (Clazz.hashcode ());
Class Clazz2=class.forname (Com.danchel.reflect.User);
System.println (Clazz2.hashcode ());

Apply the reflected API to get the class information (name, attribute, method, constructor, etc.)
Get the name of the class
System.out.println (Clazz.getname ()); Gets the name of the class that includes the package name
System.out.println (Clazz.getsinglename ()); Get the name of the class only, excluding the package name

Package com.danchel.reflect;

Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;

public class Demo02 {
public static void Main (string[] args) {
String Path = "Com.danchel.reflect.User";

try {
Class clazz =class.forname (path);

System.out.println (Clazz.getname ());//Gets the name of the class, including the package name
System.out.println (Clazz.getsimplename ()); Only get the name of the class, not the package name

Field[] Fields2 =clazz.getfields ();//Get all the properties that access is public, the property that the access permission is protected,default,private cannot get
Field[] Fields=clazz.getdeclaredfields ();//Get all the properties, not restricted by access rights
Field field =clazz.getdeclaredfield ("name");//Get a single property, not restricted by access, parameter is a string form of the property name that the object is getting
System.out.println (field);
for (Field f:fields) {
System.out.println ("attribute:" +f);
}

Clazz.getmethods ();//Get All access rights to public methods
Method[] Methods =clazz.getdeclaredmethods ();
Method Method=clazz.getdeclaredmethod ("SetName", string.class);//Get a single method, the first argument is the string form of the method name that the object is going to get, The second parameter is the type of the parameter that the method needs to pass in. Class
for (Method temp:methods) {
System.out.println ("Method:" +temp);
}

Constructor[] Constructors=clazz.getdeclaredconstructors ();
Constructor C = clazz.getdeclaredconstructor (String.class,string.class,int.class);//Get a constructor, Arguments are the types of arguments that the constructor needs to pass in. Class
System.out.println (c);
for (Constructor constructor:constructors) {
System.out.println ("constructor:" +constructor);
}

} catch (Exception e) {
E.printstacktrace ();
}
}
}

Java Reflection Mechanism Learning: initial 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.