当前位置:网站首页>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);
}
}
边栏推荐
- SecureCRT password to cancel session recording
- [vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)
- Resthighlevelclient gets the mapping of an index
- Advanced APL (realize group chat room)
- TCP cumulative acknowledgement and window value update
- Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
- Crontab scheduled task
- IP home online query platform
- JS monitors empty objects and empty references
- [HCAI] learning summary OSI model
猜你喜欢

深度学习参数初始化(一)Xavier初始化 含代码

Dora (discover offer request recognition) process of obtaining IP address

Map interface and method

Common APIs

Recursion, Fibonacci sequence

Topic | synchronous asynchronous

Le Seigneur des anneaux: l'anneau du pouvoir

高并发内存池

【已解决】Unknown error 1146

C code production YUV420 planar format file
随机推荐
Advanced API (local simulation download file)
高并发内存池
docker建立mysql:5.7版本指定路径挂载不上。
VMWare网络模式-桥接,Host-Only,NAT网络
dataworks自定義函數開發環境搭建
Advanced API (batch image Download & socket dialog)
When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
【无标题】
Spa single page application
C WinForm framework
Map interface and method
LeetCode
Jeecg menu path display problem
JMeter JSON extractor extracts two parameters at the same time
Selenium key knowledge explanation
Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS
Common APIs
sharepoint 2007 versions
Jeecg data button permission settings
How to specify the execution order for multiple global exception handling classes