The current problem is that no field has the name "address.description" (it's not even a valid name for a field).
You have to get the address field first, access its class, and get the nested field from there. To do so, use getType():
Agreement agreement = new Agreement();
Class c = agreement.getClass(); // Agreement class
Field f = c.getDeclaredField("address"); // address field
Class<?> fieldClass = f.getType(); // class of address field (Address)
Field nested = fieldClass.getDeclaredField("description"); // description field
You can also chain the calls without local variables:
Field nested = c.getDeclaredField("address").getType().getDeclaredField("description");
Answer from Joffrey on Stack OverflowThe current problem is that no field has the name "address.description" (it's not even a valid name for a field).
You have to get the address field first, access its class, and get the nested field from there. To do so, use getType():
Agreement agreement = new Agreement();
Class c = agreement.getClass(); // Agreement class
Field f = c.getDeclaredField("address"); // address field
Class<?> fieldClass = f.getType(); // class of address field (Address)
Field nested = fieldClass.getDeclaredField("description"); // description field
You can also chain the calls without local variables:
Field nested = c.getDeclaredField("address").getType().getDeclaredField("description");
you must decompose operations.
Agreement agreement = new Agreement();
Field fAddress = agreement.getClass().getDeclaredField("address");
Address address = (Address)fAddress.get(agreement);
Field fDescription = address.getClass().getDeclaredField("description");
String description = (String)fDescription.get(address);
You should pass the object to get method of the field, so
import java.lang.reflect.Field;
Field field = object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
Object value = field.get(object);
Like answered before, you should use:
Object value = field.get(objectInstance);
Another way, which is sometimes prefered, is calling the getter dynamically. example code:
public static Object runGetter(Field field, BaseValidationObject o)
{
// MZ: Find the correct method
for (Method method : o.getMethods())
{
if ((method.getName().startsWith("get")) && (method.getName().length() == (field.getName().length() + 3)))
{
if (method.getName().toLowerCase().endsWith(field.getName().toLowerCase()))
{
// MZ: Method found, run it
try
{
return method.invoke(o);
}
catch (IllegalAccessException e)
{
Logger.fatal("Could not determine method: " + method.getName());
}
catch (InvocationTargetException e)
{
Logger.fatal("Could not determine method: " + method.getName());
}
}
}
}
return null;
}
Also be aware that when your class inherits from another class, you need to recursively determine the Field. for instance, to fetch all Fields of a given class;
for (Class<?> c = someClass; c != null; c = c.getSuperclass())
{
Field[] fields = c.getDeclaredFields();
for (Field classField : fields)
{
result.add(classField);
}
}