473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 10971
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simang | last post by:
Hi everyone, I have a structure with this format: Public Structure Forms Public _01234 as string Public _04321 as string Public _03456 as integer End Structure As you can see, the variable...
2
by: Daniel | last post by:
Hello, i've created an old-ADO-recordset in C# via the following code: ADODB.RecordsetClass arTest; arTest = new ADODB.RecordsetClass(); arTest.Fields.Append("Field1",
4
by: Nicolas | last post by:
The lat part is not working why ???????? Please help......... using System; namespace ConsoleApplication4 {
3
by: dan | last post by:
Hi all, I have a reflection-problem I'm totally stuck with. Maybe someone has a hint... I want to get a fieldinformation of an event from the Control class, e.g. "TextChanged". FieldInfo fi =...
1
by: Mudassar | last post by:
i want to get the property value using reflection. Scenerio: i have a status bar on MDI form. it has property named "Panels" and i want to get a specific panel from that panels collection using...
5
by: rettigcd | last post by:
I have several classes that all have the same static member: class A{ public static string Table = "TableA"; } class B{ public static string Table = "TableB"; }
8
by: yo_mismo | last post by:
Hi, I send a parameter from a Form (Form1) to an other form (Form2): Form2 frm2 = new Form2(); frm2.number_frm2 = number_frm1; frm2.Show(); The problem i got is that the variable...
5
by: Tomislav Bartolin | last post by:
Hi, Is it possible to set value to a read-only property via reflection? Thx -- Bartol -- Remove "TB" from address to reply
3
by: Steve Amey | last post by:
Hi all I am using reflection to read the values of properties from a class. The class is returned from a Web Service so I have to access the class using FieldInfo (Using VS 2003 which converts...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.