OK so the first thing first.
What is Java Reflection ?
Java Reflection can be used to dynamically find java classes , locating and execute methods, access fields at run time.
You can find More on : http://docs.oracle.com/javase/tutorial/reflect/index.html
Here is the sample project that I did.
First I create a class called dynamic test and add one private field and four methods including one private methods. Here is the code and its straight forward.
package rd.test;
public class DynamicTest {
private int count;
public void printStar() {
System.out.println("*");
}
public void printStarWithInt(int i) {
System.out.println("* Integer : " + i);
}
public void printStarWithString(String param) {
System.out.println("* String : " + param);
}
public void printStarWithStrings(String param1, String param2) {
System.out.println("* String : " + param1 + " , and " + param2);
}
private void printStarInPrivate() {
System.out.println("* Private : *");
}
}
and here is the class with main method.
package rd;
import rd.test.DynamicTest;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException,
InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class cls = Class.forName("rd.test.DynamicTest");
Object obj = cls.newInstance();
/**
* calling printstart method without passing any parameters
* */
Method method = cls.getDeclaredMethod("printStar", null);
method.invoke(obj, null);
/**
* calling printStarWithInt method with int parameter
*/
Class[] intParam = new Class[1];
intParam[0] = Integer.TYPE;
method = cls.getDeclaredMethod("printStarWithInt", intParam);
method.invoke(obj, 100);
/**
* calling printStarWithStrings methos with two string parameters
*/
Class[] stringParams = new Class[2];
stringParams[0]=String.class;
stringParams[1]=String.class;
method = cls.getDeclaredMethod("printStarWithStrings", stringParams);
method.invoke(obj, "hi","rajith");
/**
* calling private method in DynamicTest class
*/
method = cls.getDeclaredMethod("printStarInPrivate", null);
method.setAccessible(true);
method.invoke(obj, null);
/**
* setting and getting private fileds
*/
DynamicTest dynamicTest = new DynamicTest();
Field field = cls.getDeclaredField("count");
field.setAccessible(true);
field.setInt(dynamicTest,10);
System.out.println(field.get(dynamicTest));
}
}
I explained most of the things with the comments. How to access private fields and methods, how to pass different type of parameters and all.
Excellent post!!! Selenium automation testing tool makes your software validation process lot simpler. Keep on updating your blog with such awesome information. Selenium Training in Chennai | Selenium Course in Chennai | Best Selenium training institute in Chennai
ReplyDeleteAssam 2564 Police Constable Recruitment 2016 Apply Online
ReplyDeleteYour way of describing the whole thing in this paragraph is actually pleasant, every one be able to effortlessly know it, Thanks a lot.........
Naval Dockyard Visakhapatnam Tradesman Skilled Recruitment 2016
ReplyDeleteI am thank full for your Good and clear information.........
Latest technology have created a greater impact over testing web applications. This vital in identifying important issues that raises in web appplications. Thanks for sharing this information in here. Keep blogging article like this.
ReplyDeleteSelenium training in chennai | Selenium training chennai | Best selenium training in chennai
Thanks for sharing use interview questions on .Net technology. While preparing for my job interview, your article helped me a lot to sharpen my skills and do well in my interview. One of trainer from leading dot net training in Chennai suggests me about your site.
ReplyDeleteGreat Article
ReplyDeleteOnline Java Training | Java EE course | Java Course in Chennai | Java Training in Chennai | Java Training Institutes in Chennai | J2EE Training in Chennai | java j2ee training institutes in chennai
Hello Admin,
ReplyDeleteAwesome Post! I like writing style, how you describing the topics throughout the post. I hope many web reader will keep reading your post at the end, Thanks for sharing your view.
Regards, SAS Training in Chennai
Thanks for sharing detailed information of unified functional testing automation tool. QTP Course in Chennai | QTP training
ReplyDeleteNice post..QTP Training in Chennai | QTP Training Institute in Chennai | QTP Course in Chennai
ReplyDeleteThis wordpress module is made for shippers who need to offer items from their 3dcart Online Store inside their WordPress blog .
ReplyDeletewordpress shop
WoW what a amazing post web page design IN ADDITION TO development solutions AS WELL AS wp-plugins, Android IOS, Android Applications Contact Us
ReplyDeleteJava Training in Chennai
ReplyDeleteOnline MVC Training India | Angularjs Training | javase j2ee javaee interview questions | Java Training in Chennai |
Java Training in CHennai
Quite Interesting post!!! Thanks for posting such a useful post. I wish to read your upcoming post to enhance my skill set, keep blogging.
ReplyDeleteRegards,
Software testing training institutes in chennai|Software testing training|Software training
I stand by your thoughts. It is important to make sure that the software application working precisely for the reason it originally designed for. Thus, load testing is mandatory for every application or system before deployment.
ReplyDeleteRegards,
Loadrunner Training in Chennai|hp loadrunner training in chennai|Loadrunner classes in Chennai|loadrunner
Thanks for Sharing the valuable information and thanks for sharing the wonderful article..We are glad to see such a wonderful article..
ReplyDeleteQTP Training in Chennai | QTP Training Institute in Chennai | QTP Training
ReplyDeleteGreat content thanks for sharing this informative blog which provided me technical information keep posting.
Selenium Training in Chennai | Selenium Testing Course in Chennai
I have been reading your blog posts. You blog posts are awesome. They provide good and extremely information which is more valuable. Best Selenium Training in Velachery is predominant famous for Selenium Automation Training and how to get the necessary knowledge about the technology and implement it in a effective way.
ReplyDeleteOur Android Training in Chennai aims to teach beginners and employees. Android is the fastest growing smart phone OS in the world today. Android Training in Chennai | Android Training in Tambaram | Android Training in Sholinganallur | Android Training in Chennai
ReplyDeleteNice tutorial regrading java programming. Your concept is very unique. Moreover, I am enjoying with your post.
ReplyDeletebest java certification
Your post is really awesome .it is very helpful for me to develop my skills in a right way
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Training in Bangalore
Software Testing Course in Coimbatore
Software Testing Training in Madurai
Software Testing Training Institute in Bangalore
Software Testing Course in Bangalore
Testing Course in Bangalore
Ethical hacking course in bangalore
your blog' s design is simple and clean and i like it. Your blog posts about Online writing Help are superb. Please keep them coming. Greets!
ReplyDeleteBest Tableau Training Institute in Pune
Apache Spark Training Institute in Pune
make your point known to millions of people
ReplyDeleteentertainmentbee.com
themoviesbio.com
petrefine.com
thepetsabout.com
happylifestyletrends.com
People and businesses have grown from being virtual unknowns to worldwide phenoms
ReplyDeletefrasesparaenamorarz.com
fras-es.com
punaisesdelit.org
groupe-sanguine.fr
meilleur-gps.fr