Friday, September 20

Java Reflection API , Refection advantages in java , Java Refection introspection basics.

Java Reflection API , Refection advantages in java , Java Refection introspection basics.




                                                                  Java Refection API

Refection is useful where to don't want to write a logic repeatedly many times for similar kind of object structure creation .

Reflection will reduce the overall code drastically and help in maintaining a generic code for multiple modules.

Refection directly operates on java meta data , Meta data of class like class name , constructor , methods , variable and thus provide a generic way of writing logic for multiple similar object structures

Refection provide you capability of introspecting and invoking operations by searching the suitable method at run-time.

For Example :

public class JavaReflection{


public static void PrintClassDetailsObject obj)

 Class type = obj.getClass();
 final Method[] methods = type.getMethods();

if(type instanceOf Politician)
foreach (Method method: methods )
if(method.equalsIgnorecase(ContentElection)

method();

)

if(type instanceOf Doctor
foreach (Method method: methods )
if(method.equalsIgnorecaseoperatePatient

method();

)

if(type instanceOf Actor
foreach (Method method: methods )
if(method.equalsIgnorecase(playRole)

method();

)
}
As illustrated in above example at run-time It is being identified which class does the object belongs to and depending upon that a specific method is searched out. If method is available then required operation is performed. There are lot of If ,else conditions but good thing is that we are not writing there different classes to handle different sent of features .


So if there are 1000 variations of the above implementation all can be easily plugged in here .So you are writing little code catering to wide range of features and introducing generic behavior which further makes it extensible with very little extra code and effort As the basic structure and design for any similar enhancement is already in place. No further investment of design for enhancement of similar nature But Just the core implementation effort. 

Refection class here is creating an execution blueprint . It does not have feature specific logic or code But in certain cases this kind of implementation is useful when we want to provide a centralized control of routing the request to different classes.



Reflection also has got the capability of handling overloaded methods. At run-time we can check parameters passed along with the method name to resolve the exact method .

For example :

 for (int idx = 0; idx < methods.length; idx++) {
     if (methods[idx].getName() .startsWith"PLAY"{
 if (methods[idx].getParameterTypes()[0] == String.class) {
 methods[idx].invoke(obj, new Object[] { new String() }
);
}}










Top 3 most used classes of Refection API in java


1. Class java.lang.reflect.Method

 
This class contains a description of a method and the means to dynamically invoke
it. You can use instances of this class to dynamically invoke methods.


2. Class java.lang.reflect.Modifier


  Using Modifier , you can determine whether a field is public,private,static,transient , and so on.

3. Class java.lang.reflect.AccessibleObject

 It allows you to bypass class access constraints such as “private,” which allows you to
execute methods and set fields that would normally be blocked by visibility specifiers. Visibility of private fields can be modified to make it accessible at run-time.





No comments:

Post a Comment