Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Problems invoking a getter method using reflection?

Thread view: 
Pep - 30 Nov 2005 10:51 GMT
I am trying to write a bean copy tool but am finding it hard to get the
results form a getter method?

I have already written a successful version of this tool that transfers
columns from a sql result set to a bean so am not daunted by reflection but
am confused as to how I invoke a method that returns a value.

The code I am attempting to use to obtain the String result form the getter
method of the bean is this.

private static String getBeanField(Object bean, String fieldName)
{
       String value = "";
       String methodName = "get" + fieldName;

       try
       {
               // Get the class type of the bean.
               Class beanClass = bean.getClass();
               // Get the method to be invoked
               Method beanMethod = beanClass.getMethod(fieldName, null);
               // Invoke the method against the bean with the parameter arguments
               value = (String)beanMethod.invoke(bean, null);
       }
       catch (Throwable e)
       {
               // Empty catch.
       }

       return(value);
}

The bean looks like this

class Bean
{
       private String firstName;

       public void Bean(String value)
       {
               setFirstName(value);
       }

       public void setFirstName(String value)
       {
               firstName = value;
       }

       public String getFirstName()
       {
               return(firstName);
       }

}

And I try the above code like this

Bean bean = new Bean("Pep");
String firstName = getBeanField(bean, "FirstName");

but the value returned from getBeanField is always "".

TIA,
Pep.
ph@semm.tmfweb.nl - 30 Nov 2005 11:44 GMT
beanMethod = beanClass.getMethod(fieldName, null);
nope,

should be :
beanMethod = beanClass.getMethod(methodName, null);

As you can see, you should always have some feedback from a catch.

Paul Hamaker
http://javalessons.com
Pep - 30 Nov 2005 14:50 GMT
> beanMethod = beanClass.getMethod(fieldName, null);
> nope,
>
> should be :
> beanMethod = beanClass.getMethod(methodName, null);

LOL, case of "can't see the wood for the trees" :)

> As you can see, you should always have some feedback from a catch.

Yep but I trimmed this code down significantly to post it for some help.

> Paul Hamaker
> http://javalessons.com

Funnily this now works for the test program but still does not work when run
from within a servlet?

Thanks,
Pep.
Pep - 01 Dec 2005 09:35 GMT
>> beanMethod = beanClass.getMethod(fieldName, null);
>> nope,
[quoted text clipped - 16 lines]
> Thanks,
> Pep.

Actually scratch that last comment, the modified servlet had not been
redeployed :(

Cheers,
Pep.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.