Class in reflection mechanism What is inside the Class? Check out the picture below!
Code:
Class cls=Person.class;
1. Class class: 1. The information that the object can get after looking in the mirror: the data member name, method and constructor of a certain class, and the realization of a certain class which interfaces. For each class, the JRE keeps an immutable object of type Class for it. A Class object contains specific information about a class. 2. Class objects can only be created by the system. 3. A class has only one instance of Class in the JVM. 4. An instance of each class will remember which Class instance it was generated by. 5, Class is essentially a class! Is a class used to describe the internal information of the specified class itself! 2. Get the Class object [three methods] 1. Directly pass: classname.class Class cls=Person.class; 2. getClass() method Object obj=new Person(); Class cls2=obj.getClass(); 3. Class.forName("package name.class name") [most commonly used] [most used by frameworks] try { Class cls3=Class.forName("cn.sgg.reflections.Person"); } catch (ClassNotFoundException e) { e.printStackTrace (); } 3. Generics in reflection Example: Class<Person> cls3=(Class<Person>) Class.forName("cn.sgg.reflections.Person"); Class<?> cls3=(Class<Person>) Class.forName("cn.sgg.reflections.Person"); //? represents any object type 4. Methods in the Class class 1. The method of creating an instance of a class: newInstance() Class cls3=Class.forName("cn.sgg.reflections.Person"); Object obj=cls3.newInstance();//Get the instance (through: no parameter constructor) Object obj=cls3.newInstance(new Class[]{String.class,int.class});//Get the instance (through: parameter constructor) Note: Generally speaking, if a class declares a parameterless constructor, it also declares a parameterized constructor! (The parameter is reserved for reflection!) 2、Field<------>classType.getDeclaredFields(); 3、Method<----->getDeclaredMethod(getMethodName, new Class[]{}); 4、Constructor<------>getConstructor(new Class[]{int.class,String.class}); 5. Where is the reflective one used? In the framework:
Servlet, Strust2 and other basic frameworks... 6. Class Loader: ClassLoader illustrate: ClassLoader: The class loader is used to load the class (class) into the JVM. The JVM specification defines two types of class loaders. Boot class loader (bootstap) User-defined class loader. The JVM generates an initialization loader hierarchy consisting of 3 class loaders at runtime, As shown below:
Example: 1. //Get a system class loader ClassLoader classLoader=ClassLoader.getSystemClassLoader(); System.out.println(classLoader); 2. //Get a system class loader ClassLoader classLoader=ClassLoader.getSystemClassLoader(); System.out.println("System Loader: "+classLoader); 3. //Get the loader of the parent class of the system class loader ClassLoader parentLoader=classLoader.getParent(); System.out.println("The parent class loader of the system loader: "+classLoader); 4. //Get the parent class loader of the extension class loader classLoader=parentLoader.getParent(); System.out.println("Extended class loader's parent class loader: "+classLoader); 5. //Detect which class loader the current class is loaded by try { ClassLoader classLoader2=Class.forName("cn.sgg.reflections.TestReflect").getClassLoader(); System.out.println(classLoader2); } catch (ClassNotFoundException e) { e.printStackTrace (); } 6. //Detect which class loader is responsible for loading the Object class provided by JDK ClassLoader classLoader3; try { classLoader3 = Class.forName("java.lang.Object").getClassLoader(); System.out.println(classLoader3); } catch (ClassNotFoundException e) { e.printStackTrace (); } 7. An important method about class loader: The system class loader can load all the corresponding class files and other files under this project! /bin/xxx. file suffix Example: //An important method about the class loader: InputStream in=TestReflect.class.getResourceAsStream("xxxx.txt");
Test code:
package cn.sgg.reflections; /** * Reflection - tested class: */ public class Person { String name; private int age; public Person(){} public Person(String name){ this.name=name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package cn.sgg.reflections; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.lang.reflect.Field; /** * Reflection-test class: * */ public class TestReflect { public static void main(String[] args) { new TestReflect().testClassLoader(); } /** * How to test reflection */ static void getProperty(){ // 1. Get the Class object Class cls=Person.class ; Field[] fields = cls.getDeclaredFields(); // 2. Get the Class object Object obj= new Person(); Class cls2 = obj.getClass(); // 3. Get the Class object try { Class<?> cls3=(Class<Person>) Class.forName("cn.sgg.reflections.Person"); // Get the class instance Object obj1= cls3.newInstance(); } catch (Exception e) { e.printStackTrace (); } } static void testClassLoader(){ // Get a system class loader ClassLoader classLoader= ClassLoader.getSystemClassLoader(); System.out.println( "System Loader: "+ classLoader); // Get the loader of the parent class of the system class loader ClassLoader parentLoader= classLoader.getParent(); System.out.println( "The parent class loader of the system loader: "+ classLoader); // Get the parent class loader of the extended class loader classLoader= parentLoader.getParent(); System.out.println( "Extended class loader's parent class loader: "+ classLoader); // Detect which class loader the current class is loaded by try { ClassLoader classLoader2=Class.forName("cn.sgg.reflections.TestReflect").getClassLoader(); System.out.println(classLoader2); } catch (ClassNotFoundException e) { e.printStackTrace (); } // Detect which class loader is responsible for loading the Object class provided by JDK classLoader classLoader3; try { classLoader3 = Class.forName("java.lang.Object").getClassLoader(); System.out.println(classLoader3); } catch (ClassNotFoundException e) { e.printStackTrace (); } // An important method about the class loader: InputStream in= null ; in=TestReflect.class.getResourceAsStream("xxxx.txt"); } }
System Loader: sun.misc.Launcher$AppClassLoader@18d107f The parent class loader of the system loader: sun.misc.Launcher$AppClassLoader@18d107f The parent class loader of the extended class loader: null sun.misc.Launcher$AppClassLoader@18d107f null to get the Liu object of the picture: null