当前位置:网站首页>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.
边栏推荐
- What's the point of monitoring the involution of the system?
- What should I do if the SSL certificate prompts that it is expired or invalid?
- spark operator-textFile operator
- The Servlet to jump to the JSP page, forwarding and redirection
- 增长:IT运维发展趋势报告
- Problems encountered in installing Yolo3 target detection module in Autoware
- Logical volume creation
- Advantages of overseas servers
- spark source code - task submission process - 3-ApplicationMaster
- 磁盘管理与文件系统
猜你喜欢
随机推荐
spark operator-parallelize operator
深度 Zabbix 使用指南——来自惨绿少年
markdown编辑器模板
static routing
静态路由
Regular expression small example - get number character and repeated the most
The highlight moment of operation and maintenance starts with intelligence
Apache configure reverse proxy
5分钟完成mysql离线安装
产品学习资料
带你深入了解Cookie
[ingress]-ingress使用tcp端口暴露服务
The Servlet to jump to the JSP page, forwarding and redirection
Account and Permission Management
ACL 和NAT
Spark source code-task submission process-6.1-sparkContext initialization-create spark driver side execution environment SparkEnv
Configuration of TensorFlow ObjecDetectionAPI under Anaconda3 of win10 system
有哪些事情是你做了运维才知道的?
[Day1] (Super detailed steps) Build a soft RAID disk array
Advantages of overseas servers









