Introspection
Java language is having a very important feature named as Introspection.
Using introspection we can get the
internal information of a class at run-time. We can use introspection in
developing applications that use plug-ins. A application can determine available
constructors, methods & fields of the plug-ins class and use this information at run-time. We
can also use introspection for the generation of Javadocs and for creating Java
Beans. In other words, Introspection is a
process of analyzing a bean's design patterns to discover the bean's
properties, events, and methods.
Use
of Introspection
Portability:
- We can write a component one and use them anywhere. No extra specification files that need
to be maintained independently from your component code and
there is no platform-specific
issues.
Reuse: - By following the JavaBeans design conventions,
implementing the appropriate
interfaces, and extending the appropriate classes,
we are making our component
reusable. The JavaBeans API supplies a set of classes and interfaces
to provide introspection. The package name is like :- java.beans.Introspector;
The Introspector class provides a standard way for tools to know about
the properties, events, and methods supported by a target Java Bean.
Difference between Reflection & Introspection
"Reflection" refers to the low-level API
with which we can discover "what are the methods of this class",
"what are the instance members are available
in the class", "what parameters does this method accept",
"what is the parent class of this class", and so on.
V/S
"Introspection" is a higher-level
concept; it generally uses the reflection API to know about a class. For
example, the javax.beans.Introspector class will give us a list of JavaBeans
properties of a class.
Sample program to use Introspection:-
package
com.gaurav.introspection;
import
java.beans.BeanDescriptor;
import
java.beans.BeanInfo;
import
java.beans.IntrospectionException;
import
java.beans.Introspector;
import
java.beans.MethodDescriptor;
import
java.beans.PropertyDescriptor;
public class
IntrospectionTestExample {
private int userId;
private String userName;
private double salary;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public static void main(String[] args) throws
IntrospectionException {
BeanInfo beanInfo = Introspector
.getBeanInfo(IntrospectionTestExample.class);
BeanDescriptor beanDescriptor =
beanInfo.getBeanDescriptor();
System.out
.println("Class
name is - " + beanDescriptor.getDisplayName());
System.out.println("**********
Property Names are **********");
for (PropertyDescriptor
propertyDescriptor : beanInfo
.getPropertyDescriptors())
System.out.println(propertyDescriptor.getName());
System.out.println("**********
Method Names are **********");
for (MethodDescriptor
methodDescriptor : beanInfo
.getMethodDescriptors())
System.out.println(methodDescriptor.getDisplayName());
}
}
Result :-
Note:- Here in this IntrospectionTestExample java file,
a class property is not declared. This property is
inherited
from the Object class, as it is displaying as a property of this class which we can see in the
above result figure.
Thanks Mate!
ReplyDelete