First, a custom class Person
package reflection; public class Person { private String name; public int age; 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; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person() { super(); } public Person(String name) { super(); this.name = name; } public void show() { System.out.println ( "I'm a Man" ); } private String showNation(String nation){ System.out.println ( "Nationality" + Nation); return Nation; } }
Then some of the operations reflected
package reflection; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Properties; import org.junit.jupiter.api.Test; public class ReflectionTest { @Test public void test1() throws Exception { Person p1 = new Person("Tom",12); @ 1 by reflection, to create an object of the Person class Class clazz = Person. Class ; Constructor cons = clazz.getDeclaredConstructor(String.class,int.class); Object obj = cons.newInstance("Tom",12); Person p = (Person)obj; System.out.println(p.toString()); // 2. reflection, call object specified attribute Field, clazz.getDeclaredField Age = ( "Age" ); age.set(p, 10); System.out.println(p.toString()); // 3. reflected by the calling method Method, Show = clazz.getDeclaredMethod ( "Show" ); show.invoke(p); System.out.println("**********************************************************"); } /* * Reflection call the private methods and properties * */ @Test public void test2 () throws Exception { the try { // invoked by reflection private methods and properties Class = clazz the Person. class ; // 1. private constructor calls the Constructor cons1 clazz.getDeclaredConstructor = (String. class ); cons1.setAccessible(true); Person person = (Person)cons1.newInstance("zsben"); System.out.println(person); // 2. Call the private property Field, name = clazz.getDeclaredField ( "name" ); name.setAccessible(true); name.set(person, "Lilei"); System.out.println(person); // 3. invoke private method Method, showNation = clazz.getDeclaredMethod ( "showNation", String. Class ); showNation.setAccessible(true); showNation.invoke(person, "中国"); // 4. The return value obtained private String string = (String) showNation.invoke ( person, " China" ); System.out.println(string); } catch (Exception e) { e.printStackTrace (); } } / * Understanding of the Class class * Loading process 1. class: java.exe command using a byte code file interpreted run, * Equivalent to a byte code file is loaded into memory, known as load class, the class is loaded into memory, called the run-time class * This would runtime class as an instance of the Class * Examples of 2.Class runtime class corresponds to a * 3. runtime class is loaded into memory, caches within a certain period of time, we can get this runtime class in different ways * * */ /* * Get Class instance four ways * */ @Test public void Test3 () throws a ClassNotFoundException { // Embodiment 1: calling runtime class attributes; Class later be without generic Class <the Person> = the Person of clazz1. class ; System.out.println(clazz1); @ Method 2: runtime class objects through the Person P1 = new new the Person (); Class clazz2 = p1.getClass(); System.out.println(clazz2); // Option 3: Class static method call forName (String classpath), classpath for the class path where Class clazz3 = Class.forName ( "reflection.Person" ); System.out.println(clazz3); // embodiment 4. class loader . ClassLoader = ReflectionTest ClassLoader class .getClassLoader (); classLoader.loadClass("reflection.Person"); Class clazz4 = classLoader.loadClass("reflection.Person"); System.out.println(clazz1==clazz2); System.out.println(clazz1==clazz3); System.out.println(clazz1==clazz4); } }
Class loader appreciated
package reflection; import java.io.FileInputStream; import java.io.InputStream; import java.util.Properties; import org.junit.jupiter.api.Test; /* * Class loading process: * 1. Load class: the class of the class file is read into memory, and whom to create a Class object, this process is completed by the class loader * 2. Link class: class binary data incorporated into the JRE, set the default value of a static variable (0, null, "", etc.) 3. Kind * Initialization: JVM is responsible for initializing, performing static block of code assignment performed in sequence and class attributes * * */ /* * Understand the class loader * * */ public class ClassLoaderTest { @Test public void test1 () { // for custom class, to give the class loader ClassLoaderTest: belongs System Classloader, i.e. the system class loader ClassLoader classLoader = ClassLoaderTest. class .getClassLoader (); System.out.println(classLoader); // Call system class loader getParent () obtained Extension Classloader, i.e. the extension class loader ClassLoader classLoader2 = ClassLoader.getParent (); System.out.println(classLoader2); // Bootstap the Classloader responsible for loading the core java library, not the ClassLoader classLoader3 = classLoader2.getParent (); System.out.println(classLoader3); // and 3 Similarly, String is the core java library ClassLoader classLoader4 = String. Class .getClassLoader (); System.out.println(classLoader4); } // read the configuration file is read @Test public void Test4 () throws Exception { // mode. 1 the Properties Pros = new new the Properties (); FileInputStream fis = new FileInputStream("jdbc.properties"); pros.load(fis); String user = pros.getProperty("user"); String passwd = pros.getProperty("password"); System.out.println(user+passwd); } }
Runtime class to create objects by reflection
package reflection; import org.junit.jupiter.api.Test; /* * Create objects corresponding runtime class by reflection * */ public class NewInstanceTest { @Test public void test1 () throws an InstantiationException is, IllegalAccessException { // instantiate abnormal, abnormal permissions . Class <the Person> = clazz the Person class ; Object object = clazz.newInstance(); System.out.println(object); /* * NewInstance () to create objects corresponding runtime class * Internal calls the empty constructor parameters * */ Person the Person = clazz.newInstance (); // here the person class can be obtained directly runtime class object System.out.println (person); } @Test public void test2() throws ClassNotFoundException, InstantiationException, IllegalAccessException { String classPath = ""; classPath = "java.util.Date"; System.out.println(getInstance(classPath)); classPath = "reflection.Person"; System.out.println(getInstance(classPath)); } // This method creates an instance of the specified class full name public Object getInstance (String classPath) throws ClassNotFoundException, InstantiationException, IllegalAccessException { Class clazz = Class.forName(classPath); return clazz.newInstance(); } }