Home > DeveloperSection > Articles > Introduction to Reflection API in Java

Introduction to Reflection API in Java


Java Java  J2EE  Reflection  Eclipse 
Ratings:
0 Comment(s)
 614  View(s)
Rate this:

Introduction to Reflection API in Java

 

Introduction

If you have ever used IDE (integrated development environment) like Eclipse or IntelliJ Idea for java development , these IDE have numerous intelligence features like auto completion of methods name, field names etc. even before compiling the class , have you ever imagined what is happening behind the scene ?

The IDE is using reflection API somewhere in the background. It is a very powerful concept and has a very little use in normal programing but it’s a backbone for most of the Java and J2EE frameworks like Junit, Spring, Tomcat, Eclipse, Struts, and Hibernate etc.

 

Definition

·         Java Reflection facilitate us to inspect classes, interfaces, fields and methods at runtime, without knowing the name 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  

·         The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class.

·         The java.lang and java.lang.reflect packages provide classes for java reflection.

 

From Java Docs:

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.

Referred from : Trail: The Reflection API

 

Use of Reflection:

·         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 suit.

 

Java Reflection Example

Here is a quick Java Reflection example to show you what using reflection looks like:

Method[] methods = MyObject.class.getMethods();

for(Method method : methods){

    System.out.println("method = " + method.getName());

}

This example obtains the Class object from the class called MyObject. Using the class object the example gets a list of the methods in that class, iterates the methods and print out their names.

 

Drawbacks of Java Reflection

·         Poor Performance – Since reflection resolve the types dynamically, it involves processing like scanning the classpath to find the class to load, causing slow performance.

·         Security Restrictions – Reflection requires runtime permissions that might not be available for system running under security manager. This can cause you application to fail at runtime because of security manager.

·         Security Issues – Using reflection we can access part of code that we are not supposed to access, for example we can access private fields of a class and change its value. This can be a serious security threat and cause your application to behave abnormally.

·         High Maintenance – Reflection code is hard to understand and debug, also any issues with the code can’t be found at compile time because the classes might not be available, making it less flexible and hard to maintain.


Don't want to miss updates? Please click the below button!

Follow MindStick