当前位置:网站首页>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);
}
}
边栏推荐
- [plus de détails] dernière entrevue complète redis (50)
- docket
- PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
- Advanced API (serialization & deserialization)
- C代码生产YUV420 planar格式文件
- 4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
- Strategy mode
- Operation and maintenance technical support personnel have hardware maintenance experience in Hong Kong
- Inno setup production and installation package
- “百度杯”CTF比赛 2017 二月场,Web:爆破-1
猜你喜欢
Pits encountered in the use of El checkbox group
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
Hash table, generic
High concurrency memory pool
Use the jvisualvm tool ----- tocmat to start JMX monitoring
Distributed transactions
Summary of abnormal mechanism of interview
Topic | synchronous asynchronous
691. 立方体IV
Setting up the development environment of dataworks custom function
随机推荐
Mise en place d'un environnement de développement de fonctions personnalisées
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
gstreamer ffmpeg avdec解码数据流向分析
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
docker建立mysql:5.7版本指定路径挂载不上。
Setting up the development environment of dataworks custom function
GStreamer ffmpeg avdec decoded data flow analysis
Dora (discover offer request recognition) process of obtaining IP address
[HCAI] learning summary OSI model
PHP install composer
2021-07-18
4279. Cartesian tree
How to specify the execution order for multiple global exception handling classes
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
Advanced API (serialization & deserialization)
Take you through the whole process and comprehensively understand the software accidents that belong to testing
TreeMap
twenty million two hundred and twenty thousand three hundred and nineteen
【无标题】