469,362 Members | 2,548 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

Problem get the value of a field in the Reflection API

I have written a static method that uses the Reflection API to scrape
all of the names of the instance variables in a class and their
corresponding values; and then returns a hashMap containing a
key/value pair of the name of the variable and its corresponding
value. My problem is getting the value of the field, and everything I
have seen on usenet and in the javadoc tells me I am doing the right
thing. I think I found a bug in the JDK, but thought I'd throw it out
there to see if I am doing something wrong.

When I use the reflect API, specifically Field.get(Object o), it
returns the name of the variable, not the value contained in the
variable.

Note: The object (params) that I am passing in contains only primitive
types (double, int, etc.). I am using Java(TM) 2 Runtime Environment,
Standard Edition (build 1.4.2_03-b02).

Here is the code

public static HashMap toHash(Object params)
{
try
{
HashMap desc = new HashMap();
Class c = params.getClass();
Field[] fields = c.getFields();
for (int i = 0; i < fields.length; i++)
{
String key = fields[i].getName(); //PROBLEM #1: field name is in
ALL CAPS
Object value = fields[i].get(params); //PROBLEM #2: returns the
name of the field, not it's value desc.put(key, value);
}
return desc;
}
catch (SecurityException e)
{
MiscUtils.write("Unable to convert parameters to hash. " +
e.getMessage(), SystemInfo.ERROR_MESSAGE_TYPE);
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
MiscUtils.write("Unable to convert parameters to hash. " +
e.getMessage(), SystemInfo.ERROR_MESSAGE_TYPE);
e.printStackTrace();
}
catch (IllegalAccessException e)
{
MiscUtils.write("Unable to convert parameters to hash. " +
e.getMessage(), SystemInfo.ERROR_MESSAGE_TYPE);
e.printStackTrace();
}
return null;
}

Any ideas?
Jul 17 '05 #1
3 10778
nos

"Richard" <rv********@voneconsulting.com> wrote in message
news:d7**************************@posting.google.c om...
I have written a static method that uses the Reflection API to scrape
all of the names of the instance variables in a class and their
corresponding values; and then returns a hashMap containing a
key/value pair of the name of the variable and its corresponding
value. My problem is getting the value of the field, and everything I
have seen on usenet and in the javadoc tells me I am doing the right
thing. I think I found a bug in the JDK, but thought I'd throw it out
there to see if I am doing something wrong.

When I use the reflect API, specifically Field.get(Object o), it
returns the name of the variable, not the value contained in the
variable.

Note: The object (params) that I am passing in contains only primitive
types (double, int, etc.). I am using Java(TM) 2 Runtime Environment,
Standard Edition (build 1.4.2_03-b02).

Here is the code

public static HashMap toHash(Object params)
{
try
{
HashMap desc = new HashMap();
Class c = params.getClass();
Field[] fields = c.getFields();
for (int i = 0; i < fields.length; i++)
{
String key = fields[i].getName(); //PROBLEM #1: field name is in
ALL CAPS
Object value = fields[i].get(params); //PROBLEM #2: returns the
name of the field, not it's value desc.put(key, value);
}
return desc;
}
catch (SecurityException e)
{
MiscUtils.write("Unable to convert parameters to hash. " +
e.getMessage(), SystemInfo.ERROR_MESSAGE_TYPE);
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
MiscUtils.write("Unable to convert parameters to hash. " +
e.getMessage(), SystemInfo.ERROR_MESSAGE_TYPE);
e.printStackTrace();
}
catch (IllegalAccessException e)
{
MiscUtils.write("Unable to convert parameters to hash. " +
e.getMessage(), SystemInfo.ERROR_MESSAGE_TYPE);
e.printStackTrace();
}
return null;
}

Any ideas?

I do this (need to instantiate the object first)
then printout of the field name has the correct
upper/lower case. I did not try the value.
-----------------
CarlPlot cp = new CarlPlot(500, 500);
className = cp.getClass();

Field[] fields;
if (showall)
fields = cn.getFields();
else
fields = cn.getDeclaredFields();
Jul 17 '05 #2
Nic
If you use javaBeans and javaBeanInfo you can make it.

//Istance of my bean class
Myclass istanceOfMyClass = new MyClass();
//The Class of my class
Class classOfMyClass=Myclass.class;
//the BeanInfo: contains names and descriptions of the bean (if you
use Forte or NetBeans it's easy to do)
BeanInfo beanInfoOfMyClass =
Introspector.getBeanInfo(istanceOfMyClass);
PropertyDescriptor propertiesOfMyClass[] =
beanInfoOfMyClass.getPropertyDescriptors();

for(int i=0;i<propertiesOfMyClass.length;i++){
Method getter = propertiesOfMyClass[i].getReadMethod();
Method setter = propertiesOfMyClass[i].getWriteMethod();
Object value = getter.invoke(istanceOfMyClass, new Object[]{});
}

I hope I help you!

By.

Nic
Jul 17 '05 #3
oops. The original code works fine. I was making a stupid mistake.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Simang | last post: by
2 posts views Thread by Daniel | last post: by
4 posts views Thread by Nicolas | last post: by
8 posts views Thread by yo_mismo | last post: by
5 posts views Thread by Tomislav Bartolin | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.