Reflective Popular Understanding
(based on a string) resolves the various components of a class (constructors, fields, methods).
Mainly used for framing.
The following is a simple test class
The first is a test entity, in this case a person class. There are five constructors within a class. There are two types of structures inside:
1, the public
2. Private
Each of these types contains no parameters, and parameters (one or more)
Then there is a test class that mainly tests the acquisition of constructors for several types of combinations described above, and creates objects and outputs test code. The test was tested with JUnit.
* * * * There are two ways to reflect the creation of objects.
In relation to the first method, getting a private constructor requires Getdeclaredconstructor () to get, before the instance is created, by Setaccessible (true) to be set to be executed outside the class .
With regard to the second approach, the creation of a class must be guaranteed to require a parameterless constructor in the class .
1. Get to the constructor, create an instance (object) from the constructor function
2. After loading the class, create an instance directly from the class
Person.java:
1 PackageCn.rt.gwq;2 3 Importjava.util.List;4 5 Public classPerson {6 7 PublicString value = "Public value";8 9 /**Ten * Parameterless Constructors One */ A PublicPerson () { -System.out.println ("No params constructor"); - } the - /** - * constructor for a parameter - * @paramname + */ - PublicPerson (String name) { +System.out.println ("String name constructor"); AValue = "Change the value:" +name; at } - - /** - * constructor for a parameter - * @param Age - */ in PublicPerson (intAge ) { -SYSTEM.OUT.PRINTLN ("int Age constructor"); toValue = "Change the value:" +Age ; + } - the /** * * Constructors for multiple parameters $ * @paramUSMPanax Notoginseng * @parampwd - */ the PublicPerson (String usm,string pwd) { +System.out.println ("String usm,string pwd Constructor"); AValue = "Change the value:" +USM + ":" +pwd; the } + - /** $ * Private Constructors $ * @paramList - */ - PrivatePerson (List list) { theSystem.out.println ("List constructor"); -Value = "Change the value:" +List.toarray (). toString ();Wuyi } the -}Person.java
Demo.java:reflect
1 PackageCn.rt.gwq;2 3 ImportJava.lang.reflect.Constructor;4 Importjava.util.ArrayList;5 Importjava.util.List;6 7 Importorg.junit.Test;8 9 Public classDemo {Ten One /** A * Get parameterless constructors and create objects - * @throwsException - */ the @Test - Public voidTest1 ()throwsexception{ - - //Load Class +Class clazz1 = Class.forName ("Cn.rt.gwq.Person"); - //Get Constructor +Constructor C = Clazz1.getconstructor (NULL); A //to create an object from a constructor function atPerson P = (person) c.newinstance (NULL); - //Validation - System.out.println (p.value); - - return; - in } - to /** + * Get constructors with parameters and create objects - * @throwsException the */ * @Test $ Public voidTest2 ()throwsException {Panax Notoginseng -Class clazz2 = Class.forName ("Cn.rt.gwq.Person"); theConstructor C = Clazz2.getconstructor (String.class); +Person P = (person) c.newinstance ("Bob"); A System.out.println (p.value); the + return; - } $ $ /*** - * Get constructors for multiple parameters and create objects - * @throwsException the */ - @TestWuyi Public voidTest3 ()throwsexception{ the -Class clazz3 = Class.forName ("Cn.rt.gwq.Person"); WuConstructor C = Clazz3.getconstructor (String.class, String.class); -Person P = (person) c.newinstance ("username", "password"); About System.out.println (p.value); $ - return ; - } - A /** + * Gets the private constructor and creates the object the * @throwsException - */ $ @Test the Public voidTest4 ()throwsexception{ the the //Load Class theClass clazz3 = Class.forName ("Cn.rt.gwq.Person"); - //Get Constructor in //Constructor C = clazz3.getconstructor (List.class); the //need to use a declared method to read private methods theConstructor C = Clazz3.getdeclaredconstructor (List.class); About //Mandatory readable 0 theC.setaccessible (true); the //Parameters theList List =NewArraylist<>(); +List.add (1); List.add (2); - //Creating Objects thePerson p =(person) c.newinstance (list);Bayi //Validation the System.out.println (p.value); the - return; - the } the the /*** the * Create objects directly from a class without a constructor - * Prerequisite: There must be a parameterless constructor the * @throwsException the */ the @Test94 Public voidTEST5 ()throwsException { the theClass clazz = Class.forName ("Cn.rt.gwq.Person"); thePerson p =(person) clazz.newinstance ();98 System.out.println (p.value); About - }101 102 103}Demo.java
Anatomy of Java Reflection constructors