Saturday, August 10, 2013
In our last post we were only concerned in retrieving the declaration details of any class. Today I will go further and try to sniff inside any class. So today I will show you how you can retrieve all the constructors, fields and methods of any class using Java Reflection. For now we will only try to discover all the public members of a class and deal with private ones later. The java.lang.Class provides suitable methods for discovering the members. Here in our sample code we will first take as input the concerned class name and load it using Class.forName(). Then in order to get all its public constructors we vwill use getConstructors(), for fields use getFields() and for methods use getMethods(). An important thing to mention here is that these will return all the public members declared in that class as well as those inherited from its parent. You wont be able to retrieve the private members declarec using these methods. In order to get private members also use getDeclaredConstructors(), getDeclaredFields() and getDeclaredMethods(); but in this case you wont be able to get the inherited members.
--------------------------------------------------------------------------------------------------------------------------
import static java.lang.System.out;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ClassSniffer {
public static void main(String[] args) {
out.print("Enter Class Name : ");
String cname=System.console().readLine();
try {
Class c=Class.forName(cname);
out.println("\n------OUTPUT------");
//getting all constructors
out.println("\nConstructors : ");
Constructor<?> cons[]=c.getConstructors();
for(Constructor<?> con : cons)
out.println(" "+con);
//getting all fields
out.println("\nFields : ");
Field fs[]=c.getFields();
if(fs.length>0)
for(Field f : fs)
out.println(" "+f);
else
out.println(" --No Fields Declared--");
//getting all methods
out.println("\nMethods : ");
Method ms[]=c.getMethods();
for(Method m : ms)
out.println(" "+m);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------import static java.lang.System.out;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ClassSniffer {
public static void main(String[] args) {
out.print("Enter Class Name : ");
String cname=System.console().readLine();
try {
Class c=Class.forName(cname);
out.println("\n------OUTPUT------");
//getting all constructors
out.println("\nConstructors : ");
Constructor<?> cons[]=c.getConstructors();
for(Constructor<?> con : cons)
out.println(" "+con);
//getting all fields
out.println("\nFields : ");
Field fs[]=c.getFields();
if(fs.length>0)
for(Field f : fs)
out.println(" "+f);
else
out.println(" --No Fields Declared--");
//getting all methods
out.println("\nMethods : ");
Method ms[]=c.getMethods();
for(Method m : ms)
out.println(" "+m);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------------------------------------------------
Output
--------------------------------------------------------------------------------------------------------------------------
Enter Class Name : ClassSniffer
------OUTPUT------
Constructors :
public ClassSniffer()
Fields :
--No Fields Declared--
Methods :
public static void ClassSniffer.main(java.lang.String[])
public final void java.lang.Object.wait(long,int) throws java.lang.Interrupted
Exception
public final native void java.lang.Object.wait(long) throws java.lang.Interrup
tedException
public final void java.lang.Object.wait() throws java.lang.InterruptedExceptio
n
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
NOTE : Here you can see that the methods inherited from Object class are also listed. The name of the class is mentioned just before the method name which shows the methods inherited from other class and those declared in the class itself. Try running this code with getDeclaredConstructors(), getDeclaredFields() and getDeclaredMethods() instead and see the output.
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
Labels:Reflection
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment