Java Reflection Field Find findField(Class cls, String name)

Here you can find the source of findField(Class cls, String name)

Description

Finds the first (from the bottom of the inheritance hierarchy) field with the specified name.

License

Open Source License

Parameter

Parameter Description
cls java.lang.Class
name String

Return

Field

Declaration


public static Field findField(Class cls, String name) throws NoSuchFieldException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Field;

public class Main {
    /**/*  ww w.  j a  v a2s . c o m*/
     * Finds the first (from the bottom of the inheritance hierarchy) field with
     * the specified name. Note that Class.getField returns only public fields.
     * 
     * @return Field
     * @param cls
     *            java.lang.Class
     * @param name
     *            String
     */
    //start extract findField
    public static Field findField(Class cls, String name) throws NoSuchFieldException {
        if (cls != null) {
            try {
                return cls.getDeclaredField(name);
            } catch (NoSuchFieldException e) {
                return findField(cls.getSuperclass(), name);
            }
        } else {
            throw new NoSuchFieldException();
        }
    }
}

Related

  1. findField(Class clazz, String fieldName)
  2. findField(Class clazz, String name)
  3. findField(Class clazz, String name)
  4. findField(Class clazz, String name)
  5. findField(Class clazz, String name)
  6. findField(Class objectClass, String fieldName)
  7. findField(Class type, String fieldName)
  8. findField(Class c, String name)
  9. findField(Class c, String property, Class propertyType)