当前位置:网站首页>Some basic operations of reflection
Some basic operations of reflection
2022-07-03 07:22:00 【four thousand three hundred and forty-three】
One :
package Test2;
import Test.Person;
import org.junit.Test;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/*
Get all the properties of the current runtime class
*/
public class FieldTest {
@Test
public void test1(){
Class clazz = Person.class;
// Get attribute structure
Field[] fields = clazz.getFields();
for(Field f :fields){
// You can only get the current class and its parent class with permission public Properties of
System.out.println(f);
}
System.out.println("");
//getDeclaredFields() : Get the existing properties of the current class , Including private ownership ( There is no parent class )
Field[] declaredFields = clazz.getDeclaredFields();
for(Field f:declaredFields){
System.out.println(f);
}
}
// Permission modifier data type Variable name
@Test
public void test2(){
Class clazz = Person.class;
Field[] declaredFields = clazz.getDeclaredFields();
for(Field f:declaredFields){
//1. Permission modifier
// At this time, the returned is 0:default,1:public,2:private 4:protected
int modifiers = f.getModifiers();
// Translate it Modifier.toString()
System.out.print(Modifier.toString(modifiers));
//2. data type
Class<?> type = f.getType();
System.out.print(" "+type+"\t");
//3. Variable name
String name = f.getName();
System.out.println(name);
}
}
}
Two :
package Test2;
import Test.Person;
import org.junit.Test;
import java.lang.reflect.Method;
/*
Get the method structure of the runtime class
*/
public class MethodTest {
@Test
public void test(){
Class clazz = Person.class;
//getMethods() : Get all declarations of the current runtime class and its parent class as public The method of authority
Method[] method = clazz.getMethods();
for(Method m : method){
System.out.println(m );
}
System.out.println();
//getDeclaredMethods(): Get all the methods of the current runtime class ( There is no parent class )
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method m :declaredMethods){
System.out.println(m);
}
}
@Test
public void test2(){
}
}
( heavy !) 3、 ... and :
package Test2;
import Test.Person;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/*
Call the structure specified in the runtime class : attribute 、 Method 、 Constructors
*/
public class ReflectionTest {
/*
get、set
*/
@Test
public void testField() throws Exception {
Class clazz = Person.class;
// Create an object of a runtime class
Person person =(Person) clazz.newInstance();
// Get the specified properties
Field id = clazz.getField("id");
/*
Set the value of the current property : The attribute in the runtime class is required to be public
This method is not usually used
id.set(person,1001);
set(): Parameters 1: Indicates which object's properties are set Parameters 2:value
*/
// Gets the property of the specified variable name of the runtime class
Field name = clazz.getDeclaredField("name");
// Before setting, you need to get the right Private and defualt Permission attribute jurisdiction
name.setAccessible(true);
name.set(person,"tom");
System.out.println(name.get(person ));
/*
Gets the value of the current property
get(): Parameters 1: Get the properties of which object
*/
Object o = id.get(person);
System.out.println(o);
}
/*
How to operate the methods specified by the runtime class
*/
@Test
public void testMethod() throws Exception{
Class clazz = Person.class;
Person p = (Person)clazz.newInstance();
//1. Get a specified method Parameters 1: Indicates the name of the get method Parameters 2: Because of possible overloading , Indicates the parameter list of the get method
Method show = clazz.getDeclaredMethod("show", String.class);
// Ensure that the current method is accessible
show.setAccessible(true);
// invoke(): Parameters 1: The caller of the method Parameters 2: Assign values to the parameters of the method
// invoke() The return value of the method is the return value of the method called in the corresponding class
Object china = show.invoke(p, "China");
System.out.println(china);
// Call static methods
Method showDesc = clazz.getDeclaredMethod("showDesc");
showDesc.setAccessible(true);
// If the method calling the runtime class has no return value , Back in null
// Because of the static method , Every object is the same , So there is no need to specify the calling object , But it can't be empty Object.class , null, Person.class....
Object invoke = showDesc.invoke(null);
System.out.println(invoke);//null
}
/*
How to call the constructor specified in the runtime class
*/
@Test
public void testConstructor() throws Exception{
Class clazz = Person.class;
//1. Gets the specified constructor ( Constructor names are the same ) Parameters : Indicates the parameter list of the constructor
Constructor declaredConstructor = clazz.getDeclaredConstructor(String.class);
//2. Ensure that this constructor is accessible
declaredConstructor.setAccessible(true);
//3. Call this constructor to create an object of the runtime class
Object o = declaredConstructor.newInstance("Tom");
System.out.println(o);
}
}
边栏推荐
- Advanced APL (realize group chat room)
- [Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
- The education of a value investor
- SharePoint modification usage analysis report is more than 30 days
- Arduino Serial系列函数 有关print read 的总结
- POI excel percentage
- Interfaces and related concepts
- Mise en place d'un environnement de développement de fonctions personnalisées
- Some experiences of Arduino soft serial port communication
- File links cannot be opened or downloaded in Google browser
猜你喜欢
![[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)](/img/7f/3d6b6ea5319f7165e07baf0a78d318.jpg)
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)
![PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef](/img/65/1f28071fc15e76abb37f1b128e1d90.jpg)
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef

Interview questions about producers and consumers (important)

VMWare网络模式-桥接,Host-Only,NAT网络

Flask Foundation

POI excel percentage

Basic knowledge about SQL database

SecureCRT取消Session记录的密码

3311. Longest arithmetic

JMeter test result output
随机推荐
万卷书 - 价值投资者指南 [The Education of a Value Investor]
SecureCRT password to cancel session recording
C WinForm framework
Interview questions about producers and consumers (important)
[untitled]
Some experiences of Arduino soft serial port communication
The embodiment of generics in inheritance and wildcards
C代码生产YUV420 planar格式文件
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
JMeter test result output
Distributed transactions
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
Setting up the development environment of dataworks custom function
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
[HCAI] learning summary OSI model
IP home online query platform
Take you through the whole process and comprehensively understand the software accidents that belong to testing
CentOS php7.3 installing redis extensions
Advanced API (UDP connection & map set & collection set)