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