Introspection:
It is the cutting edge of
java programming.
Introspection helps to get the attributes or property values of a qualified class during runtime.
Sample code has been given below to explain the core concepts of Introspection.
This code is in working condition, you can use it just by copying it.
Hope this would meet you need.
import java.beans.PropertyDescriptor;
import java.beans.Introspector;
import java.beans.IntrospectionException;
public class SampleClass
{
public void callSample()
{
SampleBean sample = new SampleBean();
//Object value=null;
try
{
PropertyDescriptor[] accessors = Introspector.getBeanInfo(sample.getClass()).getPropertyDescriptors();
System.out.println("Length>>>>>>>>>>>" +accessors.length);
for ( int i=0; i<accessors.length; i++)
{
System.out.println("Property Name >>>>>>>>>>>" +accessors[i].getDisplayName());
System.out.println("Read Method >>>>>>>>>>>" +accessors[i].getReadMethod());
System.out.println("Property Type >>>>>>>>>>>" +accessors[i].getPropertyType());
try
{
Object value = accessors[i].getReadMethod().invoke(sample,null);
System.out.println("The value is "+value.toString());
}
catch ( Throwable iae )
{
}
}
}
catch ( IntrospectionException ie )
{
ie.printStackTrace();
}
}
public static void main(
String a[])
{
SampleClass sc = new SampleClass();
sc.callSample();
}
}
// Seperate class
public class SampleBean
{
private String name="Inbakumar";
private String phoneNumber="12345678";
public String getName()
{
return name;
}
public String getPhoneNumber()
{
return phoneNumber;
}
}