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(Object object, String name, Class klass)
find Field
Field f = object.getClass().getDeclaredField(name);
f.setAccessible(true);
if (f.getType() == klass) {
    return f;
} else {
    return null;
FieldfindField(Object propertyId, Class classOfItem)
find Field
Field declaredField;
try {
    declaredField = classOfItem.getDeclaredField(String.valueOf(propertyId));
} catch (NoSuchFieldException e) {
    declaredField = null;
if (declaredField == null && !Object.class.equals(classOfItem))
    return findField(propertyId, classOfItem.getSuperclass());
...
TfindField(Object target, Class type)
find Field
List<T> matches = new ArrayList<T>();
for (Class<?> cl = target.getClass(); cl != Object.class; cl = cl.getSuperclass()) {
    for (Field field : cl.getDeclaredFields()) {
        if (type.isAssignableFrom(field.getType())) {
            field.setAccessible(true);
            try {
                matches.add(type.cast(field.get(target)));
            } catch (IllegalAccessException e) {
...
FieldfindField(Object target, String fieldName)
Finds a field with the given name.
Class<?> targetClass = target.getClass();
while (targetClass != Object.class) {
    try {
        Field field = targetClass.getDeclaredField(fieldName);
        return field;
    } catch (NoSuchFieldException e) {
        targetClass = targetClass.getSuperclass();
throw new NoSuchFieldException();
FieldfindField(String fieldName, ArrayList fields)
find Field
for (Field field : fields) {
    if (field.getName().equals(fieldName)) {
        return field;
return null;
FieldfindField(String fieldName, Class cl)
Find a field on a class.
Class scannedClass = cl;
do {
    try {
        return scannedClass.getDeclaredField(fieldName);
    } catch (NoSuchFieldException | SecurityException e) {
        scannedClass = scannedClass.getSuperclass();
} while (scannedClass != Object.class);
...
FieldfindField(String fieldName, Class type)
find Field
Field field = null;
while (field == null && !type.equals(Object.class)) {
    try {
        field = type.getDeclaredField(fieldName);
    } catch (Throwable ignore) {
    } finally {
        type = type.getSuperclass();
return field;
FieldfindField(String name, Class c)
find Field
Field[] fields = c.getDeclaredFields();
Field field = null;
for (Field f : fields) {
    if (f.getName().equals(name)) {
        field = f;
        break;
if (field == null) {
    if (c.getSuperclass() != null) {
        return findField(name, c.getSuperclass());
    } else {
        return null;
} else {
    field.setAccessible(true);
    return field;
FieldfindField(String name, Object o)
find Field
Field foundField = null;
Field[] fields = o.getClass().getDeclaredFields();
for (Field field : fields) {
    if (field.getName().equals(name)) {
        field.setAccessible(true);
        foundField = field;
        break;
return foundField;
FieldfindField(String string, Class clazz)
find Field
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
    if (field.getName().equals(string)) {
        field.setAccessible(true);
        return field;
return null;
...