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

FieldfindFieldOfBean(Object bean, String fieldName)
find Field Of Bean
Field field = null;
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");
...
FieldfindFieldOfTypeInClass(final Class source, final Class type)
find Field Of Type In Class
for (final Field e : source.getDeclaredFields()) {
    if (e.getType().equals(type)) {
        e.setAccessible(true);
        return e;
return null;
FieldfindFieldRecursively(Class c, String fieldName)
find Field Recursively
Field f = null;
try {
    f = c.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
    if (!c.equals(Object.class))
        f = findFieldRecursively(c.getSuperclass(), fieldName);
return f;
...
ListfindFields(Class c, boolean allowTransient, int max, Iterable> fieldClassesToFind, Iterable> exceptClasses)
Find in the given class and all its superclasses all non-static fields assignable to the specified classes to find, except of the specified 'except' classes, and returns such fields as a list.
List<Field> result = new ArrayList<Field>(max > 0 && max < 10 ? max : 10);
while (c != Object.class && c != null) {
    for (Field field : c.getDeclaredFields()) {
        if (Modifier.isStatic(field.getModifiers())) {
            continue;
        if (!allowTransient && Modifier.isTransient(field.getModifiers())) {
            continue;
...
MapfindFields(Class type)
Loads or fetches from the cache a Map of Field objects keyed into the Map by the field name they correspond to.
Map<String, Field> fieldMap;
synchronized (fieldCache) {
    fieldMap = fieldCache.get(type);
    if (fieldMap != null) {
        return fieldMap;
    fieldMap = new HashMap<String, Field>();
    Field[] fields = type.getFields();
...
ListfindFields(final Class clazz, final Predicate filter)
find Fields
final List<Field> fields = new ArrayList<>();
collectFields(clazz, filter, fields);
fields.forEach(field -> field.setAccessible(true));
return fields;
SetfindFieldsAnnotatedWith(Class annotation, Class parentClass)
find Fields Annotated With
Set<Field> annotatedFields = new HashSet<Field>();
for (Field field : parentClass.getDeclaredFields()) {
    if (field.isAnnotationPresent(annotation)) {
        annotatedFields.add(field);
return annotatedFields;
ListfindFieldsAnnotatedWith(final Class type, final Class annotation)
find Fields Annotated With
List<Field> fields = new ArrayList<Field>();
Class<?> klass = type;
while (klass != null && klass != Object.class) { 
    final List<Field> allFields = new ArrayList<Field>(Arrays.asList(klass.getDeclaredFields()));
    for (final Field field : allFields) {
        if (annotation == null || field.isAnnotationPresent(annotation)) {
            fields.add(field);
    klass = klass.getSuperclass();
return fields;
voidfindFieldsOfClass(Class target, Object o, String path, Logger log, Set done)
find Fields Of Class
if (null == path || path.equals("")) {
    path = "\nthis";
if (null == done) {
    done = new HashSet();
if (done.contains(o)) {
    return;
...
FieldfindFieldToInject(Class target, String name, Class source)
find Field To Inject
Field f = getField(target, name);
if (f != null) {
    Class<?> type = f.getType();
    if (type.isAssignableFrom(source) || checkPrimitives(type, source)) {
        f.setAccessible(true);
        return f;
return null;