当前位置:网站首页>Four ways to obtain Class objects through reflection
Four ways to obtain Class objects through reflection
2022-08-05 06:20:00 【Sajia Roshan Demon King】
Java reflection mechanism
Reflection: Through reflection, we can get all the properties and methods of any class, and we can also call these methods and properties.
Advantages and disadvantages of reflection
Benefits: Makes the code more flexible and provides convenience for out-of-the-box functionality for various frameworks
Disadvantages: It gives us the ability to analyze operation classes at runtime, which also increases security issues.
For example, the security check of generic parameters can be ignored (the security check of generic parameters occurs at compile time).
In addition, the performance of reflection is also slightly worse, but it has little effect on the framework.
If we get this information dynamically, we need to rely on the Class object.Class class object tells the running program of a class's methods, variables and other information.
Four ways to get a Class object
via
Class.forName()Get the full path of the incoming classGet by instance.getClass() of the object instancestrong>
Get the full path of the class through the class loader ClassLoader.loadClass()
Get through a specific instance object
Give a chestnut
/*** 4 ways to obtain Class objects through reflection*/@Testpublic void reflectGetMethod_Test() {try {String classPath = "com.hl.magic.items.day20.reflection.ReflectiveDemo";Class> forName = Class.forName(classPath);LOGGER.debug("[1]Get the path of the current class according to the full path of the class: {}", forName.getName());String name = ReflectiveDemo.class.getName();LOGGER.debug("[2]-Get the path of the current class according to the class object: {}", name);ReflectiveDemo reflectiveDemo = new ReflectiveDemo();Class extends ReflectiveDemo> aClass = reflectiveDemo.getClass();LOGGER.debug("[3]Get the path of the current class according to the object instance: {}", aClass.getName());Class> aClass1 = ClassLoader.getSystemClassLoader().loadClass(classPath);LOGGER.debug("[4]Get the path of the current class according to the class loader: {}", aClass1.getName());// Get the corresponding property according to the class pathReflectiveDemo reflectiveDemo1 = (ReflectiveDemo) forName.newInstance();String userName = reflectiveDemo1.getUserName();LOGGER.debug("Get the properties of the reflection object: [{}]", userName);} catch ( ClassNotFoundException | InstantiationException | IllegalAccessException e) {e.printStackTrace();}}Output:
[main] DEBUG com.hl.magic.items.day20.reflection.ReflectiveDemo - [1] Get the path of the current class based on the full class path: com.hl.magic.items.day20.reflection.ReflectiveDemo[main] DEBUG com.hl.magic.items.day20.reflection.ReflectiveDemo - [2]-Get the path of the current class according to the class object: com.hl.magic.items.day20.reflection.ReflectiveDemo[main] DEBUG com.hl.magic.items.day20.reflection.ReflectiveDemo - [3] Get the path of the current class according to the object instance: com.hl.magic.items.day20.reflection.ReflectiveDemo[main] DEBUG com.hl.magic.items.day20.reflection.ReflectiveDemo - [4] Get the path of the current class according to the class loader: com.hl.magic.items.day20.reflection.ReflectiveDemo[main] DEBUG com.hl.magic.items.day20.reflection.ReflectiveDemo - Get the properties of the reflection object: [Xiao Ming]Getting a Class object through the class loader will not be initialized, which means that static code blocks and static objects will not be executed without a series of steps including initialization.
边栏推荐
- Wechat applet page jump to pass parameters
- LinkSLA坚持用户第一,打造可持续的运维服务方案
- js dynamically get screen width and height
- 初识网页与浏览器
- Advantages of overseas servers
- 传输层协议
- Getting Started Documentation 12 webserve + Hot Updates
- Spark source code - task submission process - 4-container to start executor
- 浏览器存储WebStorage
- 7 steps to complete cloud monitoring
猜你喜欢

传输层协议(TCP3次握手)

有哪些事情是你做了运维才知道的?

VLAN details and experiments

NIO works is analysed

ALC实验
![[Day5] Soft and hard links File storage, deletion, directory management commands](/img/15/7ed58a180a72ace3463626bf446633.png)
[Day5] Soft and hard links File storage, deletion, directory management commands

IP packet format (ICMP protocol and ARP protocol)

sql server 重复值在后面计数

【Machine Learning】1 Univariate Linear Regression
![[Day6] File system permission management, file special permissions, hidden attributes](/img/ec/7fb3fa671fac8abf389844c0f4fbe7.png)
[Day6] File system permission management, file special permissions, hidden attributes
随机推荐
LeetCode面试题
从“双卡双待“到”双通“,vivo率先推动DSDA架构落地
What are some things that you only know when you do operation and maintenance?
运维的高光时刻,从智能化开始
单臂路由实验和三层交换机实验
VRRP principle and command
Disk management and file systems
Regular expression small example - get number character and repeated the most
Small example of regular expression--validate email address
VLAN details and experiments
Tencent greetings function SCF - entry instructions
The spark operator - repartition operator
js dynamically get screen width and height
LeetCode Interview Questions
初识网页与浏览器
VLAN详解及实验
IP数据包格式(ICMP协议与ARP协议)
lvm logical volume and disk quota
NIO works is analysed
static routing