searches for field type in clazz and its super classes. - Android java.lang.reflect

Android examples for java.lang.reflect:Field

Description

searches for field type in clazz and its super classes.

Demo Code



public class Main {
  /**//from  w  w w  .java 2s  .c om
   * searches for field type in clazz and its super classes.
   * 
   * @param clazz
   * @param fieldName
   * @return type of the field if it is found else null
   */
  public static Class<?> findFieldType(Class<?> clazz, String fieldName) {
    Class<?> type = clazz;
    while (!type.equals(Object.class)) {
      try {
        return type.getDeclaredField(fieldName).getType();
      } catch (SecurityException e) {
        throw e;
      } catch (NoSuchFieldException e) {
        type = type.getSuperclass();
      }
    }
    return null;
  }
}

Related Tutorials