Access private field/member java using reflection 2013

View previous topic View next topic Go down

Access private field/member java using reflection 2013

Post by Admin on Sun Sep 15, 2013 4:04 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on">We all know, in java we cannot access private members of a class outside that class. Private members are accessible only within the class.<br />In order to access private field/method or member outside the class, we need to use reflection in java.<br />Through reflection, private members are accessible outside the class also.<br /><br /><div class="separator" style="clear: both; text-align: center;">Before Reflection</div><div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-SzeOiTvWlxw/Uc7z7oMaxAI/AAAAAAAAAFE/s4hGcz-E14k/s557/BeforeReflection.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="123" src="http://3.bp.blogspot.com/-SzeOiTvWlxw/Uc7z7oMaxAI/AAAAAAAAAFE/s4hGcz-E14k/s640/BeforeReflection.JPG" width="640" /></a></div><div class="separator" style="clear: both; text-align: center;">After Reflection</div><div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-ygmArh-YbM0/Uc70M9eVHYI/AAAAAAAAAFM/wxY4hs6yOWs/s557/BeforeReflection.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="124" src="http://3.bp.blogspot.com/-ygmArh-YbM0/Uc70M9eVHYI/AAAAAAAAAFM/wxY4hs6yOWs/s640/BeforeReflection.JPG" width="640" /></a></div><br />As you can see clearly, before reflection only public member were visible. &nbsp;But after reflection all the fields are visible.<br /><br />Consider the below code, <br /><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package test; <br /> public class ReflectionPojo { <br /> private String name; <br /> private int age; <br /> } <br /></code></pre><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package test; <br /> import java.lang.reflect.Field; <br /> import java.lang.reflect.Modifier; <br /> public class AccessPrivateMembersMain { <br /> public static void main(String args[]) throws NoSuchFieldException, <br /> SecurityException, IllegalArgumentException, IllegalAccessException { <br /> ReflectionPojo reflectionPojo = new ReflectionPojo(); <br /> Field f = reflectionPojo.getClass().getDeclaredField("lastName"); <br /> f.setAccessible(true); <br /> f.set(reflectionPojo, "Kumar"); <br /> System.out.println(f.get(reflectionPojo)); <br /> } <br /> } <br /></code></pre>As shown above, we have ReflectionPojo class and it has 2 private fields, name and age.<br /><br />In order to use the fields in AccessPrivateMembersMain, we have to use reflection.<br />getDeclaredField() method throws 2 exceptions NoSuchFieldException, SecurityException and it is self-explanatory, like for example we pass a string to this method which is a field name of the class and if there is no field with that name in the class, it will throw NoSuchFieldException<br /><br />It throws IllegalArgumentException, because if we set a int value in a string field then that will go wrong.<br /><br /></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on">We all know, in java we cannot access private members of a class outside that class. Private members are accessible only within the class.<br />In order to access private field/method or member outside the class, we need to use reflection in java.<br />Through reflection, private members are accessible outside the class also.<br /><br /><div class="separator" style="clear: both; text-align: center;">Before Reflection</div><div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-SzeOiTvWlxw/Uc7z7oMaxAI/AAAAAAAAAFE/s4hGcz-E14k/s557/BeforeReflection.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="123" src="http://3.bp.blogspot.com/-SzeOiTvWlxw/Uc7z7oMaxAI/AAAAAAAAAFE/s4hGcz-E14k/s640/BeforeReflection.JPG" width="640" /></a></div><div class="separator" style="clear: both; text-align: center;">After Reflection</div><div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-ygmArh-YbM0/Uc70M9eVHYI/AAAAAAAAAFM/wxY4hs6yOWs/s557/BeforeReflection.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="124" src="http://3.bp.blogspot.com/-ygmArh-YbM0/Uc70M9eVHYI/AAAAAAAAAFM/wxY4hs6yOWs/s640/BeforeReflection.JPG" width="640" /></a></div><br />As you can see clearly, before reflection only public member were visible. &nbsp;But after reflection all the fields are visible.<br /><br />Consider the below code, <br /><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package test; <br /> public class ReflectionPojo { <br /> private String name; <br /> private int age; <br /> } <br /></code></pre><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package test; <br /> import java.lang.reflect.Field; <br /> import java.lang.reflect.Modifier; <br /> public class AccessPrivateMembersMain { <br /> public static void main(String args[]) throws NoSuchFieldException, <br /> SecurityException, IllegalArgumentException, IllegalAccessException { <br /> ReflectionPojo reflectionPojo = new ReflectionPojo(); <br /> Field f = reflectionPojo.getClass().getDeclaredField("lastName"); <br /> f.setAccessible(true); <br /> f.set(reflectionPojo, "Kumar"); <br /> System.out.println(f.get(reflectionPojo)); <br /> } <br /> } <br /></code></pre>As shown above, we have ReflectionPojo class and it has 2 private fields, name and age.<br /><br />In order to use the fields in AccessPrivateMembersMain, we have to use reflection.<br />getDeclaredField() method throws 2 exceptions NoSuchFieldException, SecurityException and it is self-explanatory, like for example we pass a string to this method which is a field name of the class and if there is no field with that name in the class, it will throw NoSuchFieldException<br /><br />It throws IllegalArgumentException, because if we set a int value in a string field then that will go wrong.<br /><br /></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum