Java Reflection Field Find findField(Object obj, String fieldName)

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

Description

Find a field in given object

License

Apache License

Parameter

Parameter Description
obj a parameter
fieldName a parameter

Declaration

public static Field findField(Object obj, String fieldName) 

Method Source Code


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

import java.lang.reflect.Field;

public class Main {
    /**/*from w ww. ja va2s .c o  m*/
     * Find a field in given object
     * 
     * @param obj
     * @param fieldName
     * @return
     */
    public static Field findField(Object obj, String fieldName) {
        Class<?> clazz = obj.getClass();
        Field field = null;
        while (clazz != null && field == null) {
            try {
                field = clazz.getDeclaredField(fieldName);
            } catch (NoSuchFieldException | SecurityException e) {
                // Do nothing
            }

            clazz = clazz.getSuperclass();
        }

        return field;
    }
}

Related

  1. findField(final Object src, final String fieldName)
  2. findField(final String className, final String fieldName)
  3. findField(Object container, String memberName)
  4. findField(Object instance, String name)
  5. findField(Object obj, String beanPath)
  6. findField(Object obj, String fieldName, Class type)
  7. findField(Object obj, String name)
  8. findField(Object object, String fieldName)
  9. findField(Object object, String name, Class klass)