By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

Problem get the value of a field in the Reflection API

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.