Overview of Java Reflection API

Overview of Reflection

Java Reflection is a process of examining or modifying the runtime behavior of a class at runtime.
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language.
Java Reflection makes it possible to inspect classes, interfaces, fields, and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.
This guide will get into Java reflection in depth. It will explain the basics of Java Reflection for classes, methods, fields, constructors, enums, useful Reflection Utility methods and Reflection Utility class.

Drawbacks of Reflection

Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.

1. Performance Overhead

Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations cannot be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts and should be avoided in sections of code which are called frequently in performance-sensitive applications.

2. Security Restrictions

Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.

3. Exposure of Internals

Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. The reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.
  • The common uses of reflection for accessing and manipulating classes, fields, methods, and constructors.
  • The java.lang.Class provides many methods that can be used to get metadata, examine and change the runtime behavior of a class.
  • The java.lang and java.lang.reflect packages provide classes for java reflection.
In this guide, we will learn the reflection for accessing and manipulating classes, fields, methods, and constructors.

Where it is used

The Reflection API is mainly used in:
  • IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc.
  • Debugger
  • Test Tools etc.
Some of the frameworks that use java reflection are:
  • JUnit – uses reflection to parse @Test annotation to get the test methods and then invoke it.
  • Spring – dependency injection, read more at Spring Dependency Injection
  • Tomcat web container to forward the request to correct module by parsing their web.xml files and request URI.
  • Eclipse auto-completion of method names
  • Struts
  • Hibernate

Java Reflection API Guide

This guide will get into Java reflection in depth. It will explain the basics of Java Reflection for classes, methods, fields, constructors, enums, useful Reflection Utility methods and Reflection Utility class. This guide covers all the Java reflection concepts with examples.

Java Reflection for Classes

These are Java Reflection APIs to get metadata of Class.
  • Get Class Object
  • Get Super Class
  • Get Public Member Classes
  • Get Declared Classes
  • Get Declaring Class
  • Getting Package Name
  • Getting Class Modifiers
  • Get Type Parameters
  • Get Implemented Interfaces
  • Get All Public Methods
  • Get All Public Constructors
  • Get All Public Fields
  • Get All Annotations
The java.lang.reflect.Method class provides APIs to access information about a method's modifiers, return type, parameters, annotations, and thrown exceptions. It also be used to invoke methods.
  • Obtaining Method Type Information
  • Obtaining Names of Method Parameters
  • Retrieving and Parsing Method Modifiers
  • Get Public Method
  • Invoking Public Method
  • Get All Public Methods
Reflection API provides several methods to analyze Class fields and modify their values at runtime, in this guide, we will look into some of the commonly used reflection functions for methods.
  • Get All Public Fields
  • Get Public Field
  • Field Declaring Class
  • Get Field Type
  • Get/Set Public Field Value
  • Get/Set Private Field Value
A constructor is used in the creation of an object that is an instance of a class. Typically it performs operations required to initialize the class before methods are invoked or fields are accessed. Constructors are never inherited.
  • Finding Constructors
  • Retrieving and Parsing Constructor Modifiers
  • Creating New Class Instances
  • Get Constructor
  • Get Constructor Information
Arrays have a component type and a length (which is not part of the type). Arrays may be manipulated either in their entirety or component by component. Reflection provides the java.lang.reflect.Array class.
  • Identifying Array Types - describes how to determine if a class member is a field of array type
  • Creating New Arrays - illustrates how to create new instances of arrays with simple and complex component types
  • Getting and Setting Arrays and Their Components - shows how to access fields of type array and individually access array elements
1. Find Field
2. Set Field
3. Get Field
4. Make the Given Field Accessible
5. Check Field "public static final" constant
6. Get Fields Including Super Classes
7. Find Method
8. Invoke the specified Method
9. Check method is an "equals" method
10. Check method is an "hashCode" method
11. Check method is an "toString" method
12. Make the Given Method Accessible
13. Get All Setter Methods
14. Check if Class is Public
15. Check if Method or Field or Constructor is Public
16. Check If Class is Private
17. Check If Method or Field or Constructor is Private
18. Check if Method or Field or Constructor is not Private
19. Check if the Class is Abstract
20. Check if Method or Field or Constructor Abstract
21. Check if the Class is Static
22. Check if Method or Field or Constructor are static
23. Check if the Class is Inner Class
24. Check if the Object is an Array
25. Make the Given Constructor Accessible

Comments