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