Reflection API in Java
Java reflection basically provides the facility to inspect or examine and modifying the run-time behavior of a class or application at run-time.
In other words, Reflection is used to find out the details of any class at run-time. It is the most powerful concept and it is mostly used for backend in any application, not in the front end for the application.
It provides the facility to analyze the behavior of a class at run-time.
The java.lang.reflect and java.lang package provides the classes and interfaces to use for reflection.
The java.lang.Class class provides some methods that can be used to get the metadata i.e data about data, examine, modifying the run-time behavior of a class.
Class class in java reflection is a final class and extends the super class of java i.e Object class and implements many interfaces like Serialization, Type, GenericDeclaration, and AnnotatedElement.
Instances of the class Class represent classes and interface in a running java application.
An enum is a kind of class and annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element types and number of dimension.
All the java primitives types(byte, short, char, int, long, float, double, and boolean) and the keyword void also represented as a Class object.
Class class has no public constructor.
The java.lang.Class class provides some methods that can be used to get the metadata i.e data about data, examine, modifying the run-time behavior of a class.
Uses of Reflection Java
The reflection API in java can be used in many areas like
- Developing IDE(Integrated Development Environment) e.g MyEclipse, Eclipse, and NetBeans etc.
- For debugging and Test tools.
- Loading Drivers and providing dynamic information.
Drawbacks of Reflection In Java
There are some drawbacks of reflection in java and these are given below.
- Poor performance.
- Security risk.
- High maintenance.
- Oops concept violation.
Reflection in java is not used in normal programming but it is mostly used in java and j2ee frameworks e.g Hibernate framework, Struts framework, JUnit, etc.
Java.lang.Class class
The Class class belongs to the java.lang package. Class class provides method for (1) get the metadata of a class at run-time and (2) examine and change the behavior of a class at run-time.Class class in java reflection is a final class and extends the super class of java i.e Object class and implements many interfaces like Serialization, Type, GenericDeclaration, and AnnotatedElement.
Instances of the class Class represent classes and interface in a running java application.
An enum is a kind of class and annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element types and number of dimension.
All the java primitives types(byte, short, char, int, long, float, double, and boolean) and the keyword void also represented as a Class object.
Class class has no public constructor.
There are Some Methods of Class class
There are given below some commonly used method of Class class.
(1) public String getName()
This method returns the name of the entity(class, interface, array class, primitive types and void) represented by this Class's object in the form of String.
(2) pubic static Class forName(String classname)throws ClassNotFoundException
This method loads the class and returns the reference of Class class.
(3) public boolean isInterface()
This method checks if it interfaces or not.
(4) public boolean isArray()
This method checks if it is array or not.
(5) public boolean isPrimitive()
This method checks if it is primitive or not.
(6) public Class getSuperclass()
This method returns the super class reference.
(7) public Object newInstance()throws InstantiationException, IllegalAccessException
Creates a new instance.
(8) public ClassLoader getClassLoader()
Returns the class loader for the class.
(9) public int getModifiers()
This method returns the java modifiers for this class or interface, encoded in an integer.
(10) public Method[] getDeclaredMethods()throws SecurityException
Returns the total number of methods of this class.
How to Get the Object of Class class
There are 3 methods to get the instance of Class class, They are
- forName() method of Class class
- getClass() method of Object class
- .class syntax
1) forName() Method of Class class
class Test
{
public static void main(String args[])throws ClassNotFoundException
{
Class c = Class.forName("Test");
System.out.println(c.getName());
}
}
Output : Test
In the above example, We have used forName() method, forName() method should be used if you know the fully qualified name of the class. It can't be used for primitive types.
2) getClass() Method of Object class
This method returns the instance of a Class class.
class Demo
{}
class Test
{
void showName(Object obj)
{
Class c = obj.getClass();
System.out.println(c.getName());
}
public static void main(String args[])
{
Demo d = new Demo();
Test t = new Test();
t.showName(d);
}
}
Output : Demo
3) isInterface() Method of Class class
interface My
{}
class My1
{}
class Test
{
public static void main(String args[])
{
try
{
Class c = Class.forName("My");
System.out.println(c.isInterface());
Class c1 = Class.forName("My1");
System.out.println(c1.isInterface());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output : true
0 comments:
Post a Comment