FieldInfo.java
//some package name: your package name
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//********************************************************************
//Given a textual name of a field, set its value and get its value
//using a string interface.
//********************************************************************
public class FieldInfo
{
//For clarity of code I am not generating the getters and setters
//Generate them before production
//Primary fields
//Inputs needed to construct this
public String m_name = null; //textual name of the field
public String m_xmlName = null; //an xml name alias for the field
public boolean m_attribute = false; //Is this an attribute
private Class m_class = null; //What class is this field part of
//Derived fields
public Method m_getAccessor = null; //get method for the field value
public Method m_setAccessor = null; //set method for the field falue
private Class m_fieldType = null; //What is the java field type
//********************************************************************
//Given an object of this type return the value as string
//********************************************************************
String getValueAsString(Object o)
throws InvocationTargetException, IllegalAccessException
{
Object reply = m_getAccessor.invoke(o,null);
return reply.toString();
}
//********************************************************************
//Given an object of this type set this fields value from a string
//********************************************************************
public void setValueUsingString(Object o, String fieldValue)
throws InvocationTargetException, IllegalAccessException
{
Object parameter = ReflectionUtils.getStringAsObject(m_fieldType.getName(),fieldValue);
Object paramArray[] = new Object[1];
paramArray[0] = parameter;
Object reply = m_setAccessor.invoke(o,paramArray);
}
//********************************************************************
//Primary constructor
//********************************************************************
public FieldInfo(Class fieldClass
, String name
, String xmlName
, boolean isAttribute)
throws NoSuchMethodException, NoSuchFieldException
{
m_name = name;
m_xmlName = xmlName;
m_attribute = isAttribute;
m_class = fieldClass;
m_fieldType = getFieldType(name);
m_getAccessor = getAccessorGetMethod(name);
m_setAccessor = getAccessorSetMethod(name);
}
//********************************************************************
//Default attribute constructor
//********************************************************************
public FieldInfo(Class fieldClass,String name, String xmlName)
throws NoSuchMethodException, NoSuchFieldException
{
this(fieldClass,name,xmlName,false);
}
//********************************************************************
//Default xmlname and attribute constructor
//********************************************************************
public FieldInfo(Class fieldClass, String name)
throws NoSuchMethodException, NoSuchFieldException
{
this(fieldClass,name,name,false);
}
private Method getAccessorGetMethod(String fieldName)
throws NoSuchMethodException
{
String firstPart = fieldName.substring(0,1);
String secondPart = fieldName.substring(1);
String getMethodName = "get" + firstPart.toUpperCase() + secondPart;
//System.out.println(getMethodName);
return m_class.getDeclaredMethod(getMethodName,null);
}
private Method getAccessorSetMethod(String fieldName)
throws NoSuchMethodException
{
String firstPart = fieldName.substring(0,1);
String secondPart = fieldName.substring(1);
String getMethodName = "set" + firstPart.toUpperCase() + secondPart;
//System.out.println(getMethodName);
Class parameters[] = new Class[1];
parameters[0] = this.m_fieldType;
return m_class.getDeclaredMethod(getMethodName,parameters);
}
private Class getFieldType(String fieldName)
throws NoSuchFieldException
{
Field field = m_class.getDeclaredField(fieldName);
Class fieldClass = field.getType();
return fieldClass;
}
private String getTypeName(String fieldName)
{
return this.m_fieldType.getName();
}
}