This article gives you the inner power of reflection. Actually - TopicsExpress



          

This article gives you the inner power of reflection. Actually speaking there is no concept of hacking. It is just a tweak you can make to a java object through reflection. In this article I will show you how to break some of the object oriented concept using reflection. As you know reflection is the ability of the software to analyse itself. There are two ways you create an instance of a class. One way you can create an object directly using new operator of the class which is at your classpath. You can create an instance of a class using java.lang.reflect package of JDK. The syntax takes the forma as Class.forName ( ). Using reflection you can dynamically load the class. Sometime it is often asked in the technical interview that why it is required or what is the use although we can directly access the class object. Be sure to give specific answer. However I am going to give you the details of reflection. You can refer to any book on java or you can go to Sun site to get large tutorials on reflection. Technicalities I represent some of the use cases you can play with the java objects using reflection. Case-I : Accessing private fields As you know that you can not directly access the private fields declared in a class. It is a violation of the object oriented concept. But it is still possible using reflection. Of course it is a round about way and it is not a direct approach. It is just like a back door entry to get the values. The complete example is given below. p a c k a g e c om . d d s . c / * * * T h i s i s a n o rm * a l l t h e f i e l d s * * @a u t h o r D e b a d a * * / p u b l i c c l a s s Emp { p r i v a t e S p r i v a t e i p r i v a t e I p r i v a t e D p r i v a t e b } Although I have written the java docs for this class, still it is worth to explain you a little. In this above class “Emp” all the fields are declared private so that no body can access these outside the scope. Please refer to the following example where you can obtain the values. p a c k a g e c om . d d s . t i mp o r t j a v a . l a n g . / * * * T h i s i s a t e s t * Emp . T h i s c l a s * p r i v a t e f i e l d * * @a u t h o r D e b a d a * * / p u b l i c c l a s s Emp T { p u b l i c s t { t { } c { } } } The above class is a test harness example for the class “Emp”. This test harness class displays you the field name , filed type and along with the field values. Case-2 : Access the private methods It is also true that you can not access the private methods out side the scope of the class. It is also possible to get the methods values if present in the class using reflection. Think that there is a Lover who stores her age and secret password inside the method. Now the intelligent Male lover wants know her age and password. This is just an example. Please refer to the following example. p a c k a g e c om . d d s . c / * * * T h i s i s a c l a s * me t h o d s a r e p r * * @a u t h o r D e b a d a * * / p u b l i c c l a s s L o v e { p r i v a t e S { r } p r i v a t e i { r } p r i v a t e S { r } } In the above class all the methods are declared private. Still you obtain the values returning from the private methods. The following is the test harness class for the “Lover”. p a c k a g e c om . d d s . t i mp o r t j a v a . l a n g . / * * T h i s i s a t e s t * L o v e r . T h i s c l * o f t h e p r i v a t e * @a u t h o r D e b a d a * * / p u b l i c c l a s s L o v e { p u b l i c s t { t { } c { } } }
Posted on: Fri, 26 Dec 2014 17:47:29 +0000

Trending Topics



Recently Viewed Topics




© 2015