Java Reflection Field Find findField(final Object src, final String fieldName)

Here you can find the source of findField(final Object src, final String fieldName)

Description

find Field

License

Apache License

Declaration

private static Field findField(final Object src, final String fieldName) throws NoSuchFieldException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {
    private static Field findField(final Object src, final String fieldName) throws NoSuchFieldException {
        Class<?> current = src.getClass();
        Field field = null;//from   w ww  . j a va  2 s  .  co  m

        while (current != null && field == null) {
            field = getField(current, fieldName);
            current = current.getSuperclass();
        }

        if (field == null) {
            throw new NoSuchFieldException(src + " does not declare " + fieldName);
        }

        return field;
    }

    private static Field getField(Class<?> clazz, final String fieldName) {
        for (Field field : clazz.getDeclaredFields()) {
            if (field.getName().equals(fieldName)) {
                return field;
            }
        }
        return null;
    }
}

Related

  1. findField(final Class cls, final String fieldName)
  2. findField(final Class currentClass, final String fieldName)
  3. findField(final Class declaringClass, final String fieldName)
  4. findField(final Class klaz, final String fieldName)
  5. findField(final Object instance, final String name)
  6. findField(final String className, final String fieldName)
  7. findField(Object container, String memberName)
  8. findField(Object instance, String name)
  9. findField(Object obj, String beanPath)