wood burning stoves 2.0*
The moose likes Java in General and the fly likes what is introspection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what is introspection?" Watch "what is introspection?" New topic
Author

what is introspection?

pinakin raval
Greenhorn

Joined: Jun 28, 2005
Posts: 5
hi friend, pl'z help me
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30146
    
150

Pinakin,
Welcome to JavaRanch!

The JavaDoc provides a good description of Introspection. Were you looking for anything more specific?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Originally posted by pinakin raval:
hi friend, pl'z help me


Answer: not an advanced question.
Inba Kumar
Greenhorn

Joined: Jul 06, 2005
Posts: 1
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

Joined: Sep 19, 2004
Posts: 531
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

Joined: Sep 19, 2004
Posts: 531
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)
 
 
subject: what is introspection?