Basic concept of reflection ?

Reflection mainly means that the program can access , The ability to detect and modify its own state or behavior .Java
The reflection mechanism is in progress , For any class , All the properties and methods of this class can be known , For any object , Can call any of its methods .

What is? Java Reflex mechanism ?

Java After program start , First of all .java Source code compiled into .class Bytecode , And then .class Bytecode loaded to JVM Running in . When we new
When an object , Java The object type is known at code compilation time , Then create the object at run time . and Java The reflection mechanism is to create objects in reverse at runtime , Get properties and methods ,
It does not know the object type at compile time . Java Reflection is often used in various frames , as Spring The process of getting objects in :​​​​​​
<!-- definition bean--><beans <bean id="userId" class="com.tyshawn.domain.User"></bean>
</beans> // Read here xml configuration file , Get to "com.tyshawn.domain.User", Then create the object by reflection User user =
(User) context.getBean("userId");
Java Basic use of reflection

User Entity class
public class User { public String name; private Integer age; public User() { }
public User(String name, Integer age) { this.name = name; this.age = age; }
public void say(){ System.out.println("Hello " + this.getName()); } public
void say(String name){ System.out.println("Hello " +name); } public String
getName() { return name; } public void setName(String name) { this.name = name;
} public Integer getAge() { return age; } public void setAge(Integer age) {
this.age = age; } @Override public String toString() { return "User{" +
"name='" + name + '\'' + ", age=" + age + '}'; }}
obtain Class class

(1) Class.forName()
Class<?> clazz = Class.forName("com.tyshawn.domain.User");
(2) class .class
Class<User> clazz = User.class;
(3) Object.getClass()​​​​​​​
User user = new User();Class<? extends User> clazz = user.getClass();
The most common is the first , The latter two are meaningless .

Create instance

(1) call Class Object's newInstance() method ​​​​​​​
Class<?> clazz = Class.forName("com.tyshawn.domain.User");User user = (User)
clazz.newInstance(); // Only nonparametric structure
(2) Call the newInstance() method ​​​​​​​
Class<?> clazz = Class.forName("com.tyshawn.domain.User");// Nonparametric structure Constructor<?>
constructor = clazz.getConstructor();User user = (User)
constructor.newInstance(); // Parametrical structure Constructor<?> constructor1 =
clazz.getConstructor(String.class, Integer.class);User user1 = (User)
constructor1.newInstance("tyshawn", 23);
Get member variable

(1) Get public member variable ​​​​​​​
Class<?> clazz = Class.forName("com.tyshawn.domain.User");User user = (User)
clazz.newInstance(); // Get common member variable Field name = clazz.getField("name");// assignment
name.set(user, "tyshaw"); System.out.println(user);
(2) Get private member variable ​​​​​​​
Class<?> clazz = Class.forName("com.tyshawn.domain.User");User user = (User)
clazz.newInstance(); // Get common member variable Field age = clazz.getDeclaredField("age");
// Remove private permissions age.setAccessible(true);// assignment age.set(user, 23); System.out.println(user);
Get member method

(1) Get public member method ​​​​​​​
Class<?> clazz = Class.forName("com.tyshawn.domain.User");Constructor<?>
constructor = clazz.getConstructor(String.class, Integer.class);User user =
(User) constructor.newInstance("Tyshawn", 23); // Get nonparametric member method Method say =
clazz.getMethod("say");say.invoke(user); // Hello Tyshawn // Get member method with parameters Method say1
= clazz.getMethod("say", String.class);say1.invoke(user, "Tom"); //Hello Tom
(2) Get private member method ​​​​​​​
Class<?> clazz = Class.forName("com.tyshawn.domain.User");Constructor<?>
constructor = clazz.getConstructor(String.class, Integer.class);User user =
(User) constructor.newInstance("Tyshawn", 23); // Get nonparametric member method Method method =
clazz.getDeclaredMethod("say");// Remove private permissions method.setAccessible(true);
method.invoke(user); // Hello Tyshawn // Get member method with parameters Method method1 =
clazz.getDeclaredMethod("say", String.class);// Remove private permissions
method1.setAccessible(true);method1.invoke(user, "Tom"); //Hello Tom

Technology
©2019-2020 Toolsou All rights reserved,
Vue Get the text and option value of the drop-down box Fiddler Simulate request sending and modifying response data ( Essence )2020 year 6 month 26 day C# Class library read json Profile help class PHP Printout logvue monitor Treeselect Change of options Linux File name validity detection mysql No backup recovery use PyMC3 Bayesian statistical analysis was performed ( code + example )mybatis-config.xml Set default value configuration TypeScript Data types in is enough