• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Setting value of a bean throught reflection

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
i have created a new bean vehicle = new Vehicle and i have used introspection to iterate over properties.
i am passing a propertyName and i want to set that field.
For example if the propertyName is make then i want to set the make value for the vehicleItem bean. at the same time i dont want to use if constructs to check if the property is make use vehicle.setMake(propertyValue)
is there a method where i can pass the object, the propertyName and propertyValue
i hope i am clear



Please let me knwo
Thanks
Rashid

 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For example if the propertyName is make then i want to set the make value for the vehicleItem bean. at the same time i dont want to use if constructs to check if the property is make use vehicle.setMake(propertyValue)



Reflection allow you to access the methods too -- so you can convert the property name to its setter, by upcasing the first character, and prepending "set". And calling the method.

Henry
 
Henry Wong
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:
Reflection allow you to access the methods too -- so you can convert the property name to its setter, by upcasing the first character, and prepending "set". And calling the method.



In thinking about this a bit more, it is probably better to use the Introspector classes -- because the JavaBean may not be following the naming conventions.

Anyway, how about this?



I had to (1) add the bean object to be set as a parameter, and (2) change the propertyValue parameter to be any generic type, as it may not be a string property. But otherwise, it should work as requested.

And to use -- something like this...



Henry
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Refering to the above solution, I am trying to introspect the static properties in my class.
On debugging the application I found out that, Introspection works only on Class instances,
is that right?

Also, do we need to implement the interface BeanInfo for inttrsopection?

Thanks,
Rama
 
Henry Wong
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Refering to the above solution, I am trying to introspect the static properties in my class.



Introspection is for Java Beans. Perhaps you are talking about reflection instead.

On debugging the application I found out that, Introspection works only on Class instances,
is that right?



Not sure what you mean.

Also, do we need to implement the interface BeanInfo for inttrsopection?



No. In fact, I have never done so myself. The only time that I needed to provide BeanInfo was for a MBean, which is a completely different animal.

Henry
 
Raja Gullapalli
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to load values from an XML file and initialize the static members of a Class. I've come across a problem where in trying to invoke
a setter method for "boolean" fails with :

java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.tsswireless.application.dispatch.config.LoadConfiguration.main(LoadConfiguration.java:83)

Trying to load values from the below XML file:





Should I be using the Boolean object instead of the primitive type? Ive tried with Boolean object, and it does not work either.
It fails at the point of invoking the set method with the parameters.

Thanks,
Rama
 
Raja Gullapalli
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resolved the issue. The problem was the Class methods that were being invoked were not marked static.
And the invoke call is being passed a null value. For non - static members an instance of the class is supposed to be passed,
for static members null is to be used.

Thanks for your help on this.

Thanks,
Rama
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!