MyArchiBook

Reflection – Dynamic Typing

with 4 comments

Today wanted to add some quick notes on my findings on Dynamic Languages.

To start with, lets look into Reflection.

REFLECTION” –  in common generic terms means a Mirror Image of something.

It carries the same meaning in Java. It means getting the mirror image of a class.
The microscopic details of a class can be explored during the runtime by specifying the class name dynamically. During this process the JVM interacts with the object of unknown type.

Let’s see an example,

public static void main(String[] args) {
	try {
		Class class1=Class.forName("aish.vaishno.musicstore.dao.MusicStoreDaoImpl");
		Package package1=class1.getPackage();
		System.out.println("Package Name with Version : "+package1);

		String canonicalName=class1.getCanonicalName();
		System.out.println("\nCanonical/Standard name of Field class: "+canonicalName);

		Method[] methods=class1.getMethods();
		System.out.println("\nMethods present in Field class:");
		for (int i = 0; i < methods.length; i++) {
			System.out.println(methods[i]);
		}

                Class[] classes=class1.getInterfaces();
                System.out.println("Interface implemented: ");
                for (int i = 0; i < classes.length; i++) {
                System.out.println(classes[i]);
                }
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
}

While specifying the className dynamically, the .class file of that particular class is opened and examined during the runtime .By Reflection, the package ,Method , Interface, modifier informations and etc.,can be retrieved.

But the demerits of this is ,

  • It slows down the performance
  • private fields and methods can be accessed.

Reflection concept is used in ,
Spring – while scanning through the packages.
ORM– when data from database is being mapped to the attributes in Entity Bean.
Likewise in Serialisation/Deserialisation, Remote Method Invocation and etc.,

Dynamic Language plays a major role in Rapid Application Development(RAD).

Groovy and Dynamic Language updates coming up.

 

Reference:

http://javarevisited.blogspot.com/2012/04/how-to-invoke-method-by-name-in-java.html

Advertisements

Written by thangaveluaishwarya

August 9, 2013 at 9:48 PM

4 Responses

Subscribe to comments with RSS.

  1. Very very deep and clear understanding you have. Every point is well revisited. Young people should understand to this level. Keep it up.

    Prem

    August 9, 2013 at 9:58 PM

  2. not many leave the reference url.
    Also the points are good

    Sachin

    August 12, 2013 at 12:08 PM

  3. […] Reflection – Dynamic Typing (aishwaryavaishno.wordpress.com) […]

  4. the pros and cons illustrated gives more clarity on its capabilities 🙂

    rajakumartu

    August 29, 2013 at 9:53 PM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: