Java Utililty Methods Reflection Field Find

List of utility methods to do Reflection Field Find

Description

The list of methods to do Reflection Field Find are organized into topic(s).

Method

FieldfindField(Class clazz, String name)
Finds a field of a given class recursively by stepping up the inheritance chain.
Class currentClass = clazz;
while (currentClass != Object.class) {
    for (Field field : currentClass.getDeclaredFields()) {
        if (name.equals(field.getName())) {
            return field;
    currentClass = currentClass.getSuperclass();
...
FieldfindField(Class clazz, String name)
find Field
return findField(clazz, name, (Class) null);
FieldfindField(Class clazz, String name)
find Field
List<Field> fields = getAllDeclaredFields(clazz);
for (Field field : fields) {
    if (name.equals(field.getName()))
        return field;
return null;
FieldfindField(Class cls, String name)
Finds the first (from the bottom of the inheritance hierarchy) field with the specified name.
if (cls != null) {
    try {
        return cls.getDeclaredField(name);
    } catch (NoSuchFieldException e) {
        return findField(cls.getSuperclass(), name);
} else {
    throw new NoSuchFieldException();
...
FieldfindField(Class objectClass, String fieldName)
find Field
Class cursor = objectClass;
while (cursor != null) {
    try {
        return cursor.getDeclaredField(fieldName);
    } catch (NoSuchFieldException ex) {
    cursor = cursor.getSuperclass();
throw new RuntimeException(
        String.format("Class %s does not contain a field named '%s'.", objectClass.getName(), fieldName));
FieldfindField(Class type, String fieldName)
Find the field with the given name in the class.
while (type != null) {
    Field[] fields = type.getDeclaredFields();
    for (Field field : fields) {
        if (field.getName().equals(fieldName))
            return field;
    type = type.getSuperclass();
return null;
FieldfindField(Class c, String name)
find Field
if (c == null) {
    return null;
String cacheKey = c.getName() + name;
Field f = refFieldsCache.get(cacheKey);
if (f != null) {
    return f;
try {
    f = c.getDeclaredField(name);
    f.setAccessible(true);
    refFieldsCache.put(cacheKey, f);
    return f;
} catch (Exception err) {
Class<?> s = c.getSuperclass();
if (s != null) {
    f = findField(s, name);
    if (f != null) {
        refFieldsCache.put(cacheKey, f);
    return f;
return null;
FieldfindField(Class c, String property, Class propertyType)
Returns a public field for a given property
Field result = null;
Field[] fields = c.getFields();
for (Field f : fields) {
    String fn = f.getName();
    if (fn.charAt(0) == '_') {
        fn = fn.substring(1);
    if (fn.equals(property) && (propertyType == null || f.getType().isAssignableFrom(propertyType))) {
...
FieldfindField(Class cl, String fieldName)
find Field
try {
    return cl.getDeclaredField(fieldName);
} catch (SecurityException sex) {
    return null;
} catch (NoSuchFieldException e) {
    if (Objects.equals(cl, Object.class)) {
        throw e;
    return findField(cl.getSuperclass(), fieldName);
FieldfindField(Class classToCheck, String propertyName)
find Field
if (classToCheck == null || Object.class.equals(classToCheck)) {
    return null;
try {
    return classToCheck.getDeclaredField(propertyName);
} catch (NoSuchFieldException nsfe) {
    return findField(classToCheck.getSuperclass(), propertyName);