Java reflection and java

Source: Internet
Author: User

Java reflection and java

Definition:The Java reflection mechanism allows us to check information about classes, interfaces, variables, and methods at Runtime outside the compilation period (Compile Time.
Reflection also allows us to instantiate objects at runtime, call methods, and obtain variable values by calling the get/set method.

 

Reflection:Using the Java reflection mechanism can check Java class information during runtime. Checking Java class information is often the first thing you do when using the Java reflection mechanism,
You can obtain the following information about the class:

Class Object
Class Name
Modifier
Package information
Parent class
Implemented Interface
Constructor
Method
Variable
Annotation

Class Object:
Two Methods for retrieving class objects through reflection
Class. forName ("Full name of Class name ");
ForName is the static method of the Class object. It loads the Class and performs static Class initialization to return the Class object.

ClassName. class;
JVM will use the Class loader to load the Class into the memory (provided that the Class has not been loaded into the memory) without Class initialization. Class objects will be returned.

 

For details, refer to the following code:

Class <?> Bean = Class. forName ("com. *. common. reflectBean "); ReflectBean reflectBean = (ReflectBean) bean. newInstance (); System. out. println ("instance 1" + reflectBean); Class <?> Bean1 = ReflectBean. class; ReflectBean reflectBean1 = (ReflectBean) bean1.newInstance (); System. out. println ("instance 2" + reflectBean1); int modifiers = bean. getModifiers (); System. out. println ("class modifier" + modifiers); System. out. println ("Whether the Modifier of the class is public" + Modifier. isPublic (modifiers); System. out. println ("full name of the class" + bean. getName (); System. out. println ("Class Name" + bean. getSimpleName (); Package packageInfo = bean. getPackage (); System. out. println ("Class package information" + packageInfo); Class superClass = bean. getSuperclass (); System. out. println ("parent class of the class" + superClass. getName (); for (Class interFace: bean. getInterfaces () {System. out. println ("class interFace" + interFace. getName () ;}for (Constructor interFace: bean. getConstructors () {System. out. println ("class constructor" + interFace. getName ();} for (Method interFace: bean. getMethods () {System. out. println ("class method" + interFace. getName ();} for (Field interFace: bean. getFields () {System. out. println ("common variables of the class" + interFace. getName () ;}for (Annotation annotation: bean. getAnnotations () {// There is no annotation here because setter getter and so on are all in the compilation period. This gets the runtime System. out. println ("class annotation" + annotation. toString ());}
@Getter@Setter@ToString(callSuper = true)@Lazypublic class ReflectBean extends ReflectBeanSuper implements ReflectInterface {    public String name;    public ReflectBean () {    }    public ReflectBean (String name) {        this.name = name;    }    @Override    public String getReflectName() {        return name;    }}
@Getter@Setter@ToStringpublic class ReflectBeanSuper {    private String sex;}
public interface ReflectInterface {    String getReflectName();}

Running result

It is relatively simple to write here, all of which are reflected in the Code. If you need specific operations, you can refer to the relevant API

 

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.