Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is introspection?

 
pinakin raval
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friend, pl'z help me
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pinakin,
Welcome to JavaRanch!

The JavaDoc provides a good description of Introspection. Were you looking for anything more specific?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is more fun to introspection! You can learn a lot about the constructors, methods and variables of any class, not just JavaBeans, by starting with a Class object. You can get one of those in (at least) two ways:

Class c = Class.forName( fully.qualified.className );
Class c = anyObject.getClass();

Look in the Class javadoc for getConstructors, getFields, getMethods, etc. HERE is a toy program I made to educate myself in reflection. The Reflector class might be interesting. Or just strange.
[ June 30, 2005: Message edited by: Stan James ]
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pinakin raval:
hi friend, pl'z help me


Answer: not an advanced question.
 
Inba Kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}

}
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pinakin raval:
hi friend, pl'z help me


There's a forum called "Java in General" that would be ideal for this question.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rick O'Shay:


There's a forum called "Java in General" that would be ideal for this question.


I should add: (Beginner)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic