Java Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.
- Java – What is reflection, and why is it useful?
- Java Reflection Example
- Java Reflection Tutorial for Classes, Methods, Fields
- Take an in-depth look at the Java Reflection API
Uses of Reflection
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
- Extensibility Features: An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.
- Class Browsers and Visual Development Environments: A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.
- Debuggers and Test Tools: Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.
Another must read:
- Java Tips: What is the Fastest Way to Copy File in Java?
- Understanding Java Annotation – Annotation Examples
Below is a simple example on How to use Reflection to Call Java Method at Runtime?
package com.crunchify.tutorials; /** * @author Crunchify.com * */ public class CrunchifyReflectionTest { public void getCompany() { System.out.println("getCompany(): Value => no param"); } public void getCompanyName(String companyName) { System.out.println("getCompanyName(): Value => " + companyName); } public void getCompanyPhone(String companyPhone) { System.out.println("getCompanyPhone(): Value => " + companyPhone); } }
package com.crunchify.tutorials; import java.lang.reflect.Method; /** * @author Crunchify.com * */ public class CrunchifyReflectionExample { @SuppressWarnings("rawtypes") public static void main(String[] args) { // no paramater Class<?> noparams[] = {}; // String parameter Class[] paramString = new Class[1]; paramString[0] = String.class; try { // Load CrunchifyReflectionTest Class at runtime Class<?> cls = Class.forName("com.crunchify.tutorials.CrunchifyReflectionTest"); Object obj = cls.newInstance(); Method method = cls.getDeclaredMethod("getCompany", noparams); method.invoke(obj); method = cls.getDeclaredMethod("getCompanyName", paramString); method.invoke(obj, new String("Google")); method = cls.getDeclaredMethod("getCompanyPhone", paramString); method.invoke(obj, new String("408.111.1111")); } catch (Exception ex) { ex.printStackTrace(); } } }
Result:
getCompany(): Value => no param getCompanyName(): Value => Google getCompanyPhone(): Value => 408.111.1111
You may want to take a look at all Spring MVC and JQuery Examples.