GOALS1. What Is Reflection2. Reflection Primitives in Java3. Overview of Java’s reflection APIs4. Advantages of Reflection5. Problems with Reflection6. Summary
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 tieIn computer programming:- Reflection is infrastructure enabling a program can see andmanipulate itself- It consists of metadata plus operations to manipulate the metadataMeta means self-referential- So metadata is data (information) about oneself
REFLECTION PRIMITIVES IN JAVAJava 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.MethodTwo ways to access a Class object for a class:Class c1 = Class.forName(“java.util.Properties”);Object obj = ...;Class c2 = obj.getClass();
ADVANTAGES OF REFLECTION1. Signature-Based PolymorphismReflection provides an alternative where we can invoke methodshaving same signature, from different classes not have a commoninterface (which is required in interface based polymorphism).2. Inspecting and Manipulating ClassesWriting direct code is based on depending on specific classdetails (like interface, method or field names) known at compile-time. If we need to write code that depends on class detailsknown only at runtime, then reflection comes handy.3. Creating Adaptable and Flexible SolutionsBecause of its dynamic nature, reflection is useful for creatingadaptable and flexible software that is often not possibleotherwise.
4. Writing Tools that Require Implementation DetailsMany of the developer tools need access to internal orimplementation details of the code. For example, an intelligenttext editor can query a class using reflection and get details aboutits implementation, which can be very helpful. When user types inan object name and a „.‟ to access a member, the text editor canpop with a list box with list of accessible members (obtained usingreflection) that the programmer can select from.
PROBLEMS WITH REFLECTION1. Exposes implementation details.One of the major objectives of object oriented programming isinformation hiding, where only the relevant higher level details ofthe class that the programmer needs to know are exposed to theusers, and low-level implementation details are hidden. Reflectionexposes such low-level implementation details of the class to theprogram.2. PerformanceThe code that is reflective is slow that the direct code thatperforms the same functionality. Reflection can be order of times(10 to 30 times) slower than the direct code.
3. Securitythere are some security concerns in using reflection, particularly incomponent and internet programming where the code might befrom un-trusted sources.Reflection does provide some limited ways to circumvent many ofthe checks that are usually statically checked. For example, inJava, private members are accessible only to the class members.If we attempt to access a private member from code outside theclass, it will result in an exception. But by setting thesuppressAccessChecks method (provided injava.lang.reflect.ReflectPermission class), access checks can beswitched off for that class. So, you can access all members fromoutside the class using reflection.
SUMMARYReflection is a powerful language feature.With reflection, it is possible to do two basic types of operationson code: inspection and manipulation. Reflection enables using„signature based polymorphism‟ as an alternative to „interfacebased polymorphism‟ in Java.Reflection also facilitates creation of flexible and adaptableframeworks and patterns, which is not usually possible with directcode. Though there are many clear advantages in usingreflection, like any other language feature, reflection can bemisused.