当前位置:网站首页>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.
边栏推荐
- spark source code - task submission process - 3-ApplicationMaster
- flink cdc 目前支持Gauss数据库源吗
- LeetCode Interview Questions
- 监控系统的内卷,有什么讲究?
- 网络层协议介绍
- Spark source code - task submission process - 6-sparkContext initialization
- King power volume LinkSLA, realize operations engineer is happy fishing
- The problem of redirecting to the home page when visiting a new page in dsf5.0
- Configuration of TensorFlow ObjecDetectionAPI under Anaconda3 of win10 system
- 从“双卡双待“到”双通“,vivo率先推动DSDA架构落地
猜你喜欢

VRRP概述及实验

网络不通?服务丢包?看这篇就够了

教您简单几步实现工业树莓派正确安装RS232转USB驱动

King power volume LinkSLA, realize operations engineer is happy fishing

Unity realizes first-person roaming (nanny-level tutorial)

Hard Disk Partitioning and Permanent Mounting

Getting Started 05 Using cb() to indicate that the current task is complete

运维的高光时刻,从智能化开始

ACL 和NAT

单臂路由实验和三层交换机实验
随机推荐
[Day8] Commands involved in using LVM to expand
flink cdc 目前支持Gauss数据库源吗
markdown editor template
LinkSLA insists that users come first and creates a sustainable operation and maintenance service plan
智能运维会取代人工运维吗?
Will intelligent operation and maintenance replace manual operation and maintenance?
Dsf5.0 bounced points determine not return a value
Getting Started Document 09 Standalone watch
Spark source code-task submission process-6.1-sparkContext initialization-create spark driver side execution environment SparkEnv
浏览器存储WebStorage
ROS2下使用ROS1 bag的方法
线上问题排查流程
程序员应该这样理解I/O
微信小程序页面跳转传参
spark operator-textFile operator
Call the TensorFlow Objection Detection API for object detection and save the detection results locally
Proprietary host CDH
LeetCode面试题
静态路由
时间复杂度和空间复杂度