my dog learned polymorphism*
The moose likes Java in General and the fly likes Reflection to set ArrayList attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reflection to set ArrayList attribute" Watch "Reflection to set ArrayList attribute" New topic

Reflection to set ArrayList attribute

Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Hello All,
I have an instance variable labels which is of type ArrayList. I'm using the Reflection API to dynamically set instance variables, which is no problem, until it comes to labels.

Assume I have a String which contains attribute-value pairs, which is fed to the Reflection mechanism. The String is in the following format:

Essentially, the Reflection mechanism will look up the Field labels and call the set method, however, you see the problem. labels is an ArrayList, and I get an IllegalArgumentException.

I've overloaded my setLabels() to accept a comma-delimited String as follows:

Apparently the Reflection API checks to see the type passed into the setxxx() method matches the instance variable type?

So, my question is, how can I use the Reflection API to set an instance variable which is of type ArrayList?

Joel McNary

Joined: Aug 20, 2001
Posts: 1817

Parse the String and create the ArrayList yourself -- reflection cannot do that. Then you can use the set(Object, Object) method of the Field to set the value of the array list -- set(target, arrayList).

Alternatively, you caould use java.lang.reflect.Method to call your setLabels methods instead of using the java.lang.reflect.Field
[ December 08, 2004: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Thanks, Joel. I had put a hack on the code before I read your message. Basically, I created another instance variable labelsRawString which, you guessed it, is of type String and represents the comma-delimited String of values. In this case, the Reflection mechanism works as expected. But of course, I'm stuck with an instance variable that serves no other purpose than to receive some data and pass it along to the appropriate instance variable.

Something interesting I did notice. I put some extra logic in the setLabelsRawString(String s) method and found that when called via Reflection, it does not actually get executed?!

For instance, a dreadfully simple example:

When I used Reflection to set the respective instance variable, the message does not print out. Does the Reflection mechanism not use the actual setxxx() methods defined in my class? Or have I just overlooked something?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Jeff --

setXXX() methods are a convention, and in no way, shape, or form are they ever called automatically by any part of Java, nor are they absolutely tied, explicitly or implicitly, to any instance variable by name or by any other criterion. There is absolutely nothing to keep you from writing

If you set a field via reflection, then the value of the field is set directly. The Reflection API doesn't consider for a moment the existence of any setXXX methods.

Anyway, if you want to call setXXX(), then you actually have two choices. You can use java.lang.reflect.Method, not java.lang.reflect.Field; then you'd have to look up the setXXX() methods specifically.

Alternatively, you seem to want to treat the class like a JavaBean -- a class for which setXXX() and getXXX() methods are specifically intended, by convention to represent properties that you can set and read by calling them. If that's always going to be the case, then instead of using reflection directly, you want to use the java.beans.Introspector class and its associated machinery, which will find all the set/getXXX methods of a class and present them to you in a convenient format for use.

[Jess in Action][AskingGoodQuestions]
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Originally posted by Ernest Friedman-Hill:

If you set a field via reflection, then the value of the field is set directly. The Reflection API doesn't consider for a moment the existence of any setXXX methods.

Ahh, now it makes perfect sense. Thanks, Ernest. It does appear that this class lends itself to the JavaBean model, so to speak, as it would be handy to initialize its instance variables via introspection. I had not thought of this before -- I was focused on Reflection (a similar idea, but different, as I now see). Perhaps I can get ride of my crude hack and refurnish this class to use the Introspector features.

Thanks again!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Reflection to set ArrayList attribute
Similar Threads
Key Advantages of Java 5
Builder patern
Strange Collections Question from OCP Java SE 6 Programmer Practice Exams book by K & B
java.lang.Reflection throwing error when using (String)field.get(new String());
public vs private instance variables