• 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

Is it the right way of doing?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have few bean obejcts with some variables along with getters and setters .
I want to see if all the varaibles of these object are null or not.

For a given passed object i have written the following method.

private boolean XXXXXXX(Object obj)
{
Method[] methodArray= obj.getClass().getMethods();
boolean isObjectEmpty = true;
for (int i=0;i<methodArray.length;i++)
{
try
{
if (methodArray[i].getName().startsWith("get") &&
!methodArray[i].getName().equals("getClass"))
{
if (methodArray[i].invoke(obj, null) != null )
{
isObjectEmpty = false;
break;
}
}
}
catch(IllegalAccessException e)
{
//do something
}
catch(InvocationTargetException e)
{
//do something
}
}
return isObjectEmpty;
}

I wanted to check with you guys whether is it right way or is there any better way of doing it?
[ July 10, 2006: Message edited by: nan sud ]
 
author
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you would like, you can cache your reflected values to improve performance.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!