Java Reflection Drawbacks And Use cases

The concept of reflection in software means the ability to inspect, analyze and modify other code at runtime.
In Java, it is possible to inspect fields, classes, methods, annotations, interfaces, etc. at runtime. You do not need to know how
classes or methods are called, neither the parameters that are needed, all of that can be retrieved at runtime using reflection. It is
also possible to instantiate new classes, to create new instances and to execute their methods, all of it using reflection.

Drawbacks
  • Poor Performance – Since java reflection resolve the types
    dynamically, it involves processing like scanning the classpath to
    find the class to load, causing slow performance.
  • Security Restrictions – Reflection requires runtime permissions that
    might not be available for system running under security manager.
    This can cause you application to fail at runtime because of security
    manager.
  • Security Issues – Using reflection we can access part of code that we
    are not supposed to access, for example we can access private fields
    of a class and change it’s value. This can be a serious security
    threat and cause your application to behave abnormally.
  • High Maintenance – Reflection code is hard to understand and debug,
    also any issues with the code can’t be found at compile time because
    the classes might not be available, making it less flexible and hard
    to maintain.
Use cases

Despite all the limitations, reflection is a very powerful tool in Java that can be taken into consideration in several scenarios.
In general, reflection can be used to observe and modify the behavior of a program at runtime. Here is a list with the most
common use cases:

  • IDEs can heavily make use of reflection in order to provide solutions for auto completion features, dynamic typing, hierarchy
    structures, etc. For example, IDEs like Eclipse or PHP Storm provide a mechanism to retrieve dynamically the arguments
    expected for a given method or a list of public methods starting by “get” for a given instance. All these are done using
    reflection.
  • Debuggers use reflection to inspect dynamically the code that is being executed.
  • Test tools like Junit or Mockito use reflection in order to invoke desired methods containing specific syntax or to mock specific
    classes, interfaces and methods.
  • Dependency injection frameworks use reflection to inject beans and properties at runtime and initialize all the context of an
    application.
  • Code analysis tools like PMD or Findbugs use reflection in order to analyze the code against the list of code violations that are
    currently configured.
  • External tools that make use of the code dynamically may use reflection as well
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值