当前位置:网站首页>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.
边栏推荐
- 用户和用户组管理、文件权限管理
- vim教程:vimtutor
- The Servlet to jump to the JSP page, forwarding and redirection
- Mina的长连接和短连接
- 正则表达式小实例--去掉字符串中间和两边的空格
- disabledDate 日期选择器 datePicker
- LinkSLA insists that users come first and creates a sustainable operation and maintenance service plan
- el-autocomplete使用
- Getting Started Document 09 Standalone watch
- Regular expression small example - get number character and repeated the most
猜你喜欢

Small example of regular expression--validate email address

Growth: IT Operations Trends Report

线上问题排查流程

ALC实验
![[Paper Intensive Reading] The relationship between Precision-Recall and ROC curves](/img/8f/3c9944db96eef623779a5abe68355b.png)
[Paper Intensive Reading] The relationship between Precision-Recall and ROC curves

监控系统的内卷,有什么讲究?

从“双卡双待“到”双通“,vivo率先推动DSDA架构落地

Getting Started Document 09 Standalone watch

静态路由

Getting Started Document 01 series in order
随机推荐
交换机原理
The problem come from line screening process
带你深入了解Cookie
To TrueNAS PVE through hard disk
spark source code - task submission process - 2-YarnClusterApplication
[Day1] (Super detailed steps) Build a soft RAID disk array
Mina断线重连
spark source code-RPC communication mechanism
Technology Sharing Miscellaneous Technologies
NIO works is analysed
Getting Started 04 When a task depends on another task, it needs to be executed in sequence
Cloud computing - osi seven layers and TCP\IP protocol
sql server 重复值在后面计数
What impact does CIPU have on the cloud computing industry?
Getting Started 05 Using cb() to indicate that the current task is complete
NAT实验
【Machine Learning】1 Univariate Linear Regression
VRRP principle and command
磁盘管理与文件系统
What's the point of monitoring the involution of the system?