当前位置:网站首页>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.
边栏推荐
猜你喜欢
Getting Started 05 Using cb() to indicate that the current task is complete
VRRP principle and command
RAID磁盘阵列
Growth: IT Operations Trends Report
Getting Started Document 09 Standalone watch
disabledDate 日期选择器 datePicker
What?CDN cache acceleration only works for accelerating static content?
实力卷王LinkSLA,实现运维工程师快乐摸鱼
The problem of redirecting to the home page when visiting a new page in dsf5.0
静态路由
随机推荐
network issue?Service packet loss?This is enough
路由器和静态路由的配置
Insight into the general trend of the Internet, after reading this article, you will have a thorough understanding of Chinese domain names
TCP/IP four-layer model
static routing
Problems encountered in installing Yolo3 target detection module in Autoware
线上问题排查流程
跨域的十种解决方案详解(总结)
LeetCode Interview Questions
spark operator-parallelize operator
时间复杂度和空间复杂度
Quick question and quick answer - FAQ of Tencent Cloud Server
实力卷王LinkSLA,实现运维工程师快乐摸鱼
解决这三大问题,运维效率将超90%的医院
Hard Disk Partitioning and Permanent Mounting
To TrueNAS PVE through hard disk
从“双卡双待“到”双通“,vivo率先推动DSDA架构落地
[Day1] (Super detailed steps) Build a soft RAID disk array
千亿IT运维市场,产品要凭实力说话
618,你也许可以清醒亿点点