2. GOALS
1. What Is Reflection
2. Reflection Primitives in Java
3. Overview of Java’s reflection APIs
4. Advantages of Reflection
5. Problems with Reflection
6. Summary
3. WHAT IS REFLECTION?
When you look in a mirror:
- You can see your reflection
- You can act on what you see, for example, straighten your tie
In computer programming:
- Reflection is infrastructure enabling a program can see and
manipulate itself
- It consists of metadata plus operations to manipulate the metadata
Meta means self-referential
- So metadata is data (information) about oneself
4. REFLECTION PRIMITIVES IN JAVA
Java stores metadata in classes
- Metadata for a class: java.lang.Class
- Metadata for a constructor: java.lang.reflect.Constructor
- Metadata for a field: java.lang.reflect.Field
- Metadata for a method: java.lang.reflect.Method
Two ways to access a Class object for a class:
Class c1 = Class.forName(“java.util.Properties”);
Object obj = ...;
Class c2 = obj.getClass();
7. ADVANTAGES OF REFLECTION
1. Signature-Based Polymorphism
Reflection provides an alternative where we can invoke methods
having same signature, from different classes not have a common
interface (which is required in interface based polymorphism).
2. Inspecting and Manipulating Classes
Writing direct code is based on depending on specific class
details (like interface, method or field names) known at compile-
time. If we need to write code that depends on class details
known only at runtime, then reflection comes handy.
3. Creating Adaptable and Flexible Solutions
Because of its dynamic nature, reflection is useful for creating
adaptable and flexible software that is often not possible
otherwise.
8. 4. Writing Tools that Require Implementation Details
Many of the developer tools need access to internal or
implementation details of the code. For example, an intelligent
text editor can query a class using reflection and get details about
its implementation, which can be very helpful. When user types in
an object name and a „.‟ to access a member, the text editor can
pop with a list box with list of accessible members (obtained using
reflection) that the programmer can select from.
9. PROBLEMS WITH REFLECTION
1. Exposes implementation details.
One of the major objectives of object oriented programming is
information hiding, where only the relevant higher level details of
the class that the programmer needs to know are exposed to the
users, and low-level implementation details are hidden. Reflection
exposes such low-level implementation details of the class to the
program.
2. Performance
The code that is reflective is slow that the direct code that
performs the same functionality. Reflection can be order of times
(10 to 30 times) slower than the direct code.
10. 3. Security
there are some security concerns in using reflection, particularly in
component and internet programming where the code might be
from un-trusted sources.
Reflection does provide some limited ways to circumvent many of
the checks that are usually statically checked. For example, in
Java, private members are accessible only to the class members.
If we attempt to access a private member from code outside the
class, it will result in an exception. But by setting the
suppressAccessChecks method (provided in
java.lang.reflect.ReflectPermission class), access checks can be
switched off for that class. So, you can access all members from
outside the class using reflection.
11. SUMMARY
Reflection is a powerful language feature.
With reflection, it is possible to do two basic types of operations
on code: inspection and manipulation. Reflection enables using
„signature based polymorphism‟ as an alternative to „interface
based polymorphism‟ in Java.
Reflection also facilitates creation of flexible and adaptable
frameworks and patterns, which is not usually possible with direct
code. Though there are many clear advantages in using
reflection, like any other language feature, reflection can be
misused.