Java Reflection Field Find findFieldOfBean(Object bean, String fieldName)

Here you can find the source of findFieldOfBean(Object bean, String fieldName)

Description

find Field Of Bean

License

Open Source License

Declaration

public static Field findFieldOfBean(Object bean, String fieldName) 

Method Source Code


//package com.java2s;
import java.lang.reflect.Field;

public class Main {
    public static Field findFieldOfBean(Object bean, String fieldName) {
        Field field = null;//from  w w w.ja v a  2  s.  c om
        Class<?> clazz = bean.getClass();
        while (clazz != null && field == null) {
            field = findField(clazz.getDeclaredFields(), fieldName);
            clazz = clazz.getSuperclass();
        }
        if (field == null)
            throw new IllegalStateException("Field " + fieldName + " not found");
        return field;
    }

    public static Field findField(Field[] fields, String name) {
        for (Field field : fields)
            if (field.getName().equals(name))
                return field;
        return null;
    }
}

Related

  1. findFieldIn(Class type, String name)
  2. findFieldInClass(Class clazz, String fieldName)
  3. findFieldInClassHierarchy(Class clazz, String fieldName)
  4. findFieldIncludeSuperclass(String fieldName, Class clazz)
  5. findFieldInternal(Class currentClass, Class annotation, Set fields)
  6. findFieldOfTypeInClass(final Class source, final Class type)
  7. findFieldRecursively(Class c, String fieldName)
  8. findFields(Class c, boolean allowTransient, int max, Iterable> fieldClassesToFind, Iterable> exceptClasses)
  9. findFields(Class type)