Commentss required for reflection bean copying util class

B

Ben Jessel

Hi,

I've writing a simple conversion utility that copies the contents of
one bean to another. I'm interested in any feedback. Is the way I have
written this a good approach to approach things? It's pretty
straightforward when you're copying say,
getUserName(String)->setUserName(String), but a different matter when
you're dealing with conversion with primitives
getUserName(String)->setUserName(long).

Is there a more concise way of doing this? And what do I do for the
character conversion, towards the bottom? ( convert it to an int and
cast it? )

Thanks

Ben

/**
* <p>
* </p>
* @param a_Source
* @param a_Destination
* @return
*/
public Object populate ( Object a_Source, Object a_Destination )
{
Map srcProperties = getBeanProperties( a_Source, SELECTORS );
Map destProperties = getBeanProperties( a_Destination,
INJECTORS );


Iterator keySetIterator = srcProperties.keySet().iterator();
while ( keySetIterator.hasNext() )
{
String selectorKey = ( String ) ( keySetIterator.next()
);
Method selectorMethod = ( Method ) srcProperties.get(
selectorKey );

if ( destProperties.get( selectorKey )!=null )
{
Method injectorMethod = ( ( Method )
destProperties.get( selectorKey ) );

try
{

if ( injectorMethod.getParameterTypes().length ==
1 )
{
Object selectorResult[] = new Object[1];
selectorResult[0] = selectorMethod.invoke(
a_Source, null );

if (
injectorMethod.getParameterTypes()[0].isAssignableFrom(
selectorMethod.getReturnType()) )
{
// get the results from the selector
object
injectorMethod.invoke( a_Destination,
selectorResult );

}
else
{
if (
injectorMethod.getParameterTypes()[0].getName().equals(
"java.lang.String" ) )
{
Class clazz =
selectorMethod.getReturnType();
System.out.println(clazz.getName());

selectorResult[0] =
String.valueOf( selectorResult[0] );
injectorMethod.invoke(
a_Destination, selectorResult );
}

if (
injectorMethod.getParameterTypes()[0].getName().equals(
"java.lang.Long" ) )
{
Class clazz =
selectorMethod.getReturnType();
System.out.println(clazz.getName());

selectorResult[0] = Long.valueOf(
String.valueOf(selectorResult[0]) );
injectorMethod.invoke(
a_Destination, selectorResult );
}

if (
injectorMethod.getParameterTypes()[0].getName().equals( "long" ) )
{
Class clazz =
selectorMethod.getReturnType();
System.out.println(clazz.getName());

selectorResult[0] = Long.valueOf(
String.valueOf(selectorResult[0]==null ? "0" : selectorResult[0]) );
injectorMethod.invoke(
a_Destination, selectorResult );
}

if (
injectorMethod.getParameterTypes()[0].getName().equals( "int" ) )
{
Class clazz =
selectorMethod.getReturnType();
System.out.println(clazz.getName());

selectorResult[0] =
Integer.valueOf( String.valueOf(selectorResult[0]==null ? "0" :
selectorResult[0]) );
injectorMethod.invoke(
a_Destination, selectorResult );
}
if (
injectorMethod.getParameterTypes()[0].getName().equals( "byte" ) )
{
Class clazz =
selectorMethod.getReturnType();
System.out.println(clazz.getName());

selectorResult[0] = Byte.valueOf(
String.valueOf(selectorResult[0]==null ? "0" : selectorResult[0]) );
injectorMethod.invoke(
a_Destination, selectorResult );
}
if (
injectorMethod.getParameterTypes()[0].getName().equals( "float" ) )
{
Class clazz =
selectorMethod.getReturnType();
System.out.println(clazz.getName());

selectorResult[0] = Float.valueOf(
String.valueOf(selectorResult[0]==null ? "0" : selectorResult[0]) );
injectorMethod.invoke(
a_Destination, selectorResult );
}
if (
injectorMethod.getParameterTypes()[0].getName().equals( "double" ) )
{
Class clazz =
selectorMethod.getReturnType();
System.out.println(clazz.getName());

selectorResult[0] = Float.valueOf(
String.valueOf(selectorResult[0]==null ? "0" : selectorResult[0]) );
injectorMethod.invoke(
a_Destination, selectorResult );
}
if (
injectorMethod.getParameterTypes()[0].getName().equals( "char" ) )
{
// ??
}
if (
injectorMethod.getParameterTypes()[0].getName().equals(
"java.util.Date" ) )
{
selectorResult[0] = new Date(
String.valueOf(selectorResult[0]==null ? "" : selectorResult[0]) );
injectorMethod.invoke( a_Destination,
selectorResult );

}
}

}
}
catch (InvocationTargetException ite ) {}
catch (IllegalAccessException iea) {}
}

}
return a_Destination;
}




/**
* <p>
* This gets the bean properties for a supplied bean.
* A map is constructed where the key is the property name ( the
method
* name minus the starting set/get ). The value is the method
itself.
* This means that we can have fast searching by property name
on the key
* as well as quick method parameter type / return type.
* The options for the <code>a_PropertyType</code> are:
* <ul>
* <li> 1 - get SELECTOR bean properties. </ul>
* <li> 2 - get INJECTOR bean properties. </ul>
* </ul>
* Injector properties must have only one parameter.
* Selectors must have a non-void return type.
* </p>
* @param a_Bean
* @param a_PropertyType
* @return
*/
public static Map getBeanProperties ( Object a_Bean, int
a_PropertyType )
/*
---------------------------------------------------------------------------
d1 08 Sept 02 Ben Jessel Created.
---------------------------------------------------------------------------
*/
{
// get all the methods for this class
Method[] beanMethods = a_Bean.getClass().getMethods();
Map propertyMap = new HashMap();

for ( int i=0; i<beanMethods.length; i++ )
{
if ( ( a_PropertyType == SELECTORS ) &&
( beanMethods [ i ].getName().startsWith( "get" ) ) )
{
if ( !beanMethods [ i
].getReturnType().getName().equals("void"))
{
// Get the name of this method, and remove the
"get" to just
// give a property name
String propertyName = beanMethods [ i
].getName();
propertyName = propertyName.substring(
propertyName.indexOf("get")+3 );

propertyMap.put ( propertyName, beanMethods [ i ]
);
}
}
else if ( ( a_PropertyType == INJECTORS ) &&
( beanMethods [ i ].getName().startsWith( "set" ) ) )
{
if ( beanMethods [ i
].getParameterTypes().length==1 )
{
// Get the name of this method, and remove the
"set" to just
// give a property name
String propertyName = beanMethods [ i
].getName();
propertyName = propertyName.substring(
propertyName.indexOf("set")+3 );

propertyMap.put ( propertyName, beanMethods [ i
] );

}
}


}
return propertyMap;
}
}
 
B

Ben Jessel

VisionSet said:
Hope this was an academic exercise.

Thanks. It was partially academic I wrote it for myself ( ...and
certainly no money changed hands! ). I've been reading Rod Johnson's
Expert 1-2-1 book, and I've been trying to implement some custom code
for populating beans, while avoiding tying myself to struts API.
BeanUtils seems to use a map of parameter names to populate a bean,
I'm trying to short circuit that somewhat.
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top