Java Basics - Reflection

Write code to change Basin 2021-02-23 17:46:57
java basics reflection

Each class has one Class object , Contains information about the class . When compiling a new class , It's going to give you the same name .class file , The contents of this file are saved Class object .

Class loading is equivalent to Class Loading of objects , Classes are not loaded dynamically until they are first used JVM in . You can also use Class.forName("com.mysql.jdbc.Driver") This way controls the loading of classes , This method returns a Class object .

Reflection can provide run-time class information , And the class can be loaded at run time , Even at compile time .class You can load it in if it doesn't exist .

Class and java.lang.reflect Together they provide support for reflection ,java.lang.reflect The class library mainly contains the following three classes :

  • Field : have access to get() and set() Method to read and modify Field The fields associated with the object ;
  • Method : have access to invoke() Method invocation and Method Object association method ;
  • Constructor : It can be used Constructor Of newInstance() Create a new object .

Advantages of reflection :

  • Extensibility : Applications can create instances of extensible objects with fully qualified names , To use user-defined classes from the outside .
  • Class browser and visual development environment : A class browser needs to be able to enumerate the members of a class . Visual development environment ( Such as IDE) You can benefit from taking advantage of the type information available in reflection , To help programmers write the right code .
  • Debuggers and test tools : The debugger needs to be able to examine private members in a class . The test tool can use reflection to automatically invoke discoverable objects defined in the class API Definition , To ensure high code coverage in a set of tests .

Disadvantages of reflection :

Although reflection is very powerful , But it can't be abused . If a function can be done without reflection , Then it's best not to . When we use reflection technology , Here are a few things to keep in mind .

  • Performance overhead : Reflection involves the resolution of dynamic types , therefore JVM This code cannot be optimized . therefore , Reflection operations are much less efficient than those that are not . We should avoid using reflection in frequently executed code or in performance-intensive programs .
  • Security restrictions : Using reflection technology requires the program to run in an environment with no security constraints . If a program must run in an environment with security constraints , Such as Applet, So that's a problem .
  • Internal exposure : Reflection allows code to perform operations that would not normally be allowed ( Such as accessing private properties and methods ), So using reflection can cause unexpected side effects , This can cause code dysfunction and break portability . Reflection code breaks abstraction , So when the platform changes , The behavior of the code is likely to change as well .
  • Trail: The Reflection API
  • In depth analysis of Java Reflection (1)- Basics
本文为[Write code to change Basin]所创,转载请带上原文链接,感谢

  1. 开源办公开发平台丨Mysql5.7两套四节点主从结构环境搭建教程(二)
  2. 开源办公开发平台丨Mysql5.7两套四节点主从结构环境搭建教程(一)
  3. Open source office development platform mysql5.7 two sets of four node master-slave structure environment building tutorial (2)
  4. HTTP的“无状态”和REST的“状态转换”
  5. Open source office development platform mysql5.7 two sets of four node master-slave structure environment building tutorial (1)
  6. 【大数据哔哔集20210128】使用Hive计算环比和同比
  7. 【大数据哔哔集20210125】Kafka将逐步弃用对zookeeper的依赖
  8. 【大数据哔哔集20210124】有人问我Kafka Leader选举?我真没慌
  9. 【大数据哔哔集20210123】别问,问就是Kafka高可靠
  10. Spring 事务、异步和循环依赖有什么关系?
  11. Spring 动态代理时是如何解决循环依赖的?为什么要使用三级缓存?
  12. "Stateless" of HTTP and "state transition" of rest
  13. [big data bibiji 20210128] use hive to calculate month on month and year on year
  14. [big data bibiji 20210125] Kafka will gradually abandon its dependence on zookeeper
  15. [big data beeps 20210124] someone asked me about Kafka leader election? I'm not in a panic
  16. [big data bibiji 20210123] don't ask, ask is Kafka highly reliable
  17. jQuery Gantt Package 在Visual Studio中创建一个新的ASP.NET项目
  18. What is the relationship between spring transactions, asynchrony, and circular dependencies?
  19. How to solve circular dependency in spring dynamic proxy? Why use level 3 caching?
  20. Unicode码的二进制转换(Java)
  21. JAVA的函数式接口
  22. JAVA里的元祖类
  23. JQuery Gantt package to create a new ASP.NET project
  24. Binary conversion of Unicode code (Java)
  25. The functional interface of Java
  26. Yuanzu class in Java
  27. Java中的CPU占用高和内存占用高的问题排查
  28. HashMap连环问你能答出几道?
  29. IntelliJ IDEA 还能画思维导图,果然最强 IDE!
  30. java中的反射和代理
  31. Troubleshooting of high CPU and memory usage in Java
  32. How many questions can you answer?
  33. IntelliJ idea can also draw mind maps. It's really the strongest ide!
  34. Reflection and proxy in Java
  35. Java中的CPU占用高和内存占用高的问题排查
  36. Linux OOM(out of memory)
  37. mysql 自定义函数因参数名称报错
  38. Troubleshooting of high CPU and memory usage in Java
  39. Linux OOM(out of memory)
  40. MySQL user defined function error due to parameter name
  41. echarts-gl 3D 地图柱状图可视化GDP
  42. Visualization of histogram of ecarts GL 3D map
  43. 金三银四如何应对Redis面试,一文深入Redis实战实践!
  44. 阿里资深架构师定制金三银四面试整理出来的一份Java核心知识点.pdf
  45. 为什么Java开发工程师工资高,却很多人想转行?
  46. How to deal with the interview of redis!
  47. Ali senior architect customizes a Java core knowledge point sorted out in the interview of golden, silver and four.pdf
  48. Why do java development engineers have high salaries, but many people want to change careers?
  49. 用注解开发SpringMVC
  50. Developing spring MVC with annotations
  51. 编译redis报错/deps/hiredis/libhiredis.a解决
  52. Compile redis report error / DEPs / hirredis / libhirredis. A solution
  53. 用注解开发SpringMVC
  54. Developing spring MVC with annotations
  55. Spring学习笔记-01
  56. Centos安装和卸载docker
  57. Spring learning notes-01
  58. Installing and uninstalling docker in CentOS
  59. Java基础-异常
  60. Java基础-反射