Problems invoking a getter method using reflection?

P

Pep

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.
 
P

ph

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
 
P

Pep

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.

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

Thanks,
Pep.
 
P

Pep

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


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


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

Thanks,
Pep.

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

Cheers,
Pep.
 
Joined
Aug 22, 2006
Messages
1
Reaction score
0
plz reffer it , may could your Help

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

/*------------------------------------------------*/
plz go through this program


Method[] refMethod=refClassObject.getDeclaredMethods();
for(int i=0;i<refMethod.length;i++)
{
try{
Class[] parameterTypes = refClassObject.getClasses();
String methodName=refMethod.getName();
if(StringUtils.countMatches(methodName,"get")>0 || StringUtils.countMatches(methodName,"is")>0)
{
String methodType=refMethod.getReturnType().getName();
Method m=refClassObject.getMethod(methodName,parameterTypes );
String s=String.valueOf(m.invoke(objectInSession,null));
System.err.println(StringUtils.rightPad(methodType,35,null)+" "+ StringUtils.rightPad(methodName,35,null)+ " "+s);
}
}catch(Exception errr)
{

}

}






/*------------------------------------------------*/
 
Joined
Jul 8, 2009
Messages
1
Reaction score
0
And you probably want to ensure that the first chanracter is upper case:
...
String firstLetter = fieldName.substring(0, 1);
String remainingPart = fieldName.substring(1, fieldName.length());
String methodName = "get" + firstLetter.toUpperCase() + remainingPart;
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top