USING JAVA REFLECTOR API

Thursday, February 9, 2012 0 comments
JAVA REFLECTOR API 


Do you want to know how Java IDE knows about the functions name available in a class file?
Do you want to want to know dynamically about the functions that are publicly available in a Java class file.If the ans is Yes, You have to learn about Java Reflector API.If you ever tried to make an JAVA IDE this features can be very helpful in IDE.

Although there are lots of work of this API but I would just show you how you can display the available functions of a class

First you have to know that every class will have invisible import of a class file "java.lang.Object". Even if you won't import this class file manually it will be added to your class file. This class file gives you authority or power to create the object which is the base of Object Oriented language.

Now Lets move to the code
First create a class for testing or you can just use your already created class file. Here I have create a Test Class.Here I have made two function one is public and another is private.As we know that private functions are not visible to other class. so for checking I created a private function.
Now Lets create a actual class that extracts the method/function names of the class.
Here I have imported the Reflect class, using this class I will be able to know the names of available function in any class.
Look at the first statement of the main function. There an array of class Method is created.We store t all the functions of the class(the class of which we need the info).
In-order to access the function/methods name of class we will do like this.
Method[] variable_name=Class_Name.class.getMethods()
In place of Class_Name provide the the name of class for which you need info!!!!!!!!!

Later we just print the array using for loop.


Here's the output of the class
As I have said it print all the available function of the class + function of java.lang.object class.
You can see here!!!!!!!!!!!!!!!!!!!!!!!!!!




0 comments:

Post a Comment

Recent Posts

WebInfo

 

©Copyright 2011 WebInfo