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 claz, String... names)
find Field
Exception e = null;
Class<?> clas = claz;
while (claz != null) {
    for (String name : names) {
        try {
            return clas.getDeclaredField(name);
        } catch (Exception ee) {
            e = ee;
...
FieldfindField(Class clazz, final String field)
Finds a field by name.
do {
    for (Field f : clazz.getDeclaredFields()) {
        if (f.getName().equals(field)) {
            return f;
} while ((clazz = clazz.getSuperclass()) != null);
return null;
...
FieldfindField(Class clazz, String field)
Finds the field from the specified class
return Arrays.stream(clazz.getDeclaredFields()).filter(f -> f.getName().equals(field)).findFirst()
        .orElse(null);
FieldfindField(Class clazz, String field, Class type, int index)
find Field
int temp = index;
for (Field f : clazz.getDeclaredFields())
    if ((field == null || f.getName().equals(field)) && type.isAssignableFrom(f.getType()) && temp-- <= 0)
        return f;
if (clazz.getSuperclass() != null)
    return findField(clazz.getSuperclass(), field, type, index);
return null;
FieldfindField(Class clazz, String fieldName)
find Field
Class<?> typeBeingSearched = clazz;
while (typeBeingSearched != null && !typeBeingSearched.equals(Object.class)) {
    Field[] fields = typeBeingSearched.getDeclaredFields();
    for (Field field : fields) {
        if (fieldName.equals(field.getName())) {
            return field;
    typeBeingSearched = typeBeingSearched.getSuperclass();
throw new IllegalStateException("Field (" + fieldName + ") could not be found in " + clazz.getName());
FieldfindField(Class clazz, String fieldName)
find Field
for (Field field : clazz.getDeclaredFields()) {
    if (field.getName().equals(fieldName))
        return field;
return null;
FieldfindField(Class clazz, String fieldName)
find Field
Class<?> current = clazz;
do {
    try {
        return current.getDeclaredField(fieldName);
    } catch (Exception e) {
} while ((current = current.getSuperclass()) != null);
throw new NoSuchFieldException(fieldName);
...
FieldfindField(Class clazz, String fieldName)
find Field
Field field = null;
try {
    field = clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
if (field == null) {
    Class<?> superClazz = clazz.getSuperclass();
    if (superClazz != null) {
...
FieldfindField(Class clazz, String fieldName)
Find field.
if (clazz == null)
    throw new IllegalArgumentException("Null class");
if (fieldName == null)
    throw new IllegalArgumentException("Null field name");
try {
    Class<?> current = clazz;
    while (current != null) {
        try {
...
FieldfindField(Class clazz, String fname, boolean isSearchSuperclass)
find Field
Field f = null;
try {
    f = clazz.getDeclaredField(fname);
} catch (NoSuchFieldException e) {
if (f == null && isSearchSuperclass) {
    do {
        clazz = clazz.getSuperclass();
...