java/sample code: General idea of a typeface
/*
* Created on Oct 1, 2005
*/
package com.ai.typefaces;
import java.util.Iterator;
import com.ai.common.IDictionary;
import com.ai.reflection.BeanInfo;
import com.ai.reflection.BeanInfoRegistry;
import com.ai.reflection.FieldInfo;
import com.ai.reflection.ReflectionException;
/**
* @author Satya Komatineni
*
*/
public class TypeFaceFacility implements ITypeFaceFacility
{
public Object castTo(Class objectClass, IDictionary dictionary)
throws ReflectionException
{
//Instantiate the object based on the class
//instantiate a bean info for this object
//For each public field infos
// get its name
// get its value from dictionary
// set the value using get or directly on the field info
// return the object
BeanInfo bi = BeanInfoRegistry.getBeanInfo(objectClass);
Object targetObject = bi.constructObject();
Iterator fieldInfoItr = bi.getFieldInfos();
while(fieldInfoItr.hasNext())
{
//for each field info
FieldInfo fi = (FieldInfo)fieldInfoItr.next();
Object fieldValue = dictionary.get(fi.m_name);
fi.setValueUsingString(targetObject,(String)fieldValue);
}
return null;
}
}